chunk-string-array v1.0.0
Chunk
Chunks a string array based on string length.
Installation
$ npm install chunk-string-arrayUsage
var chunk = require( 'chunk-string-array' );chunk( arr, length, options );
Chunks a string array based on string length. The array elements are split into subarrays according to a length parameter, which specifies a combined length of all subarray elements.
var out = chunk( ['abc', 'de', 'fg', 'h', 'ijkl' ], 3 );
/*
[
['abc'],
['de'],
['fg','h'],
['ijkl']
]
*/The function accepts the following options:
- strict:
booleanindicating whether thelengthcriterion should be strictly enforced; i.e., no combined subarraylengthshould exceed the criterion. Such a condition can occur when a singlestringexceeds thelength. Setting this option totrueensures that allstringsmust be less than or equal to thelengthvalue. Default:false.
To strictly enforce a maximum subarray length, set the strict option to true.
var out = chunk( ['abc', 'de', 'fg', 'h', 'ijkl' ], 3, {
'strict': true
});
// => throws ErrorNotes
- The
functionsplitsarrayelements atomically; i.e., subarrays do not contain partialstringelements.
Examples
var mnames = require( 'datasets-male-first-names-en' );
var fnames = require( 'datasets-female-first-names-en' );
var shuffle = require( 'compute-shuffle' );
var chunk = require( 'chunk-string-array' );
// Create random groups of people under the constraint that the sum of all first names of people within a group cannot exceed 40 characters...
var maxLength = 40;
var names = mnames.concat( fnames );
shuffle( names );
var groups = chunk( names, maxLength );
for ( var i = 0; i < groups.length; i++ ) {
console.log( groups[ i ].join( ',' ) );
}To run the example code from the top-level application directory,
$ node ./examples/index.jsTests
Unit
This repository uses tape for unit tests. To run the tests, execute the following command in the top-level application directory:
$ make testAll new feature development should have corresponding unit tests to validate correct functionality.
Test Coverage
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-covIstanbul creates a ./reports/coverage directory. To access an HTML version of the report,
$ make view-covBrowser Support
This repository uses Testling for browser testing. To run the tests in a (headless) local web browser, execute the following command in the top-level application directory:
$ make test-browsersTo view the tests in a local web browser,
$ make view-browser-testsLicense
Copyright
Copyright © 2016. Athan Reines.