chunk-string-array
v1.0.0
Published
Chunks a string array based on string length.
Downloads
13
Maintainers
Readme
Chunk
Chunks a string array based on string length.
Installation
$ npm install chunk-string-array
Usage
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:
boolean
indicating whether thelength
criterion should be strictly enforced; i.e., no combined subarraylength
should exceed the criterion. Such a condition can occur when a singlestring
exceeds thelength
. Setting this option totrue
ensures that allstrings
must be less than or equal to thelength
value. 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 Error
Notes
- The
function
splitsarray
elements atomically; i.e., subarrays do not contain partialstring
elements.
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.js
Tests
Unit
This repository uses tape for unit tests. To run the tests, execute the following command in the top-level application directory:
$ make test
All 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-cov
Istanbul creates a ./reports/coverage
directory. To access an HTML version of the report,
$ make view-cov
Browser 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-browsers
To view the tests in a local web browser,
$ make view-browser-tests
License
Copyright
Copyright © 2016. Athan Reines.