README
Apportions arrays or strings into blocks of the requested size.
Install
Install with npm:
$ npm install --save apportion-es5
Usage
var apportion = require('apportion-es5');
// apportion(indexable, blockSize, onlyComplete = false, overlapping = false)
console.log(apportion([1, 2, 3, 4, 5], 2)); // => [ [ 1, 2 ], [ 3, 4 ], [ 5 ] ]
console.log(apportion([1, 2, 3, 4, 5], 3)); // => [ [ 1, 2, 3 ], [ 4, 5 ] ]
console.log(apportion('ABCDEFG', 3)); // => [ 'ABC', 'DEF', 'G' ]
By default, incomplete elements (those that have a length less than the requested block size) are also supplied, but they can also be removed:
console.log(apportion([1, 2, 3, 4, 5], 2, true)); // => [ [ 1, 2 ], [ 3, 4 ] ]
console.log(apportion('ABCDE', 2, true)); // => [ 'AB', 'CD' ]
You can also apportion in an overlapping fashion:
console.log(apportion([1, 2, 3], 2, false, true)); // => [ [ 1, 2 ], [ 2, 3 ], [ 3 ] ]
console.log(apportion('ABCDE', 2, true, true)); // => [ 'AB', 'BC', 'CD', 'DE' ]
This version is designed for ECMAScript 5 (ES5) and can only apportion arrays or strings. apportion is recommended for ECMAScript 2015 (ES6/ES2015) and above.
Tests
$ npm test
Implemented using jest.
License
MIT license: see LICENSE.