es-toolkit 1.51.0-dev.2065 → 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/browser.global.js +1 -1
- package/dist/compat/function/debounce.js +1 -1
- package/dist/compat/function/debounce.mjs +1 -1
- 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/chunk.d.mts +19 -0
- package/dist/fp/iterator/chunk.d.ts +19 -0
- package/dist/fp/iterator/chunk.js +24 -0
- package/dist/fp/iterator/chunk.mjs +24 -0
- package/dist/fp/iterator/count.d.mts +20 -0
- package/dist/fp/iterator/count.d.ts +20 -0
- package/dist/fp/iterator/count.js +25 -0
- package/dist/fp/iterator/count.mjs +25 -0
- package/dist/fp/iterator/drop.d.mts +20 -0
- package/dist/fp/iterator/drop.d.ts +20 -0
- package/dist/fp/iterator/drop.js +24 -0
- package/dist/fp/iterator/drop.mjs +24 -0
- package/dist/fp/iterator/dropWhile.d.mts +18 -0
- package/dist/fp/iterator/dropWhile.d.ts +18 -0
- package/dist/fp/iterator/dropWhile.js +23 -0
- package/dist/fp/iterator/dropWhile.mjs +23 -0
- package/dist/fp/iterator/every.d.mts +20 -0
- package/dist/fp/iterator/every.d.ts +20 -0
- package/dist/fp/iterator/every.js +24 -0
- package/dist/fp/iterator/every.mjs +24 -0
- package/dist/fp/iterator/filter.d.mts +37 -0
- package/dist/fp/iterator/filter.d.ts +37 -0
- package/dist/fp/iterator/filter.js +8 -0
- package/dist/fp/iterator/filter.mjs +8 -0
- package/dist/fp/iterator/find.d.mts +20 -0
- package/dist/fp/iterator/find.d.ts +20 -0
- package/dist/fp/iterator/find.js +24 -0
- package/dist/fp/iterator/find.mjs +24 -0
- package/dist/fp/iterator/flatMap.d.mts +20 -0
- package/dist/fp/iterator/flatMap.d.ts +20 -0
- package/dist/fp/iterator/flatMap.js +24 -0
- package/dist/fp/iterator/flatMap.mjs +24 -0
- package/dist/fp/iterator/forEach.d.mts +22 -0
- package/dist/fp/iterator/forEach.d.ts +22 -0
- package/dist/fp/iterator/forEach.js +26 -0
- package/dist/fp/iterator/forEach.mjs +26 -0
- package/dist/fp/iterator/head.d.mts +19 -0
- package/dist/fp/iterator/head.d.ts +19 -0
- package/dist/fp/iterator/head.js +24 -0
- package/dist/fp/iterator/head.mjs +24 -0
- package/dist/fp/iterator/index.d.mts +22 -0
- package/dist/fp/iterator/index.d.ts +22 -0
- package/dist/fp/iterator/index.js +43 -0
- package/dist/fp/iterator/index.mjs +22 -0
- package/dist/fp/iterator/map.d.mts +20 -0
- package/dist/fp/iterator/map.d.ts +20 -0
- package/dist/fp/iterator/map.js +24 -0
- package/dist/fp/iterator/map.mjs +24 -0
- package/dist/fp/iterator/partition.d.mts +22 -0
- package/dist/fp/iterator/partition.d.ts +22 -0
- package/dist/fp/iterator/partition.js +27 -0
- package/dist/fp/iterator/partition.mjs +27 -0
- package/dist/fp/iterator/reduce.d.mts +24 -0
- package/dist/fp/iterator/reduce.d.ts +24 -0
- package/dist/fp/iterator/reduce.js +28 -0
- package/dist/fp/iterator/reduce.mjs +28 -0
- package/dist/fp/iterator/scan.d.mts +21 -0
- package/dist/fp/iterator/scan.d.ts +21 -0
- package/dist/fp/iterator/scan.js +26 -0
- package/dist/fp/iterator/scan.mjs +26 -0
- package/dist/fp/iterator/some.d.mts +20 -0
- package/dist/fp/iterator/some.d.ts +20 -0
- package/dist/fp/iterator/some.js +24 -0
- package/dist/fp/iterator/some.mjs +24 -0
- package/dist/fp/iterator/take.d.mts +21 -0
- package/dist/fp/iterator/take.d.ts +21 -0
- package/dist/fp/iterator/take.js +25 -0
- package/dist/fp/iterator/take.mjs +25 -0
- package/dist/fp/iterator/takeWhile.d.mts +19 -0
- package/dist/fp/iterator/takeWhile.d.ts +19 -0
- package/dist/fp/iterator/takeWhile.js +24 -0
- package/dist/fp/iterator/takeWhile.mjs +24 -0
- package/dist/fp/iterator/toArray.d.mts +21 -0
- package/dist/fp/iterator/toArray.d.ts +21 -0
- package/dist/fp/iterator/toArray.js +25 -0
- package/dist/fp/iterator/toArray.mjs +25 -0
- package/dist/fp/iterator/uniqBy.d.mts +20 -0
- package/dist/fp/iterator/uniqBy.d.ts +20 -0
- package/dist/fp/iterator/uniqBy.js +25 -0
- package/dist/fp/iterator/uniqBy.mjs +25 -0
- package/dist/fp/iterator/zip.d.mts +20 -0
- package/dist/fp/iterator/zip.d.ts +20 -0
- package/dist/fp/iterator/zip.js +25 -0
- package/dist/fp/iterator/zip.mjs +25 -0
- package/dist/iterator/_internal/iterator.js +74 -0
- package/dist/iterator/_internal/iterator.mjs +74 -0
- 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/chunk.d.mts +21 -0
- package/dist/iterator/chunk.d.ts +21 -0
- package/dist/iterator/chunk.js +40 -0
- package/dist/iterator/chunk.mjs +40 -0
- package/dist/iterator/count.d.mts +18 -0
- package/dist/iterator/count.d.ts +18 -0
- package/dist/iterator/count.js +26 -0
- package/dist/iterator/count.mjs +26 -0
- package/dist/iterator/dropWhile.d.mts +20 -0
- package/dist/iterator/dropWhile.d.ts +20 -0
- package/dist/iterator/dropWhile.js +52 -0
- package/dist/iterator/dropWhile.mjs +52 -0
- package/dist/iterator/head.d.mts +21 -0
- package/dist/iterator/head.d.ts +21 -0
- package/dist/iterator/head.js +26 -0
- package/dist/iterator/head.mjs +26 -0
- package/dist/iterator/index.d.mts +13 -0
- package/dist/iterator/index.d.ts +13 -0
- package/dist/iterator/index.js +25 -0
- package/dist/iterator/index.mjs +13 -0
- package/dist/iterator/iterate.d.mts +20 -0
- package/dist/iterator/iterate.d.ts +20 -0
- package/dist/iterator/iterate.js +32 -0
- package/dist/iterator/iterate.mjs +32 -0
- package/dist/iterator/partition.d.mts +18 -0
- package/dist/iterator/partition.d.ts +18 -0
- package/dist/iterator/partition.js +36 -0
- package/dist/iterator/partition.mjs +36 -0
- package/dist/iterator/range.d.mts +42 -0
- package/dist/iterator/range.d.ts +42 -0
- package/dist/iterator/range.js +25 -0
- package/dist/iterator/range.mjs +25 -0
- package/dist/iterator/scan.d.mts +23 -0
- package/dist/iterator/scan.d.ts +23 -0
- package/dist/iterator/scan.js +47 -0
- package/dist/iterator/scan.mjs +47 -0
- package/dist/iterator/takeWhile.d.mts +20 -0
- package/dist/iterator/takeWhile.d.ts +20 -0
- package/dist/iterator/takeWhile.js +34 -0
- package/dist/iterator/takeWhile.mjs +34 -0
- package/dist/iterator/uniqBy.d.mts +22 -0
- package/dist/iterator/uniqBy.d.ts +22 -0
- package/dist/iterator/uniqBy.js +43 -0
- package/dist/iterator/uniqBy.mjs +43 -0
- package/dist/iterator/zip.d.mts +23 -0
- package/dist/iterator/zip.d.ts +23 -0
- package/dist/iterator/zip.js +45 -0
- package/dist/iterator/zip.mjs +45 -0
- package/fp/iterator.d.ts +1 -0
- package/fp/iterator.js +1 -0
- package/iterator.d.ts +1 -0
- package/iterator.js +1 -0
- package/package.json +44 -1
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
//#region src/iterator/_internal/iterator.ts
|
|
2
|
+
/**
|
|
3
|
+
* Wraps a `next` function into a lazy {@link IteratorObject} whose prototype is
|
|
4
|
+
* the native `Iterator.prototype`. The result therefore behaves exactly like a
|
|
5
|
+
* value returned by a built-in iterator helper (e.g. `array.values().map(...)`):
|
|
6
|
+
* it is single-shot, it is iterable via `Symbol.iterator` (returning itself),
|
|
7
|
+
* and it carries every native helper method (`map`, `filter`, `take`, `drop`,
|
|
8
|
+
* `flatMap`, `reduce`, `toArray`, ...) so results can be chained with them.
|
|
9
|
+
*
|
|
10
|
+
* The result also follows the IteratorClose protocol of the native helpers, so
|
|
11
|
+
* upstream resources (e.g. `try/finally` blocks in generator sources) are
|
|
12
|
+
* released. `onClose` runs exactly once, at whichever of these happens first:
|
|
13
|
+
* the consumer terminates early (`return()`, e.g. from `take` or a `for...of`
|
|
14
|
+
* `break`), `next` throws, or `next` reports done. After the iterator is
|
|
15
|
+
* closed, `next` is never called again and every subsequent step is done.
|
|
16
|
+
*
|
|
17
|
+
* Using a hand-rolled `next` rather than a generator function is a deliberate
|
|
18
|
+
* performance choice — driving the iterator protocol directly measured roughly
|
|
19
|
+
* twice as fast as `yield`-based generators for the same transforms, while
|
|
20
|
+
* `Object.create(Iterator.prototype)` adds no measurable overhead over a plain
|
|
21
|
+
* object literal.
|
|
22
|
+
*
|
|
23
|
+
* @template T - The type of values produced by the iterator.
|
|
24
|
+
* @param next - Called to produce each step; return `{ done: true, value: undefined }` to finish.
|
|
25
|
+
* @param onClose - Called once when the iterator closes; use it to close upstream iterators.
|
|
26
|
+
* @returns A lazy {@link IteratorObject} that yields the values produced by `next`.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* function repeat<T>(value: T, times: number): IteratorObject<T, undefined> {
|
|
30
|
+
* let remaining = times;
|
|
31
|
+
* return iterator(function () {
|
|
32
|
+
* if (remaining <= 0) {
|
|
33
|
+
* return { value: undefined, done: true };
|
|
34
|
+
* }
|
|
35
|
+
* remaining--;
|
|
36
|
+
* return { value, done: false };
|
|
37
|
+
* });
|
|
38
|
+
* }
|
|
39
|
+
*/
|
|
40
|
+
function iterator(next, onClose) {
|
|
41
|
+
let closed = false;
|
|
42
|
+
const close = () => {
|
|
43
|
+
if (!closed) {
|
|
44
|
+
closed = true;
|
|
45
|
+
onClose?.();
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const result = Object.create(Iterator.prototype);
|
|
49
|
+
result.next = function() {
|
|
50
|
+
if (closed) return {
|
|
51
|
+
value: void 0,
|
|
52
|
+
done: true
|
|
53
|
+
};
|
|
54
|
+
let step;
|
|
55
|
+
try {
|
|
56
|
+
step = next();
|
|
57
|
+
} catch (error) {
|
|
58
|
+
close();
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
if (step.done) close();
|
|
62
|
+
return step;
|
|
63
|
+
};
|
|
64
|
+
result.return = function() {
|
|
65
|
+
close();
|
|
66
|
+
return {
|
|
67
|
+
value: void 0,
|
|
68
|
+
done: true
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
//#endregion
|
|
74
|
+
export { iterator };
|
|
@@ -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 };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/iterator/chunk.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Lazily groups the elements of `source` into arrays of length `size`. The final
|
|
4
|
+
* chunk holds the remaining elements when the source length is not an exact
|
|
5
|
+
* multiple of `size`, so it may be shorter.
|
|
6
|
+
*
|
|
7
|
+
* Each chunk is produced only when requested, so this works with infinite
|
|
8
|
+
* iterators when bounded by a short-circuiting helper.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The type of elements produced by the iterator.
|
|
11
|
+
* @param source - The iterator to split into chunks.
|
|
12
|
+
* @param size - The length of each chunk; must be an integer greater than zero.
|
|
13
|
+
* @returns A lazy iterator over arrays of up to `size` elements.
|
|
14
|
+
* @throws {Error} Throws an error if `size` is not an integer greater than zero.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* chunk([1, 2, 3, 4, 5].values(), 2).toArray(); // => [[1, 2], [3, 4], [5]]
|
|
18
|
+
*/
|
|
19
|
+
declare function chunk<T>(source: Iterator<T>, size: number): IteratorObject<T[], undefined>;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { chunk };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/iterator/chunk.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Lazily groups the elements of `source` into arrays of length `size`. The final
|
|
4
|
+
* chunk holds the remaining elements when the source length is not an exact
|
|
5
|
+
* multiple of `size`, so it may be shorter.
|
|
6
|
+
*
|
|
7
|
+
* Each chunk is produced only when requested, so this works with infinite
|
|
8
|
+
* iterators when bounded by a short-circuiting helper.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The type of elements produced by the iterator.
|
|
11
|
+
* @param source - The iterator to split into chunks.
|
|
12
|
+
* @param size - The length of each chunk; must be an integer greater than zero.
|
|
13
|
+
* @returns A lazy iterator over arrays of up to `size` elements.
|
|
14
|
+
* @throws {Error} Throws an error if `size` is not an integer greater than zero.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* chunk([1, 2, 3, 4, 5].values(), 2).toArray(); // => [[1, 2], [3, 4], [5]]
|
|
18
|
+
*/
|
|
19
|
+
declare function chunk<T>(source: Iterator<T>, size: number): IteratorObject<T[], undefined>;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { chunk };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const require_iterator = require("./_internal/iterator.js");
|
|
2
|
+
//#region src/iterator/chunk.ts
|
|
3
|
+
/**
|
|
4
|
+
* Lazily groups the elements of `source` into arrays of length `size`. The final
|
|
5
|
+
* chunk holds the remaining elements when the source length is not an exact
|
|
6
|
+
* multiple of `size`, so it may be shorter.
|
|
7
|
+
*
|
|
8
|
+
* Each chunk is produced only when requested, so this works with infinite
|
|
9
|
+
* iterators when bounded by a short-circuiting helper.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type of elements produced by the iterator.
|
|
12
|
+
* @param source - The iterator to split into chunks.
|
|
13
|
+
* @param size - The length of each chunk; must be an integer greater than zero.
|
|
14
|
+
* @returns A lazy iterator over arrays of up to `size` elements.
|
|
15
|
+
* @throws {Error} Throws an error if `size` is not an integer greater than zero.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* chunk([1, 2, 3, 4, 5].values(), 2).toArray(); // => [[1, 2], [3, 4], [5]]
|
|
19
|
+
*/
|
|
20
|
+
function chunk(source, size) {
|
|
21
|
+
if (!Number.isInteger(size) || size <= 0) throw new Error("Size must be an integer greater than zero.");
|
|
22
|
+
return require_iterator.iterator(function() {
|
|
23
|
+
const buffer = [];
|
|
24
|
+
while (buffer.length < size) {
|
|
25
|
+
const result = source.next();
|
|
26
|
+
if (result.done) break;
|
|
27
|
+
buffer.push(result.value);
|
|
28
|
+
}
|
|
29
|
+
if (buffer.length === 0) return {
|
|
30
|
+
value: void 0,
|
|
31
|
+
done: true
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
value: buffer,
|
|
35
|
+
done: false
|
|
36
|
+
};
|
|
37
|
+
}, () => void source.return?.());
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
exports.chunk = chunk;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { iterator } from "./_internal/iterator.mjs";
|
|
2
|
+
//#region src/iterator/chunk.ts
|
|
3
|
+
/**
|
|
4
|
+
* Lazily groups the elements of `source` into arrays of length `size`. The final
|
|
5
|
+
* chunk holds the remaining elements when the source length is not an exact
|
|
6
|
+
* multiple of `size`, so it may be shorter.
|
|
7
|
+
*
|
|
8
|
+
* Each chunk is produced only when requested, so this works with infinite
|
|
9
|
+
* iterators when bounded by a short-circuiting helper.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type of elements produced by the iterator.
|
|
12
|
+
* @param source - The iterator to split into chunks.
|
|
13
|
+
* @param size - The length of each chunk; must be an integer greater than zero.
|
|
14
|
+
* @returns A lazy iterator over arrays of up to `size` elements.
|
|
15
|
+
* @throws {Error} Throws an error if `size` is not an integer greater than zero.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* chunk([1, 2, 3, 4, 5].values(), 2).toArray(); // => [[1, 2], [3, 4], [5]]
|
|
19
|
+
*/
|
|
20
|
+
function chunk(source, size) {
|
|
21
|
+
if (!Number.isInteger(size) || size <= 0) throw new Error("Size must be an integer greater than zero.");
|
|
22
|
+
return iterator(function() {
|
|
23
|
+
const buffer = [];
|
|
24
|
+
while (buffer.length < size) {
|
|
25
|
+
const result = source.next();
|
|
26
|
+
if (result.done) break;
|
|
27
|
+
buffer.push(result.value);
|
|
28
|
+
}
|
|
29
|
+
if (buffer.length === 0) return {
|
|
30
|
+
value: void 0,
|
|
31
|
+
done: true
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
value: buffer,
|
|
35
|
+
done: false
|
|
36
|
+
};
|
|
37
|
+
}, () => void source.return?.());
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
export { chunk };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/iterator/count.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Consumes `source` and returns the number of elements it produces. This is a
|
|
4
|
+
* terminal operation: it pulls every element, so it must not be used on an
|
|
5
|
+
* infinite iterator.
|
|
6
|
+
*
|
|
7
|
+
* Unlike `source.toArray().length`, this counts without allocating an array.
|
|
8
|
+
*
|
|
9
|
+
* @template T - The type of elements produced by the iterator.
|
|
10
|
+
* @param source - The iterator to count.
|
|
11
|
+
* @returns The number of elements produced by `source`.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* count([1, 2, 3].values()); // => 3
|
|
15
|
+
*/
|
|
16
|
+
declare function count<T>(source: Iterator<T>): number;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { count };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/iterator/count.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Consumes `source` and returns the number of elements it produces. This is a
|
|
4
|
+
* terminal operation: it pulls every element, so it must not be used on an
|
|
5
|
+
* infinite iterator.
|
|
6
|
+
*
|
|
7
|
+
* Unlike `source.toArray().length`, this counts without allocating an array.
|
|
8
|
+
*
|
|
9
|
+
* @template T - The type of elements produced by the iterator.
|
|
10
|
+
* @param source - The iterator to count.
|
|
11
|
+
* @returns The number of elements produced by `source`.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* count([1, 2, 3].values()); // => 3
|
|
15
|
+
*/
|
|
16
|
+
declare function count<T>(source: Iterator<T>): number;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { count };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//#region src/iterator/count.ts
|
|
2
|
+
/**
|
|
3
|
+
* Consumes `source` and returns the number of elements it produces. This is a
|
|
4
|
+
* terminal operation: it pulls every element, so it must not be used on an
|
|
5
|
+
* infinite iterator.
|
|
6
|
+
*
|
|
7
|
+
* Unlike `source.toArray().length`, this counts without allocating an array.
|
|
8
|
+
*
|
|
9
|
+
* @template T - The type of elements produced by the iterator.
|
|
10
|
+
* @param source - The iterator to count.
|
|
11
|
+
* @returns The number of elements produced by `source`.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* count([1, 2, 3].values()); // => 3
|
|
15
|
+
*/
|
|
16
|
+
function count(source) {
|
|
17
|
+
let total = 0;
|
|
18
|
+
let next = source.next();
|
|
19
|
+
while (!next.done) {
|
|
20
|
+
total++;
|
|
21
|
+
next = source.next();
|
|
22
|
+
}
|
|
23
|
+
return total;
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
26
|
+
exports.count = count;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//#region src/iterator/count.ts
|
|
2
|
+
/**
|
|
3
|
+
* Consumes `source` and returns the number of elements it produces. This is a
|
|
4
|
+
* terminal operation: it pulls every element, so it must not be used on an
|
|
5
|
+
* infinite iterator.
|
|
6
|
+
*
|
|
7
|
+
* Unlike `source.toArray().length`, this counts without allocating an array.
|
|
8
|
+
*
|
|
9
|
+
* @template T - The type of elements produced by the iterator.
|
|
10
|
+
* @param source - The iterator to count.
|
|
11
|
+
* @returns The number of elements produced by `source`.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* count([1, 2, 3].values()); // => 3
|
|
15
|
+
*/
|
|
16
|
+
function count(source) {
|
|
17
|
+
let total = 0;
|
|
18
|
+
let next = source.next();
|
|
19
|
+
while (!next.done) {
|
|
20
|
+
total++;
|
|
21
|
+
next = source.next();
|
|
22
|
+
}
|
|
23
|
+
return total;
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
26
|
+
export { count };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/iterator/dropWhile.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Lazily skips elements from `source` as long as `shouldDrop` returns a truthy
|
|
4
|
+
* value, then yields every remaining element (including the one that first
|
|
5
|
+
* failed the predicate).
|
|
6
|
+
*
|
|
7
|
+
* The native iterator helpers offer `drop` (by count) but not a predicate-based
|
|
8
|
+
* `dropWhile`, which is why this is provided.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The type of elements produced by the iterator.
|
|
11
|
+
* @param source - The iterator to drop elements from.
|
|
12
|
+
* @param shouldDrop - Called with `(value, index)`; elements are skipped while it returns truthy.
|
|
13
|
+
* @returns A lazy iterator over the elements after the dropped leading run.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* dropWhile([1, 2, 3, 1].values(), x => x < 3).toArray(); // => [3, 1]
|
|
17
|
+
*/
|
|
18
|
+
declare function dropWhile<T>(source: Iterator<T>, shouldDrop: (value: T, index: number) => boolean): IteratorObject<T, undefined>;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { dropWhile };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/iterator/dropWhile.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Lazily skips elements from `source` as long as `shouldDrop` returns a truthy
|
|
4
|
+
* value, then yields every remaining element (including the one that first
|
|
5
|
+
* failed the predicate).
|
|
6
|
+
*
|
|
7
|
+
* The native iterator helpers offer `drop` (by count) but not a predicate-based
|
|
8
|
+
* `dropWhile`, which is why this is provided.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The type of elements produced by the iterator.
|
|
11
|
+
* @param source - The iterator to drop elements from.
|
|
12
|
+
* @param shouldDrop - Called with `(value, index)`; elements are skipped while it returns truthy.
|
|
13
|
+
* @returns A lazy iterator over the elements after the dropped leading run.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* dropWhile([1, 2, 3, 1].values(), x => x < 3).toArray(); // => [3, 1]
|
|
17
|
+
*/
|
|
18
|
+
declare function dropWhile<T>(source: Iterator<T>, shouldDrop: (value: T, index: number) => boolean): IteratorObject<T, undefined>;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { dropWhile };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
const require_iterator = require("./_internal/iterator.js");
|
|
2
|
+
//#region src/iterator/dropWhile.ts
|
|
3
|
+
/**
|
|
4
|
+
* Lazily skips elements from `source` as long as `shouldDrop` returns a truthy
|
|
5
|
+
* value, then yields every remaining element (including the one that first
|
|
6
|
+
* failed the predicate).
|
|
7
|
+
*
|
|
8
|
+
* The native iterator helpers offer `drop` (by count) but not a predicate-based
|
|
9
|
+
* `dropWhile`, which is why this is provided.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type of elements produced by the iterator.
|
|
12
|
+
* @param source - The iterator to drop elements from.
|
|
13
|
+
* @param shouldDrop - Called with `(value, index)`; elements are skipped while it returns truthy.
|
|
14
|
+
* @returns A lazy iterator over the elements after the dropped leading run.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* dropWhile([1, 2, 3, 1].values(), x => x < 3).toArray(); // => [3, 1]
|
|
18
|
+
*/
|
|
19
|
+
function dropWhile(source, shouldDrop) {
|
|
20
|
+
let index = 0;
|
|
21
|
+
let dropping = true;
|
|
22
|
+
return require_iterator.iterator(function() {
|
|
23
|
+
while (dropping) {
|
|
24
|
+
const result = source.next();
|
|
25
|
+
if (result.done) {
|
|
26
|
+
dropping = false;
|
|
27
|
+
return {
|
|
28
|
+
value: void 0,
|
|
29
|
+
done: true
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
if (!shouldDrop(result.value, index++)) {
|
|
33
|
+
dropping = false;
|
|
34
|
+
return {
|
|
35
|
+
value: result.value,
|
|
36
|
+
done: false
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const result = source.next();
|
|
41
|
+
if (result.done) return {
|
|
42
|
+
value: void 0,
|
|
43
|
+
done: true
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
value: result.value,
|
|
47
|
+
done: false
|
|
48
|
+
};
|
|
49
|
+
}, () => void source.return?.());
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
exports.dropWhile = dropWhile;
|