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