source: node_modules/d3-array/src/cross.js@ e4c61dd

Last change on this file since e4c61dd was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 872 bytes
RevLine 
[e4c61dd]1function length(array) {
2 return array.length | 0;
3}
4
5function empty(length) {
6 return !(length > 0);
7}
8
9function arrayify(values) {
10 return typeof values !== "object" || "length" in values ? values : Array.from(values);
11}
12
13function reducer(reduce) {
14 return values => reduce(...values);
15}
16
17export default function cross(...values) {
18 const reduce = typeof values[values.length - 1] === "function" && reducer(values.pop());
19 values = values.map(arrayify);
20 const lengths = values.map(length);
21 const j = values.length - 1;
22 const index = new Array(j + 1).fill(0);
23 const product = [];
24 if (j < 0 || lengths.some(empty)) return product;
25 while (true) {
26 product.push(index.map((j, i) => values[i][j]));
27 let i = j;
28 while (++index[i] === lengths[i]) {
29 if (i === 0) return reduce ? product.map(reduce) : product;
30 index[i--] = 0;
31 }
32 }
33}
Note: See TracBrowser for help on using the repository browser.