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