Skip to content

Instantly share code, notes, and snippets.

@bholloway
Created October 18, 2019 04:05
Show Gist options
  • Save bholloway/14e81c8fb593d2baa208645290273060 to your computer and use it in GitHub Desktop.
Save bholloway/14e81c8fb593d2baa208645290273060 to your computer and use it in GitHub Desktop.
permutations of arrays for jest test.each
/**
* Give all permutations of the values of the given arrays.
*
* Each given array implies at least on column in the output. Array elements are spread to create multiple columns.
* This may yeild variable length elements in the final output.
*/
const permute = (...arrays) =>
arrays.reduceRight(
(previous, array) =>
array.reduce(
(accumulator, value) => [
...accumulator,
...(previous.length ? previous : [[]]).map(values =>
[].concat(value).concat(values),
),
],
[],
),
[],
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment