|
Last change
on this file since a762898 was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago |
|
Prototype 1.1
|
-
Property mode
set to
100644
|
|
File size:
872 bytes
|
| Line | |
|---|
| 1 | function length(array) {
|
|---|
| 2 | return array.length | 0;
|
|---|
| 3 | }
|
|---|
| 4 |
|
|---|
| 5 | function empty(length) {
|
|---|
| 6 | return !(length > 0);
|
|---|
| 7 | }
|
|---|
| 8 |
|
|---|
| 9 | function arrayify(values) {
|
|---|
| 10 | return typeof values !== "object" || "length" in values ? values : Array.from(values);
|
|---|
| 11 | }
|
|---|
| 12 |
|
|---|
| 13 | function reducer(reduce) {
|
|---|
| 14 | return values => reduce(...values);
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | export 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.