apportion

Apportions arrays or strings into blocks

Usage no npm install needed!

<script type="module">
  import apportion from 'https://cdn.skypack.dev/apportion';
</script>

README

apportion NPM version

Apportions arrays, strings or any other values with a slice(...) method and length field into blocks of the requested size.

Install

Install with npm:

$ npm install --save apportion

Usage

const apportion = require('apportion');
// 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' ]

Note that the module is written for ECMAScript 2015 (ES6/ES2015) and above. If you need a version that works on ES5, see apportion-es5.

Tests

$ npm test

Implemented using jest.

License

MIT license: see LICENSE.