es-toolkit 1.51.0-dev.2069 → 1.51.0-dev.2070
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/fp/iterator/cartesianProduct.d.mts +23 -0
- package/dist/fp/iterator/cartesianProduct.d.ts +23 -0
- package/dist/fp/iterator/cartesianProduct.js +28 -0
- package/dist/fp/iterator/cartesianProduct.mjs +28 -0
- package/dist/fp/iterator/index.d.mts +2 -1
- package/dist/fp/iterator/index.d.ts +2 -1
- package/dist/fp/iterator/index.js +2 -0
- package/dist/fp/iterator/index.mjs +2 -1
- package/dist/iterator/cartesianProduct.d.mts +33 -0
- package/dist/iterator/cartesianProduct.d.ts +33 -0
- package/dist/iterator/cartesianProduct.js +95 -0
- package/dist/iterator/cartesianProduct.mjs +95 -0
- package/dist/iterator/index.d.mts +2 -1
- package/dist/iterator/index.d.ts +2 -1
- package/dist/iterator/index.js +2 -0
- package/dist/iterator/index.mjs +2 -1
- package/package.json +1 -1
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/fp/iterator/cartesianProduct.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Creates a function that lazily takes the Cartesian product of the piped
|
|
4
|
+
* iterator and `other`, for use with {@link pipe}. Every element of the piped
|
|
5
|
+
* iterator is paired with every element of `other`, with `other` advancing
|
|
6
|
+
* fastest. `other` is buffered into an array when iteration starts, while the
|
|
7
|
+
* piped iterator is consumed lazily, so it may be infinite.
|
|
8
|
+
*
|
|
9
|
+
* @template T - The type of elements produced by the piped iterator.
|
|
10
|
+
* @template U - The type of elements produced by `other`.
|
|
11
|
+
* @param other - The iterator to take the product with.
|
|
12
|
+
* @returns A function mapping an `Iterator<T>` to a lazy `IteratorObject<[T, U]>`.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* import { pipe } from 'es-toolkit/fp';
|
|
16
|
+
* import { cartesianProduct, toArray } from 'es-toolkit/fp/iterator';
|
|
17
|
+
*
|
|
18
|
+
* pipe([1, 2].values(), cartesianProduct(['a', 'b'].values()), toArray());
|
|
19
|
+
* // => [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
|
|
20
|
+
*/
|
|
21
|
+
declare function cartesianProduct<T, U>(other: Iterator<U>): (source: Iterator<T>) => IteratorObject<[T, U], undefined>;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { cartesianProduct };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/fp/iterator/cartesianProduct.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Creates a function that lazily takes the Cartesian product of the piped
|
|
4
|
+
* iterator and `other`, for use with {@link pipe}. Every element of the piped
|
|
5
|
+
* iterator is paired with every element of `other`, with `other` advancing
|
|
6
|
+
* fastest. `other` is buffered into an array when iteration starts, while the
|
|
7
|
+
* piped iterator is consumed lazily, so it may be infinite.
|
|
8
|
+
*
|
|
9
|
+
* @template T - The type of elements produced by the piped iterator.
|
|
10
|
+
* @template U - The type of elements produced by `other`.
|
|
11
|
+
* @param other - The iterator to take the product with.
|
|
12
|
+
* @returns A function mapping an `Iterator<T>` to a lazy `IteratorObject<[T, U]>`.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* import { pipe } from 'es-toolkit/fp';
|
|
16
|
+
* import { cartesianProduct, toArray } from 'es-toolkit/fp/iterator';
|
|
17
|
+
*
|
|
18
|
+
* pipe([1, 2].values(), cartesianProduct(['a', 'b'].values()), toArray());
|
|
19
|
+
* // => [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
|
|
20
|
+
*/
|
|
21
|
+
declare function cartesianProduct<T, U>(other: Iterator<U>): (source: Iterator<T>) => IteratorObject<[T, U], undefined>;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { cartesianProduct };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const require_cartesianProduct = require("../../iterator/cartesianProduct.js");
|
|
2
|
+
//#region src/fp/iterator/cartesianProduct.ts
|
|
3
|
+
/**
|
|
4
|
+
* Creates a function that lazily takes the Cartesian product of the piped
|
|
5
|
+
* iterator and `other`, for use with {@link pipe}. Every element of the piped
|
|
6
|
+
* iterator is paired with every element of `other`, with `other` advancing
|
|
7
|
+
* fastest. `other` is buffered into an array when iteration starts, while the
|
|
8
|
+
* piped iterator is consumed lazily, so it may be infinite.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The type of elements produced by the piped iterator.
|
|
11
|
+
* @template U - The type of elements produced by `other`.
|
|
12
|
+
* @param other - The iterator to take the product with.
|
|
13
|
+
* @returns A function mapping an `Iterator<T>` to a lazy `IteratorObject<[T, U]>`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* import { pipe } from 'es-toolkit/fp';
|
|
17
|
+
* import { cartesianProduct, toArray } from 'es-toolkit/fp/iterator';
|
|
18
|
+
*
|
|
19
|
+
* pipe([1, 2].values(), cartesianProduct(['a', 'b'].values()), toArray());
|
|
20
|
+
* // => [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
|
|
21
|
+
*/
|
|
22
|
+
function cartesianProduct(other) {
|
|
23
|
+
return function cartesianProductInIterator(source) {
|
|
24
|
+
return require_cartesianProduct.cartesianProduct(source, other);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
//#endregion
|
|
28
|
+
exports.cartesianProduct = cartesianProduct;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { cartesianProduct as cartesianProduct$1 } from "../../iterator/cartesianProduct.mjs";
|
|
2
|
+
//#region src/fp/iterator/cartesianProduct.ts
|
|
3
|
+
/**
|
|
4
|
+
* Creates a function that lazily takes the Cartesian product of the piped
|
|
5
|
+
* iterator and `other`, for use with {@link pipe}. Every element of the piped
|
|
6
|
+
* iterator is paired with every element of `other`, with `other` advancing
|
|
7
|
+
* fastest. `other` is buffered into an array when iteration starts, while the
|
|
8
|
+
* piped iterator is consumed lazily, so it may be infinite.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The type of elements produced by the piped iterator.
|
|
11
|
+
* @template U - The type of elements produced by `other`.
|
|
12
|
+
* @param other - The iterator to take the product with.
|
|
13
|
+
* @returns A function mapping an `Iterator<T>` to a lazy `IteratorObject<[T, U]>`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* import { pipe } from 'es-toolkit/fp';
|
|
17
|
+
* import { cartesianProduct, toArray } from 'es-toolkit/fp/iterator';
|
|
18
|
+
*
|
|
19
|
+
* pipe([1, 2].values(), cartesianProduct(['a', 'b'].values()), toArray());
|
|
20
|
+
* // => [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
|
|
21
|
+
*/
|
|
22
|
+
function cartesianProduct(other) {
|
|
23
|
+
return function cartesianProductInIterator(source) {
|
|
24
|
+
return cartesianProduct$1(source, other);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
//#endregion
|
|
28
|
+
export { cartesianProduct };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { cartesianProduct } from "./cartesianProduct.mjs";
|
|
1
2
|
import { chunk } from "./chunk.mjs";
|
|
2
3
|
import { count } from "./count.mjs";
|
|
3
4
|
import { drop } from "./drop.mjs";
|
|
@@ -18,4 +19,4 @@ import { takeWhile } from "./takeWhile.mjs";
|
|
|
18
19
|
import { toArray } from "./toArray.mjs";
|
|
19
20
|
import { uniqBy } from "./uniqBy.mjs";
|
|
20
21
|
import { zip } from "./zip.mjs";
|
|
21
|
-
export { chunk, count, drop, dropWhile, every, filter, find, flatMap, forEach, head, map, partition, reduce, scan, some, take, takeWhile, toArray, uniqBy, zip };
|
|
22
|
+
export { cartesianProduct, chunk, count, drop, dropWhile, every, filter, find, flatMap, forEach, head, map, partition, reduce, scan, some, take, takeWhile, toArray, uniqBy, zip };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { cartesianProduct } from "./cartesianProduct.js";
|
|
1
2
|
import { chunk } from "./chunk.js";
|
|
2
3
|
import { count } from "./count.js";
|
|
3
4
|
import { drop } from "./drop.js";
|
|
@@ -18,4 +19,4 @@ import { takeWhile } from "./takeWhile.js";
|
|
|
18
19
|
import { toArray } from "./toArray.js";
|
|
19
20
|
import { uniqBy } from "./uniqBy.js";
|
|
20
21
|
import { zip } from "./zip.js";
|
|
21
|
-
export { chunk, count, drop, dropWhile, every, filter, find, flatMap, forEach, head, map, partition, reduce, scan, some, take, takeWhile, toArray, uniqBy, zip };
|
|
22
|
+
export { cartesianProduct, chunk, count, drop, dropWhile, every, filter, find, flatMap, forEach, head, map, partition, reduce, scan, some, take, takeWhile, toArray, uniqBy, zip };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_cartesianProduct = require("./cartesianProduct.js");
|
|
2
3
|
const require_chunk = require("./chunk.js");
|
|
3
4
|
const require_count = require("./count.js");
|
|
4
5
|
const require_drop = require("./drop.js");
|
|
@@ -19,6 +20,7 @@ const require_takeWhile = require("./takeWhile.js");
|
|
|
19
20
|
const require_toArray = require("./toArray.js");
|
|
20
21
|
const require_uniqBy = require("./uniqBy.js");
|
|
21
22
|
const require_zip = require("./zip.js");
|
|
23
|
+
exports.cartesianProduct = require_cartesianProduct.cartesianProduct;
|
|
22
24
|
exports.chunk = require_chunk.chunk;
|
|
23
25
|
exports.count = require_count.count;
|
|
24
26
|
exports.drop = require_drop.drop;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { cartesianProduct } from "./cartesianProduct.mjs";
|
|
1
2
|
import { chunk } from "./chunk.mjs";
|
|
2
3
|
import { count } from "./count.mjs";
|
|
3
4
|
import { drop } from "./drop.mjs";
|
|
@@ -18,4 +19,4 @@ import { takeWhile } from "./takeWhile.mjs";
|
|
|
18
19
|
import { toArray } from "./toArray.mjs";
|
|
19
20
|
import { uniqBy } from "./uniqBy.mjs";
|
|
20
21
|
import { zip } from "./zip.mjs";
|
|
21
|
-
export { chunk, count, drop, dropWhile, every, filter, find, flatMap, forEach, head, map, partition, reduce, scan, some, take, takeWhile, toArray, uniqBy, zip };
|
|
22
|
+
export { cartesianProduct, chunk, count, drop, dropWhile, every, filter, find, flatMap, forEach, head, map, partition, reduce, scan, some, take, takeWhile, toArray, uniqBy, zip };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
//#region src/iterator/cartesianProduct.d.ts
|
|
2
|
+
type IteratorValue<T> = T extends Iterator<infer V> ? V : never;
|
|
3
|
+
/**
|
|
4
|
+
* Lazily computes the [Cartesian product](https://en.wikipedia.org/wiki/Cartesian_product)
|
|
5
|
+
* of the source iterators, yielding every possible tuple formed by picking one
|
|
6
|
+
* element from each source, in lexicographic order — the rightmost source
|
|
7
|
+
* advances fastest, like the digits of an odometer.
|
|
8
|
+
*
|
|
9
|
+
* Because every source except the first is traversed many times, those sources
|
|
10
|
+
* are buffered into arrays when iteration starts. The first source is consumed
|
|
11
|
+
* lazily, one element at a time, so it may be infinite. When iteration ends —
|
|
12
|
+
* because the first source ran out, a buffered source was empty, or the
|
|
13
|
+
* consumer terminated early — every source is closed via its `return` method.
|
|
14
|
+
*
|
|
15
|
+
* If no sources are passed, a single empty tuple is yielded, matching the
|
|
16
|
+
* array `cartesianProduct`. If any source is empty, nothing is yielded.
|
|
17
|
+
*
|
|
18
|
+
* @template T - A tuple of the source iterator types.
|
|
19
|
+
* @param sources - The iterators to take the product of.
|
|
20
|
+
* @returns A lazy iterator over tuples representing the Cartesian product.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* cartesianProduct([1, 2].values(), ['a', 'b'].values()).toArray();
|
|
24
|
+
* // => [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // The first source may be infinite; elements are produced on demand.
|
|
28
|
+
* cartesianProduct(range(0, Infinity), ['a', 'b'].values()).take(3).toArray();
|
|
29
|
+
* // => [[0, 'a'], [0, 'b'], [1, 'a']]
|
|
30
|
+
*/
|
|
31
|
+
declare function cartesianProduct<T extends Array<Iterator<unknown>>>(...sources: T): IteratorObject<{ [K in keyof T]: IteratorValue<T[K]> }, undefined>;
|
|
32
|
+
//#endregion
|
|
33
|
+
export { cartesianProduct };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
//#region src/iterator/cartesianProduct.d.ts
|
|
2
|
+
type IteratorValue<T> = T extends Iterator<infer V> ? V : never;
|
|
3
|
+
/**
|
|
4
|
+
* Lazily computes the [Cartesian product](https://en.wikipedia.org/wiki/Cartesian_product)
|
|
5
|
+
* of the source iterators, yielding every possible tuple formed by picking one
|
|
6
|
+
* element from each source, in lexicographic order — the rightmost source
|
|
7
|
+
* advances fastest, like the digits of an odometer.
|
|
8
|
+
*
|
|
9
|
+
* Because every source except the first is traversed many times, those sources
|
|
10
|
+
* are buffered into arrays when iteration starts. The first source is consumed
|
|
11
|
+
* lazily, one element at a time, so it may be infinite. When iteration ends —
|
|
12
|
+
* because the first source ran out, a buffered source was empty, or the
|
|
13
|
+
* consumer terminated early — every source is closed via its `return` method.
|
|
14
|
+
*
|
|
15
|
+
* If no sources are passed, a single empty tuple is yielded, matching the
|
|
16
|
+
* array `cartesianProduct`. If any source is empty, nothing is yielded.
|
|
17
|
+
*
|
|
18
|
+
* @template T - A tuple of the source iterator types.
|
|
19
|
+
* @param sources - The iterators to take the product of.
|
|
20
|
+
* @returns A lazy iterator over tuples representing the Cartesian product.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* cartesianProduct([1, 2].values(), ['a', 'b'].values()).toArray();
|
|
24
|
+
* // => [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // The first source may be infinite; elements are produced on demand.
|
|
28
|
+
* cartesianProduct(range(0, Infinity), ['a', 'b'].values()).take(3).toArray();
|
|
29
|
+
* // => [[0, 'a'], [0, 'b'], [1, 'a']]
|
|
30
|
+
*/
|
|
31
|
+
declare function cartesianProduct<T extends Array<Iterator<unknown>>>(...sources: T): IteratorObject<{ [K in keyof T]: IteratorValue<T[K]> }, undefined>;
|
|
32
|
+
//#endregion
|
|
33
|
+
export { cartesianProduct };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
const require_iterator = require("./_internal/iterator.js");
|
|
2
|
+
//#region src/iterator/cartesianProduct.ts
|
|
3
|
+
/**
|
|
4
|
+
* Lazily computes the [Cartesian product](https://en.wikipedia.org/wiki/Cartesian_product)
|
|
5
|
+
* of the source iterators, yielding every possible tuple formed by picking one
|
|
6
|
+
* element from each source, in lexicographic order — the rightmost source
|
|
7
|
+
* advances fastest, like the digits of an odometer.
|
|
8
|
+
*
|
|
9
|
+
* Because every source except the first is traversed many times, those sources
|
|
10
|
+
* are buffered into arrays when iteration starts. The first source is consumed
|
|
11
|
+
* lazily, one element at a time, so it may be infinite. When iteration ends —
|
|
12
|
+
* because the first source ran out, a buffered source was empty, or the
|
|
13
|
+
* consumer terminated early — every source is closed via its `return` method.
|
|
14
|
+
*
|
|
15
|
+
* If no sources are passed, a single empty tuple is yielded, matching the
|
|
16
|
+
* array `cartesianProduct`. If any source is empty, nothing is yielded.
|
|
17
|
+
*
|
|
18
|
+
* @template T - A tuple of the source iterator types.
|
|
19
|
+
* @param sources - The iterators to take the product of.
|
|
20
|
+
* @returns A lazy iterator over tuples representing the Cartesian product.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* cartesianProduct([1, 2].values(), ['a', 'b'].values()).toArray();
|
|
24
|
+
* // => [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // The first source may be infinite; elements are produced on demand.
|
|
28
|
+
* cartesianProduct(range(0, Infinity), ['a', 'b'].values()).take(3).toArray();
|
|
29
|
+
* // => [[0, 'a'], [0, 'b'], [1, 'a']]
|
|
30
|
+
*/
|
|
31
|
+
function cartesianProduct(...sources) {
|
|
32
|
+
const pools = [];
|
|
33
|
+
const indices = [];
|
|
34
|
+
let firstValue;
|
|
35
|
+
let state = "initial";
|
|
36
|
+
return require_iterator.iterator(function() {
|
|
37
|
+
if (state === "done") return {
|
|
38
|
+
value: void 0,
|
|
39
|
+
done: true
|
|
40
|
+
};
|
|
41
|
+
if (state === "initial") {
|
|
42
|
+
state = "active";
|
|
43
|
+
if (sources.length === 0) {
|
|
44
|
+
state = "done";
|
|
45
|
+
return {
|
|
46
|
+
value: [],
|
|
47
|
+
done: false
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
for (let index = 1; index < sources.length; index++) {
|
|
51
|
+
const pool = [];
|
|
52
|
+
let step = sources[index].next();
|
|
53
|
+
while (!step.done) {
|
|
54
|
+
pool.push(step.value);
|
|
55
|
+
step = sources[index].next();
|
|
56
|
+
}
|
|
57
|
+
pools.push(pool);
|
|
58
|
+
}
|
|
59
|
+
if (pools.some((pool) => pool.length === 0)) return {
|
|
60
|
+
value: void 0,
|
|
61
|
+
done: true
|
|
62
|
+
};
|
|
63
|
+
const step = sources[0].next();
|
|
64
|
+
if (step.done) return {
|
|
65
|
+
value: void 0,
|
|
66
|
+
done: true
|
|
67
|
+
};
|
|
68
|
+
firstValue = step.value;
|
|
69
|
+
for (let index = 0; index < pools.length; index++) indices.push(0);
|
|
70
|
+
}
|
|
71
|
+
const tuple = new Array(sources.length);
|
|
72
|
+
tuple[0] = firstValue;
|
|
73
|
+
for (let index = 0; index < indices.length; index++) tuple[index + 1] = pools[index][indices[index]];
|
|
74
|
+
let position = indices.length - 1;
|
|
75
|
+
while (position >= 0) {
|
|
76
|
+
indices[position]++;
|
|
77
|
+
if (indices[position] < pools[position].length) break;
|
|
78
|
+
indices[position] = 0;
|
|
79
|
+
position--;
|
|
80
|
+
}
|
|
81
|
+
if (position < 0) {
|
|
82
|
+
const step = sources[0].next();
|
|
83
|
+
if (step.done) state = "done";
|
|
84
|
+
else firstValue = step.value;
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
value: tuple,
|
|
88
|
+
done: false
|
|
89
|
+
};
|
|
90
|
+
}, () => {
|
|
91
|
+
for (const source of sources) source.return?.();
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
//#endregion
|
|
95
|
+
exports.cartesianProduct = cartesianProduct;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { iterator } from "./_internal/iterator.mjs";
|
|
2
|
+
//#region src/iterator/cartesianProduct.ts
|
|
3
|
+
/**
|
|
4
|
+
* Lazily computes the [Cartesian product](https://en.wikipedia.org/wiki/Cartesian_product)
|
|
5
|
+
* of the source iterators, yielding every possible tuple formed by picking one
|
|
6
|
+
* element from each source, in lexicographic order — the rightmost source
|
|
7
|
+
* advances fastest, like the digits of an odometer.
|
|
8
|
+
*
|
|
9
|
+
* Because every source except the first is traversed many times, those sources
|
|
10
|
+
* are buffered into arrays when iteration starts. The first source is consumed
|
|
11
|
+
* lazily, one element at a time, so it may be infinite. When iteration ends —
|
|
12
|
+
* because the first source ran out, a buffered source was empty, or the
|
|
13
|
+
* consumer terminated early — every source is closed via its `return` method.
|
|
14
|
+
*
|
|
15
|
+
* If no sources are passed, a single empty tuple is yielded, matching the
|
|
16
|
+
* array `cartesianProduct`. If any source is empty, nothing is yielded.
|
|
17
|
+
*
|
|
18
|
+
* @template T - A tuple of the source iterator types.
|
|
19
|
+
* @param sources - The iterators to take the product of.
|
|
20
|
+
* @returns A lazy iterator over tuples representing the Cartesian product.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* cartesianProduct([1, 2].values(), ['a', 'b'].values()).toArray();
|
|
24
|
+
* // => [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // The first source may be infinite; elements are produced on demand.
|
|
28
|
+
* cartesianProduct(range(0, Infinity), ['a', 'b'].values()).take(3).toArray();
|
|
29
|
+
* // => [[0, 'a'], [0, 'b'], [1, 'a']]
|
|
30
|
+
*/
|
|
31
|
+
function cartesianProduct(...sources) {
|
|
32
|
+
const pools = [];
|
|
33
|
+
const indices = [];
|
|
34
|
+
let firstValue;
|
|
35
|
+
let state = "initial";
|
|
36
|
+
return iterator(function() {
|
|
37
|
+
if (state === "done") return {
|
|
38
|
+
value: void 0,
|
|
39
|
+
done: true
|
|
40
|
+
};
|
|
41
|
+
if (state === "initial") {
|
|
42
|
+
state = "active";
|
|
43
|
+
if (sources.length === 0) {
|
|
44
|
+
state = "done";
|
|
45
|
+
return {
|
|
46
|
+
value: [],
|
|
47
|
+
done: false
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
for (let index = 1; index < sources.length; index++) {
|
|
51
|
+
const pool = [];
|
|
52
|
+
let step = sources[index].next();
|
|
53
|
+
while (!step.done) {
|
|
54
|
+
pool.push(step.value);
|
|
55
|
+
step = sources[index].next();
|
|
56
|
+
}
|
|
57
|
+
pools.push(pool);
|
|
58
|
+
}
|
|
59
|
+
if (pools.some((pool) => pool.length === 0)) return {
|
|
60
|
+
value: void 0,
|
|
61
|
+
done: true
|
|
62
|
+
};
|
|
63
|
+
const step = sources[0].next();
|
|
64
|
+
if (step.done) return {
|
|
65
|
+
value: void 0,
|
|
66
|
+
done: true
|
|
67
|
+
};
|
|
68
|
+
firstValue = step.value;
|
|
69
|
+
for (let index = 0; index < pools.length; index++) indices.push(0);
|
|
70
|
+
}
|
|
71
|
+
const tuple = new Array(sources.length);
|
|
72
|
+
tuple[0] = firstValue;
|
|
73
|
+
for (let index = 0; index < indices.length; index++) tuple[index + 1] = pools[index][indices[index]];
|
|
74
|
+
let position = indices.length - 1;
|
|
75
|
+
while (position >= 0) {
|
|
76
|
+
indices[position]++;
|
|
77
|
+
if (indices[position] < pools[position].length) break;
|
|
78
|
+
indices[position] = 0;
|
|
79
|
+
position--;
|
|
80
|
+
}
|
|
81
|
+
if (position < 0) {
|
|
82
|
+
const step = sources[0].next();
|
|
83
|
+
if (step.done) state = "done";
|
|
84
|
+
else firstValue = step.value;
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
value: tuple,
|
|
88
|
+
done: false
|
|
89
|
+
};
|
|
90
|
+
}, () => {
|
|
91
|
+
for (const source of sources) source.return?.();
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
//#endregion
|
|
95
|
+
export { cartesianProduct };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { cartesianProduct } from "./cartesianProduct.mjs";
|
|
1
2
|
import { chunk } from "./chunk.mjs";
|
|
2
3
|
import { count } from "./count.mjs";
|
|
3
4
|
import { dropWhile } from "./dropWhile.mjs";
|
|
@@ -9,4 +10,4 @@ import { scan } from "./scan.mjs";
|
|
|
9
10
|
import { takeWhile } from "./takeWhile.mjs";
|
|
10
11
|
import { uniqBy } from "./uniqBy.mjs";
|
|
11
12
|
import { zip } from "./zip.mjs";
|
|
12
|
-
export { chunk, count, dropWhile, head, iterate, partition, range, scan, takeWhile, uniqBy, zip };
|
|
13
|
+
export { cartesianProduct, chunk, count, dropWhile, head, iterate, partition, range, scan, takeWhile, uniqBy, zip };
|
package/dist/iterator/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { cartesianProduct } from "./cartesianProduct.js";
|
|
1
2
|
import { chunk } from "./chunk.js";
|
|
2
3
|
import { count } from "./count.js";
|
|
3
4
|
import { dropWhile } from "./dropWhile.js";
|
|
@@ -9,4 +10,4 @@ import { scan } from "./scan.js";
|
|
|
9
10
|
import { takeWhile } from "./takeWhile.js";
|
|
10
11
|
import { uniqBy } from "./uniqBy.js";
|
|
11
12
|
import { zip } from "./zip.js";
|
|
12
|
-
export { chunk, count, dropWhile, head, iterate, partition, range, scan, takeWhile, uniqBy, zip };
|
|
13
|
+
export { cartesianProduct, chunk, count, dropWhile, head, iterate, partition, range, scan, takeWhile, uniqBy, zip };
|
package/dist/iterator/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_cartesianProduct = require("./cartesianProduct.js");
|
|
2
3
|
const require_chunk = require("./chunk.js");
|
|
3
4
|
const require_count = require("./count.js");
|
|
4
5
|
const require_dropWhile = require("./dropWhile.js");
|
|
@@ -10,6 +11,7 @@ const require_uniqBy = require("./uniqBy.js");
|
|
|
10
11
|
const require_zip = require("./zip.js");
|
|
11
12
|
const require_iterate = require("./iterate.js");
|
|
12
13
|
const require_range = require("./range.js");
|
|
14
|
+
exports.cartesianProduct = require_cartesianProduct.cartesianProduct;
|
|
13
15
|
exports.chunk = require_chunk.chunk;
|
|
14
16
|
exports.count = require_count.count;
|
|
15
17
|
exports.dropWhile = require_dropWhile.dropWhile;
|
package/dist/iterator/index.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { cartesianProduct } from "./cartesianProduct.mjs";
|
|
1
2
|
import { chunk } from "./chunk.mjs";
|
|
2
3
|
import { count } from "./count.mjs";
|
|
3
4
|
import { dropWhile } from "./dropWhile.mjs";
|
|
@@ -9,4 +10,4 @@ import { uniqBy } from "./uniqBy.mjs";
|
|
|
9
10
|
import { zip } from "./zip.mjs";
|
|
10
11
|
import { iterate } from "./iterate.mjs";
|
|
11
12
|
import { range } from "./range.mjs";
|
|
12
|
-
export { chunk, count, dropWhile, head, iterate, partition, range, scan, takeWhile, uniqBy, zip };
|
|
13
|
+
export { cartesianProduct, chunk, count, dropWhile, head, iterate, partition, range, scan, takeWhile, uniqBy, zip };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "es-toolkit",
|
|
3
|
-
"version": "1.51.0-dev.
|
|
3
|
+
"version": "1.51.0-dev.2070+912e8324",
|
|
4
4
|
"description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
|
|
5
5
|
"homepage": "https://es-toolkit.dev",
|
|
6
6
|
"bugs": "https://github.com/toss/es-toolkit/issues",
|