Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(utils): permutation helper #7355

Merged
merged 2 commits into from
Apr 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,23 @@ class Utils {

/**
* 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' },
* { a: false, b: 1, c: 'x' },
* { a: false, b: 2, c: 'x' }
* ]
* ```
* @param {Object} object The object to permutate.
* @param {Integer} [index=0] The current key index.
* @param {Object} [current={}] The current result entry being composed.
Expand All @@ -145,7 +162,7 @@ class Utils {
const nextIndex = index + 1;

if (nextIndex < keys.length) {
this.getObjectKeyPermutations(object, nextIndex, current, results);
Utils.getObjectKeyPermutations(object, nextIndex, current, results);
} else {
const result = Object.assign({}, current);
results.push(result);
Expand Down Expand Up @@ -178,7 +195,7 @@ class Utils {
const type = types[key];
const isOptional = !!type.o;
const param = params[key];
if (!(isOptional && param == null) && (!type.v(param))) {
if (!(isOptional && param == null) && !type.v(param)) {
throw `Invalid parameter ${key} must be of type ${type.t} but is ${typeof param}`;
}
}
Expand Down