Last active
July 15, 2016 23:19
-
-
Save nkatzman/6020ed3211085843ccf6e0a287fa91ff to your computer and use it in GitHub Desktop.
Matrix Permutation Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var clone = require('clone'); | |
function generateMatrixHelper(options, currentSettings, keysToIterate, keyIndexer, permutations) { | |
if (keysToIterate.every((optionKey) => currentSettings[optionKey])) { | |
permutations.push(currentSettings); | |
return; | |
} | |
if (keyIndexer >= keysToIterate.length) { | |
return; | |
} | |
let keyToAdd = keysToIterate[keyIndexer]; | |
keyIndexer += 1; | |
options[keyToAdd].forEach( (value) => { | |
currentSettings[keyToAdd] = value; | |
return generateMatrixHelper(options, clone(currentSettings), keysToIterate, keyIndexer, permutations); | |
}); | |
} | |
function generateMatrix(options) { | |
let thing = {}; | |
let settings = {}; | |
let perms = [] | |
let keysToIterate = Object.keys(options); | |
let keyIndexer = 0; | |
let keyToStart = keysToIterate[keyIndexer]; | |
options[keyToStart].forEach( (value) => { | |
// set { v: 1 } and then { v: 2 } | |
thing[keyToStart] = value; | |
return generateMatrixHelper(options, clone(thing), keysToIterate, 1, perms) | |
}); | |
return perms; | |
} | |
let obj = { | |
x: [1,2], | |
// y: ['a', 'b'], | |
// z: ['c', 'd'], | |
a: ['e', 'f', 'g'] | |
// b: [3] | |
} | |
console.log(generateMatrix(obj)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Outputs: