1 | import {composeTransitions} from './compose';
|
2 |
|
3 | import {ComposedTransition, VariantsMap} from '../application/contracts';
|
4 |
|
5 | export const permutations = <V>(variants: VariantsMap): Map<V, ComposedTransition> => {
|
6 | const options: {[variant: string]: Array<any>} = {};
|
7 |
|
8 | for (const k of Object.keys(variants)) {
|
9 | options[k] = Array.from(variants[k].values);
|
10 | }
|
11 |
|
12 | const combinations = recursivePermutations<V>(options);
|
13 |
|
14 | const tuples = combinations.map(
|
15 | variant => <[V, ComposedTransition]> [variant, composeTransitions(variants, variant)]);
|
16 |
|
17 | return new Map(tuples);
|
18 | };
|
19 |
|
20 | const recursivePermutations = <V>(options: {[key: string]: Array<any>}): Array<V> => {
|
21 | const keys = Object.keys(options);
|
22 | if (keys.length === 0) {
|
23 | return new Array<V>();
|
24 | }
|
25 |
|
26 | const state: V = <V> <any> {};
|
27 |
|
28 | const transformer = (index: number) => {
|
29 | const reducer = (p: Array<V>, c: V) => {
|
30 | state[keys[index]] = c;
|
31 |
|
32 | if (index + 1 < keys.length) {
|
33 | return p.concat(...transformer(index + 1));
|
34 | }
|
35 |
|
36 | return p.concat(Object.assign({}, state));
|
37 | }
|
38 |
|
39 | return options[keys[index]].reduce(reducer, new Array<V>());
|
40 | };
|
41 |
|
42 | return transformer(0);
|
43 | };
|