deez-literal
Version:
Template Tag allowing for a MultiDimensional string literal. Allows passing in arrays to a template and returning an array of strings of all possible combinations.
31 lines (29 loc) • 801 B
JavaScript
function deez(strings, ...dimensions) {
return [...deezIter(strings, ...dimensions)];
}
function* deezIter(strings, ...dimensions) {
const [first, ...rest] = dimensions.map(
(d) => Array.isArray(d) ? d.slice() : [d]
);
const polyD = arrangeDimensions(first, rest);
for (const poly of polyD) {
let next = "";
for (const [idx, str] of strings.entries())
next += str + (poly[idx] ?? "");
yield next;
}
}
function* arrangeDimensions(iter, others) {
const next = others.shift();
if (!next) {
for (const part of iter)
yield [part];
return;
}
for (const part of iter)
for (const other of arrangeDimensions(next, others))
yield [part, other].flat();
return;
}
export { arrangeDimensions, deez, deezIter };
//# sourceMappingURL=index.js.map