UNPKG

@nodescript/stdlib

Version:
44 lines (43 loc) 1 kB
export const module = { version: '1.1.3', moduleName: 'Array / Cartesian', description: ` Computes a Cartesian product of a two-dimensional array. `, keywords: ['combinations'], params: { array: { schema: { type: 'array', items: { type: 'array', items: { type: 'any' }, }, }, }, }, result: { schema: { type: 'array', items: { type: 'any' }, } }, }; export const compute = params => { const { array } = params; if (array.length === 0) { return []; } let buf = array[0].map(_ => [_]); let current = []; for (const list of array.slice(1)) { for (const existing of buf) { for (const item of list) { current.push([...existing, item]); } } buf = current; current = []; } return buf; };