Skip to content

Instantly share code, notes, and snippets.

@dherman
Created February 11, 2020 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dherman/4d33477c256f96cc0cd67e6db8ccd23e to your computer and use it in GitHub Desktop.
Save dherman/4d33477c256f96cc0cd67e6db8ccd23e to your computer and use it in GitHub Desktop.
quick n dirty generator of 2^6 combinations
const ATTRIBUTES = ['A', 'B', 'C', 'D', 'E', 'F'];
// type attribute = 'A' | 'B' | 'C' | 'D' | 'E' | 'F'
// type value = "x" | ""
// type combo = [value]
function combos() {
return combosFrom(ATTRIBUTES, 0);
}
// ([attribute], index) -> [combo]
function combosFrom(attributes, from) {
// attribute
let attribute = attributes[from];
if (from === attributes.length - 1) {
return ['x', ''];
}
// [combo]
let rest = combosFrom(attributes, from + 1);
// [combo]
let withAttributeOn = rest.map(combo => ['x'].concat(combo));
// [combo]
let withAttributeOff = rest.map(combo => [''].concat(combo));
// [combo]
return withAttributeOn.concat(withAttributeOff);
}
// ([combo]) -> [string]
function asCSV(combos) {
return combos.map(combo => combo.join(','));
}
module.exports = function() {
asCSV(combos()).map(row => { console.log(row) });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment