UNPKG

3.33 kBPlain TextView Raw
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
25type Props<T> = T extends Record<string, infer V> ? Record<keyof T, V> : T
26type ArrayElement<A> = A extends readonly (infer T)[] ? T : never
27
28/**
29 * Given possible values for each prop, returns all combinations of those prop values.
30 * To generate the prop names and values from the component source see the `parsePropValues` utility
31 *
32 * @param {Object} propValues an object with the shape {propName: arrayOfPossibleValues}
33 * @returns {Array} an array of all prop combinations [{propAName: propAValue, propBName: propBValue}]
34 *
35 * @module generatePropCombinations
36 * @private
37 */
38export function generatePropCombinations<T>(propValues: Props<T>) {
39 type PropValueType = ArrayElement<Props<T>[keyof Props<T>]>
40 const propNames = Object.keys(propValues)
41 const combos: Array<Record<keyof Props<T>, PropValueType>> = []
42
43 if (!propNames.length) return combos
44
45 const numProps = propNames.length
46 for (let i = 0; i < numProps; i++) {
47 const propName = propNames[i]
48 const valuesForProp = propValues[propName as keyof Props<T>]
49
50 if (!Array.isArray(valuesForProp) || !valuesForProp.length) {
51 throw new Error(
52 `[ui-examples-loader] Please provide a non-empty array of possible values for
53 prop ${propName}. in "propValues"`
54 )
55 }
56
57 const numValues = valuesForProp.length
58 const numCombos = combos.length
59
60 for (let j = 0; j < numValues; j++) {
61 const propValue = valuesForProp[j]
62 if (numCombos > 0) {
63 for (let k = 0; k < numCombos; k++) {
64 const combo = combos[k]
65
66 // Check against the keys of the object here. `combo[propName]` could
67 // evaluate to a boolean value which will mess up this logic.
68 if (!Object.keys(combo).includes(propName)) {
69 // eslint-disable-next-line no-param-reassign
70 combo[propName as keyof Props<T>] = propValue
71 } else {
72 combos.push({ ...combo, [propName]: propValue })
73 }
74 }
75 } else {
76 //@ts-expect-error TODO: fix this
77 combos.push({ [propName]: propValue as PropValueType })
78 }
79 }
80 }
81 return combos
82}
83
84export default generatePropCombinations