locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
24 lines (23 loc) • 871 B
JavaScript
export function chunked(values, size) {
// discuss at: https://locutus.io/kotlin/collections/chunked/
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Splits values into consecutive chunks of at most size, like Kotlin chunked.
// example 1: chunked([1, 2, 3, 4, 5], 2)
// returns 1: [[1, 2], [3, 4], [5]]
// example 2: chunked(['a', 'b', 'c'], 3)
// returns 2: [['a', 'b', 'c']]
// example 3: chunked([], 2)
// returns 3: []
if (!Array.isArray(values)) {
return [];
}
const width = Math.trunc(Number(size));
if (!Number.isFinite(width) || width <= 0) {
throw new RangeError('chunked(): size must be a positive integer');
}
const out = [];
for (let i = 0; i < values.length; i += width) {
out.push(values.slice(i, i + width));
}
return out;
}