diff --git a/src/lib/util/arrays.ts b/src/lib/util/arrays.ts index 891de8101..b8d11660f 100644 --- a/src/lib/util/arrays.ts +++ b/src/lib/util/arrays.ts @@ -1,9 +1,12 @@ import { assert } from './errors.js'; -export { chunk, chunkString }; +export { chunk, chunkString, zip, pad }; function chunk(array: T[], size: number): T[][] { - assert(array.length % size === 0, 'invalid input length'); + assert( + array.length % size === 0, + `chunk(): invalid input length, it must be a multiple of ${size}` + ); return Array.from({ length: array.length / size }, (_, i) => array.slice(size * i, size * (i + 1)) ); @@ -12,3 +15,19 @@ function chunk(array: T[], size: number): T[][] { function chunkString(str: string, size: number): string[] { return chunk([...str], size).map((c) => c.join('')); } + +function zip(a: T[], b: S[]) { + assert( + a.length <= b.length, + 'zip(): second array must be at least as long as the first array' + ); + return a.map((a, i): [T, S] => [a, b[i]!]); +} + +function pad(array: T[], size: number, value: T): T[] { + assert( + array.length <= size, + `target size ${size} should be greater or equal than the length of the array ${array.length}` + ); + return array.concat(Array.from({ length: size - array.length }, () => value)); +}