mirror of
https://github.com/nunocoracao/blowfish.git
synced 2025-06-08 16:01:53 +02:00
34 lines
872 B
JavaScript
34 lines
872 B
JavaScript
|
function length(array) {
|
||
|
return array.length | 0;
|
||
|
}
|
||
|
|
||
|
function empty(length) {
|
||
|
return !(length > 0);
|
||
|
}
|
||
|
|
||
|
function arrayify(values) {
|
||
|
return typeof values !== "object" || "length" in values ? values : Array.from(values);
|
||
|
}
|
||
|
|
||
|
function reducer(reduce) {
|
||
|
return values => reduce(...values);
|
||
|
}
|
||
|
|
||
|
export default function cross(...values) {
|
||
|
const reduce = typeof values[values.length - 1] === "function" && reducer(values.pop());
|
||
|
values = values.map(arrayify);
|
||
|
const lengths = values.map(length);
|
||
|
const j = values.length - 1;
|
||
|
const index = new Array(j + 1).fill(0);
|
||
|
const product = [];
|
||
|
if (j < 0 || lengths.some(empty)) return product;
|
||
|
while (true) {
|
||
|
product.push(index.map((j, i) => values[i][j]));
|
||
|
let i = j;
|
||
|
while (++index[i] === lengths[i]) {
|
||
|
if (i === 0) return reduce ? product.map(reduce) : product;
|
||
|
index[i--] = 0;
|
||
|
}
|
||
|
}
|
||
|
}
|