Skip to content

Instantly share code, notes, and snippets.

@formix
Last active August 30, 2022 09:37
Show Gist options
  • Save formix/81ae27dd85a1bc9f0384ccc7b259b0cc to your computer and use it in GitHub Desktop.
Save formix/81ae27dd85a1bc9f0384ccc7b259b0cc to your computer and use it in GitHub Desktop.
Javascript combinatorial table example
function combine(combins, tables, getSortKey = (v => v.key)) {
let validCombins = [];
for (let i =0; i < combins.length; i++) {
let c = combins[i];
let valid = true
for (const k in c) {
if (tables[k].selection === undefined) continue;
let sel = tables[k].selection;
valid &= sel === c[k];
if (!valid) break;
}
if (valid) {
validCombins.push(c);
}
}
let ret = {};
for (const tn in tables) {
let table = tables[tn];
let outMap = new Map();
for (let i = 0; i < validCombins.length; i++) {
let key = validCombins[i][tn];
outMap.set(key, table.data[key]);
}
let outTable = mapToSortedKeyValue(outMap, getSortKey);
ret[tn] = {
data: outTable,
selection: table.selection
}
}
return ret;
}
function mapToSortedKeyValue(map, getSortKey = (v => v.key)) {
let table = [];
[...map.keys()].forEach(k => {
table.push({key: k, value: map.get(k)});
});
table.sort((a, b) => {
let k1 = getSortKey(a);
let k2 = getSortKey(b);
if (k1 > k2) return 1;
if (k1 < k2) return -1;
return 0;
});
return table;
}
/// TESTS \\\
let categories = { A: "A1", B: "B2" };
let sizes = { SMA: "Small", MED: "Medium", LAR: "Large" };
let series = { al: "alpha", be: "beta", ga: "gamma", de: "delta", ep: "epsilon", "ze": "zeta", et: "eta", th: "theta" };
let mainNotes = { c: "do", d: "re", e: "mi", f: "fa", g: "sol", a: "la", b: "si" };
let existingCombinaisons = [
{ categories: 'A', sizes: 'SMA', series: 'et', mainNotes: 'e' },
{ categories: 'B', sizes: 'SMA', series: 'ep', mainNotes: 'c' },
{ categories: 'B', sizes: 'SMA', series: 'ze', mainNotes: 'c' },
{ categories: 'A', sizes: 'SMA', series: 'ep', mainNotes: 'd' },
{ categories: 'A', sizes: 'LAR', series: 'ga', mainNotes: 'b' },
{ categories: 'A', sizes: 'SMA', series: 'de', mainNotes: 'c' },
{ categories: 'B', sizes: 'MED', series: 'ze', mainNotes: 'd' },
{ categories: 'B', sizes: 'SMA', series: 'ga', mainNotes: 'g' },
{ categories: 'A', sizes: 'LAR', series: 'ze', mainNotes: 'c' },
{ categories: 'A', sizes: 'MED', series: 'et', mainNotes: 'g' },
{ categories: 'B', sizes: 'SMA', series: 'ze', mainNotes: 'a' },
{ categories: 'B', sizes: 'MED', series: 'ze', mainNotes: 'a' },
{ categories: 'A', sizes: 'SMA', series: 'be', mainNotes: 'b' },
{ categories: 'A', sizes: 'SMA', series: 'al', mainNotes: 'c' },
{ categories: 'A', sizes: 'MED', series: 'ze', mainNotes: 'b' },
{ categories: 'B', sizes: 'LAR', series: 'ga', mainNotes: 'f' },
{ categories: 'B', sizes: 'MED', series: 'th', mainNotes: 'a' },
{ categories: 'A', sizes: 'MED', series: 'ep', mainNotes: 'a' },
{ categories: 'A', sizes: 'LAR', series: 'be', mainNotes: 'd' },
{ categories: 'B', sizes: 'LAR', series: 'al', mainNotes: 'b' },
{ categories: 'A', sizes: 'LAR', series: 'et', mainNotes: 'c' },
{ categories: 'B', sizes: 'LAR', series: 'be', mainNotes: 'e' },
{ categories: 'A', sizes: 'MED', series: 'ep', mainNotes: 'c' },
{ categories: 'B', sizes: 'SMA', series: 'ga', mainNotes: 'a' },
{ categories: 'A', sizes: 'LAR', series: 'de', mainNotes: 'e' },
{ categories: 'B', sizes: 'LAR', series: 'et', mainNotes: 'f' },
{ categories: 'B', sizes: 'LAR', series: 'de', mainNotes: 'e' },
{ categories: 'A', sizes: 'SMA', series: 'de', mainNotes: 'g' },
{ categories: 'B', sizes: 'SMA', series: 'th', mainNotes: 'f' },
{ categories: 'A', sizes: 'SMA', series: 'th', mainNotes: 'g' },
{ categories: 'B', sizes: 'MED', series: 'ze', mainNotes: 'f' },
{ categories: 'B', sizes: 'LAR', series: 'et', mainNotes: 'e' },
{ categories: 'A', sizes: 'LAR', series: 'de', mainNotes: 'g' },
{ categories: 'B', sizes: 'MED', series: 'th', mainNotes: 'f' },
{ categories: 'B', sizes: 'LAR', series: 'al', mainNotes: 'e' },
{ categories: 'B', sizes: 'MED', series: 'de', mainNotes: 'f' },
{ categories: 'B', sizes: 'MED', series: 'al', mainNotes: 'e' },
{ categories: 'A', sizes: 'SMA', series: 'ga', mainNotes: 'd' },
{ categories: 'A', sizes: 'MED', series: 'th', mainNotes: 'd' },
{ categories: 'B', sizes: 'MED', series: 'et', mainNotes: 'c' },
{ categories: 'A', sizes: 'SMA', series: 'be', mainNotes: 'a' },
{ categories: 'A', sizes: 'MED', series: 'th', mainNotes: 'f' },
{ categories: 'B', sizes: 'LAR', series: 'de', mainNotes: 'b' },
{ categories: 'A', sizes: 'SMA', series: 'al', mainNotes: 'a' },
{ categories: 'B', sizes: 'LAR', series: 'ep', mainNotes: 'a' },
{ categories: 'A', sizes: 'LAR', series: 'et', mainNotes: 'b' },
{ categories: 'A', sizes: 'LAR', series: 'ze', mainNotes: 'b' },
{ categories: 'B', sizes: 'SMA', series: 'et', mainNotes: 'c' },
{ categories: 'B', sizes: 'MED', series: 'ga', mainNotes: 'a' },
{ categories: 'A', sizes: 'MED', series: 'de', mainNotes: 'e' },
]
console.log(existingCombinaisons);
const util = require('util');
let combined = combine(existingCombinaisons, {
categories: {
data: categories,
selection: "B"
},
sizes: {
data: sizes,
selection: "LAR"
},
series: {
data: series,
//selection: "be"
},
mainNotes: {
data: mainNotes,
//selection: "e"
}
});
console.log(util.inspect(combined, false, null, true));
/* Output
{
categories: { data: [ { key: 'B', value: 'B2' } ], selection: 'B' },
sizes: { data: [ { key: 'LAR', value: 'Large' } ], selection: 'LAR' },
series: {
data: [
{ key: 'al', value: 'alpha' },
{ key: 'be', value: 'beta' },
{ key: 'de', value: 'delta' },
{ key: 'ep', value: 'epsilon' },
{ key: 'et', value: 'eta' },
{ key: 'ga', value: 'gamma' }
],
selection: undefined
},
mainNotes: {
data: [
{ key: 'a', value: 'la' },
{ key: 'b', value: 'si' },
{ key: 'e', value: 'mi' },
{ key: 'f', value: 'fa' }
],
selection: undefined
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment