1.0.0 • Published 8 years ago

chunk-string-array v1.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
8 years ago

Chunk

NPM version Build Status Coverage Status Dependencies

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 the length criterion should be strictly enforced; i.e., no combined subarray length should exceed the criterion. Such a condition can occur when a single string exceeds the length. Setting this option to true ensures that all strings must be less than or equal to the length 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 splits array elements atomically; i.e., subarrays do not contain partial string 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

MIT license.

Copyright

Copyright © 2016. Athan Reines.