1 | /*
|
2 | * The MIT License (MIT)
|
3 | *
|
4 | * Copyright (c) 2015 - present Instructure, Inc.
|
5 | *
|
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | * of this software and associated documentation files (the "Software"), to deal
|
8 | * in the Software without restriction, including without limitation the rights
|
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | * copies of the Software, and to permit persons to whom the Software is
|
11 | * furnished to do so, subject to the following conditions:
|
12 | *
|
13 | * The above copyright notice and this permission notice shall be included in all
|
14 | * copies or substantial portions of the Software.
|
15 | *
|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22 | * SOFTWARE.
|
23 | */
|
24 |
|
25 | /**
|
26 | * Given possible values for each prop, returns all combinations of those prop values.
|
27 | * To generate the prop names and values from the component source see the `parsePropValues` utility
|
28 | *
|
29 | * @param {Object} propValues an object with the shape {propName: arrayOfPossibleValues}
|
30 | * @returns {Array} an array of all prop combinations [{propAName: propAValue, propBName: propBValue}]
|
31 | *
|
32 | * @module generatePropCombinations
|
33 | * @private
|
34 | */
|
35 | export function generatePropCombinations(propValues) {
|
36 | const propNames = Object.keys(propValues);
|
37 | const combos = [];
|
38 | if (!propNames.length) return combos;
|
39 | const numProps = propNames.length;
|
40 |
|
41 | for (let i = 0; i < numProps; i++) {
|
42 | const propName = propNames[i];
|
43 | const valuesForProp = propValues[propName];
|
44 |
|
45 | if (!Array.isArray(valuesForProp) || !valuesForProp.length) {
|
46 | throw new Error(`[ui-examples-loader] Please provide a non-empty array of possible values for
|
47 | prop ${propName}. in "propValues"`);
|
48 | }
|
49 |
|
50 | const numValues = valuesForProp.length;
|
51 | const numCombos = combos.length;
|
52 |
|
53 | for (let j = 0; j < numValues; j++) {
|
54 | const propValue = valuesForProp[j];
|
55 |
|
56 | if (numCombos > 0) {
|
57 | for (let k = 0; k < numCombos; k++) {
|
58 | const combo = combos[k]; // Check against the keys of the object here. `combo[propName]` could
|
59 | // evaluate to a boolean value which will mess up this logic.
|
60 |
|
61 | if (!Object.keys(combo).includes(propName)) {
|
62 | // eslint-disable-next-line no-param-reassign
|
63 | combo[propName] = propValue;
|
64 | } else {
|
65 | combos.push({ ...combo,
|
66 | [propName]: propValue
|
67 | });
|
68 | }
|
69 | }
|
70 | } else {
|
71 | combos.push({
|
72 | [propName]: propValue
|
73 | });
|
74 | }
|
75 | }
|
76 | }
|
77 |
|
78 | return combos;
|
79 | }
|
80 | export default generatePropCombinations; |
\ | No newline at end of file |