* Creates an object with all permutations of the original keys. * For example, this definition: * ``` * { * a: [true, false], * b: [1, 2], * c: ['x'] * } * ``` * permutates to: * ``` * [ * { a: true, b: 1, c: 'x' }, * { a: true, b: 2, c: 'x' }, *
(object, index = 0, current = {}, results = [])
| 217 | * @param {Array} [results=[]] The resulting array of permutations. |
| 218 | */ |
| 219 | static getObjectKeyPermutations(object, index = 0, current = {}, results = []) { |
| 220 | const keys = Object.keys(object); |
| 221 | const key = keys[index]; |
| 222 | const values = object[key]; |
| 223 | |
| 224 | for (const value of values) { |
| 225 | current[key] = value; |
| 226 | const nextIndex = index + 1; |
| 227 | |
| 228 | if (nextIndex < keys.length) { |
| 229 | Utils.getObjectKeyPermutations(object, nextIndex, current, results); |
| 230 | } else { |
| 231 | const result = Object.assign({}, current); |
| 232 | results.push(result); |
| 233 | } |
| 234 | } |
| 235 | return results; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Validates parameters and throws if a parameter is invalid. |