Split a list into rows with start or end alignment and padding
A photo gallery lays thumbnails out in rows of a fixed width, and a number formatter groups digits in threes from the right. Both are the same operation: split a list into rows of size items. They differ only in where the short row goes and whether it is padded.
Implement chunkRows(items, size, options?). It returns a new array of rows, where each row is an array of up to size items.
chunkRows(items, size, { align?: 'start' | 'end', pad? }) // => Array of rows
size items, except one short row when items.length is not a multiple of size.align: 'start' (the default) fills rows from the beginning, so the short row is the last one.align: 'end' fills rows from the end, so the short row is the first one.pad key, the short row is padded to size items with that value. Padding goes at the end of the short row for 'start', and at the beginning for 'end'. The key counts even when its value is undefined or null.pad key, the short row stays short.items array returns [], even with pad.RangeError if size is not an integer number of at least 1 (for example 0, -2, 2.5, NaN, Infinity or the string '2'), or if align is set to anything other than 'start' or 'end'.items. Every row is a new array, even when there is only one row. Items themselves are not copied.chunkRows(['a', 'b', 'c', 'd', 'e'], 2);
// => [['a', 'b'], ['c', 'd'], ['e']]
chunkRows(['1', '2', '3', '4', '5', '6', '7'], 3, { align: 'end' });
// => [['1'], ['2', '3', '4'], ['5', '6', '7']]
chunkRows([1, 2, 3, 4, 5], 3, { pad: null });
// => [[1, 2, 3], [4, 5, null]]
chunkRows([1, 2, 3, 4, 5], 2, { align: 'end', pad: 0 });
// => [[0, 1], [2, 3], [4, 5]]
chunkRows([], 4, { pad: 0 });
// => []
chunkRows([1, 2], 0);
// throws RangeError