Created
October 18, 2019 04:05
-
-
Save bholloway/14e81c8fb593d2baa208645290273060 to your computer and use it in GitHub Desktop.
permutations of arrays for jest test.each
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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