es-toolkit 1.51.0-dev.2065 → 1.51.0-dev.2069
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/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 +21 -0
- package/dist/fp/iterator/index.d.ts +21 -0
- package/dist/fp/iterator/index.js +41 -0
- package/dist/fp/iterator/index.mjs +21 -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/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 +12 -0
- package/dist/iterator/index.d.ts +12 -0
- package/dist/iterator/index.js +23 -0
- package/dist/iterator/index.mjs +12 -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,42 @@
|
|
|
1
|
+
//#region src/iterator/range.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Lazily yields numbers from `0` (inclusive) up to `end` (exclusive), incrementing by `1`.
|
|
4
|
+
*
|
|
5
|
+
* Unlike `es-toolkit`'s array `range`, this produces a lazy {@link IteratorObject}:
|
|
6
|
+
* no numbers are computed until the iterator is consumed, so it composes with
|
|
7
|
+
* other iterator helpers and supports early termination.
|
|
8
|
+
*
|
|
9
|
+
* @param end - The end number of the range (exclusive).
|
|
10
|
+
* @returns A lazy iterator over the numbers in the range.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* range(4).toArray(); // => [0, 1, 2, 3]
|
|
14
|
+
*/
|
|
15
|
+
declare function range(end: number): IteratorObject<number, undefined>;
|
|
16
|
+
/**
|
|
17
|
+
* Lazily yields numbers from `start` (inclusive) up to `end` (exclusive), incrementing by `1`.
|
|
18
|
+
*
|
|
19
|
+
* @param start - The starting number of the range (inclusive).
|
|
20
|
+
* @param end - The end number of the range (exclusive).
|
|
21
|
+
* @returns A lazy iterator over the numbers in the range.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* range(1, 4).toArray(); // => [1, 2, 3]
|
|
25
|
+
*/
|
|
26
|
+
declare function range(start: number, end: number): IteratorObject<number, undefined>;
|
|
27
|
+
/**
|
|
28
|
+
* Lazily yields numbers from `start` (inclusive) up to `end` (exclusive), incrementing by `step`.
|
|
29
|
+
*
|
|
30
|
+
* @param start - The starting number of the range (inclusive).
|
|
31
|
+
* @param end - The end number of the range (exclusive).
|
|
32
|
+
* @param step - The step value for the range.
|
|
33
|
+
* @returns A lazy iterator over the numbers in the range.
|
|
34
|
+
* @throws {Error} Throws an error if the step value is not a non-zero integer.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* range(0, 20, 5).toArray(); // => [0, 5, 10, 15]
|
|
38
|
+
* range(0, -4, -1).toArray(); // => [0, -1, -2, -3]
|
|
39
|
+
*/
|
|
40
|
+
declare function range(start: number, end: number, step: number): IteratorObject<number, undefined>;
|
|
41
|
+
//#endregion
|
|
42
|
+
export { range };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
//#region src/iterator/range.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Lazily yields numbers from `0` (inclusive) up to `end` (exclusive), incrementing by `1`.
|
|
4
|
+
*
|
|
5
|
+
* Unlike `es-toolkit`'s array `range`, this produces a lazy {@link IteratorObject}:
|
|
6
|
+
* no numbers are computed until the iterator is consumed, so it composes with
|
|
7
|
+
* other iterator helpers and supports early termination.
|
|
8
|
+
*
|
|
9
|
+
* @param end - The end number of the range (exclusive).
|
|
10
|
+
* @returns A lazy iterator over the numbers in the range.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* range(4).toArray(); // => [0, 1, 2, 3]
|
|
14
|
+
*/
|
|
15
|
+
declare function range(end: number): IteratorObject<number, undefined>;
|
|
16
|
+
/**
|
|
17
|
+
* Lazily yields numbers from `start` (inclusive) up to `end` (exclusive), incrementing by `1`.
|
|
18
|
+
*
|
|
19
|
+
* @param start - The starting number of the range (inclusive).
|
|
20
|
+
* @param end - The end number of the range (exclusive).
|
|
21
|
+
* @returns A lazy iterator over the numbers in the range.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* range(1, 4).toArray(); // => [1, 2, 3]
|
|
25
|
+
*/
|
|
26
|
+
declare function range(start: number, end: number): IteratorObject<number, undefined>;
|
|
27
|
+
/**
|
|
28
|
+
* Lazily yields numbers from `start` (inclusive) up to `end` (exclusive), incrementing by `step`.
|
|
29
|
+
*
|
|
30
|
+
* @param start - The starting number of the range (inclusive).
|
|
31
|
+
* @param end - The end number of the range (exclusive).
|
|
32
|
+
* @param step - The step value for the range.
|
|
33
|
+
* @returns A lazy iterator over the numbers in the range.
|
|
34
|
+
* @throws {Error} Throws an error if the step value is not a non-zero integer.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* range(0, 20, 5).toArray(); // => [0, 5, 10, 15]
|
|
38
|
+
* range(0, -4, -1).toArray(); // => [0, -1, -2, -3]
|
|
39
|
+
*/
|
|
40
|
+
declare function range(start: number, end: number, step: number): IteratorObject<number, undefined>;
|
|
41
|
+
//#endregion
|
|
42
|
+
export { range };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const require_iterator = require("./_internal/iterator.js");
|
|
2
|
+
//#region src/iterator/range.ts
|
|
3
|
+
function range(start, end, step = 1) {
|
|
4
|
+
if (end == null) {
|
|
5
|
+
end = start;
|
|
6
|
+
start = 0;
|
|
7
|
+
}
|
|
8
|
+
if (!Number.isInteger(step) || step === 0) throw new Error(`The step value must be a non-zero integer, but got ${step}.`);
|
|
9
|
+
const finalEnd = end;
|
|
10
|
+
let current = start;
|
|
11
|
+
return require_iterator.iterator(function() {
|
|
12
|
+
if (step > 0 ? current >= finalEnd : current <= finalEnd) return {
|
|
13
|
+
value: void 0,
|
|
14
|
+
done: true
|
|
15
|
+
};
|
|
16
|
+
const value = current;
|
|
17
|
+
current += step;
|
|
18
|
+
return {
|
|
19
|
+
value,
|
|
20
|
+
done: false
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
exports.range = range;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { iterator } from "./_internal/iterator.mjs";
|
|
2
|
+
//#region src/iterator/range.ts
|
|
3
|
+
function range(start, end, step = 1) {
|
|
4
|
+
if (end == null) {
|
|
5
|
+
end = start;
|
|
6
|
+
start = 0;
|
|
7
|
+
}
|
|
8
|
+
if (!Number.isInteger(step) || step === 0) throw new Error(`The step value must be a non-zero integer, but got ${step}.`);
|
|
9
|
+
const finalEnd = end;
|
|
10
|
+
let current = start;
|
|
11
|
+
return iterator(function() {
|
|
12
|
+
if (step > 0 ? current >= finalEnd : current <= finalEnd) return {
|
|
13
|
+
value: void 0,
|
|
14
|
+
done: true
|
|
15
|
+
};
|
|
16
|
+
const value = current;
|
|
17
|
+
current += step;
|
|
18
|
+
return {
|
|
19
|
+
value,
|
|
20
|
+
done: false
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
export { range };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/iterator/scan.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Lazily yields the running accumulation of `source` under `callback`, like a
|
|
4
|
+
* `reduce` that emits every intermediate result. The `initial` value is emitted
|
|
5
|
+
* first, followed by the accumulator after each element.
|
|
6
|
+
*
|
|
7
|
+
* For an input of length `n`, the output has length `n + 1`. This is the
|
|
8
|
+
* "scan-left" / prefix-scan behavior and has no native iterator-helper
|
|
9
|
+
* equivalent.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type of elements produced by `source`.
|
|
12
|
+
* @template U - The type of the accumulated value.
|
|
13
|
+
* @param source - The iterator to accumulate over.
|
|
14
|
+
* @param callback - Called with `(accumulator, value, index)`; returns the next accumulator.
|
|
15
|
+
* @param initial - The initial accumulator, emitted as the first value.
|
|
16
|
+
* @returns A lazy iterator over the initial value and each successive accumulator.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* scan([1, 2, 3].values(), (acc, x) => acc + x, 0).toArray(); // => [0, 1, 3, 6]
|
|
20
|
+
*/
|
|
21
|
+
declare function scan<T, U>(source: Iterator<T>, callback: (accumulator: U, value: T, index: number) => U, initial: U): IteratorObject<U, undefined>;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { scan };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/iterator/scan.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Lazily yields the running accumulation of `source` under `callback`, like a
|
|
4
|
+
* `reduce` that emits every intermediate result. The `initial` value is emitted
|
|
5
|
+
* first, followed by the accumulator after each element.
|
|
6
|
+
*
|
|
7
|
+
* For an input of length `n`, the output has length `n + 1`. This is the
|
|
8
|
+
* "scan-left" / prefix-scan behavior and has no native iterator-helper
|
|
9
|
+
* equivalent.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type of elements produced by `source`.
|
|
12
|
+
* @template U - The type of the accumulated value.
|
|
13
|
+
* @param source - The iterator to accumulate over.
|
|
14
|
+
* @param callback - Called with `(accumulator, value, index)`; returns the next accumulator.
|
|
15
|
+
* @param initial - The initial accumulator, emitted as the first value.
|
|
16
|
+
* @returns A lazy iterator over the initial value and each successive accumulator.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* scan([1, 2, 3].values(), (acc, x) => acc + x, 0).toArray(); // => [0, 1, 3, 6]
|
|
20
|
+
*/
|
|
21
|
+
declare function scan<T, U>(source: Iterator<T>, callback: (accumulator: U, value: T, index: number) => U, initial: U): IteratorObject<U, undefined>;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { scan };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const require_iterator = require("./_internal/iterator.js");
|
|
2
|
+
//#region src/iterator/scan.ts
|
|
3
|
+
/**
|
|
4
|
+
* Lazily yields the running accumulation of `source` under `callback`, like a
|
|
5
|
+
* `reduce` that emits every intermediate result. The `initial` value is emitted
|
|
6
|
+
* first, followed by the accumulator after each element.
|
|
7
|
+
*
|
|
8
|
+
* For an input of length `n`, the output has length `n + 1`. This is the
|
|
9
|
+
* "scan-left" / prefix-scan behavior and has no native iterator-helper
|
|
10
|
+
* equivalent.
|
|
11
|
+
*
|
|
12
|
+
* @template T - The type of elements produced by `source`.
|
|
13
|
+
* @template U - The type of the accumulated value.
|
|
14
|
+
* @param source - The iterator to accumulate over.
|
|
15
|
+
* @param callback - Called with `(accumulator, value, index)`; returns the next accumulator.
|
|
16
|
+
* @param initial - The initial accumulator, emitted as the first value.
|
|
17
|
+
* @returns A lazy iterator over the initial value and each successive accumulator.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* scan([1, 2, 3].values(), (acc, x) => acc + x, 0).toArray(); // => [0, 1, 3, 6]
|
|
21
|
+
*/
|
|
22
|
+
function scan(source, callback, initial) {
|
|
23
|
+
let accumulator = initial;
|
|
24
|
+
let index = 0;
|
|
25
|
+
let emittedInitial = false;
|
|
26
|
+
return require_iterator.iterator(function() {
|
|
27
|
+
if (!emittedInitial) {
|
|
28
|
+
emittedInitial = true;
|
|
29
|
+
return {
|
|
30
|
+
value: accumulator,
|
|
31
|
+
done: false
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
const result = source.next();
|
|
35
|
+
if (result.done) return {
|
|
36
|
+
value: void 0,
|
|
37
|
+
done: true
|
|
38
|
+
};
|
|
39
|
+
accumulator = callback(accumulator, result.value, index++);
|
|
40
|
+
return {
|
|
41
|
+
value: accumulator,
|
|
42
|
+
done: false
|
|
43
|
+
};
|
|
44
|
+
}, () => void source.return?.());
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
exports.scan = scan;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { iterator } from "./_internal/iterator.mjs";
|
|
2
|
+
//#region src/iterator/scan.ts
|
|
3
|
+
/**
|
|
4
|
+
* Lazily yields the running accumulation of `source` under `callback`, like a
|
|
5
|
+
* `reduce` that emits every intermediate result. The `initial` value is emitted
|
|
6
|
+
* first, followed by the accumulator after each element.
|
|
7
|
+
*
|
|
8
|
+
* For an input of length `n`, the output has length `n + 1`. This is the
|
|
9
|
+
* "scan-left" / prefix-scan behavior and has no native iterator-helper
|
|
10
|
+
* equivalent.
|
|
11
|
+
*
|
|
12
|
+
* @template T - The type of elements produced by `source`.
|
|
13
|
+
* @template U - The type of the accumulated value.
|
|
14
|
+
* @param source - The iterator to accumulate over.
|
|
15
|
+
* @param callback - Called with `(accumulator, value, index)`; returns the next accumulator.
|
|
16
|
+
* @param initial - The initial accumulator, emitted as the first value.
|
|
17
|
+
* @returns A lazy iterator over the initial value and each successive accumulator.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* scan([1, 2, 3].values(), (acc, x) => acc + x, 0).toArray(); // => [0, 1, 3, 6]
|
|
21
|
+
*/
|
|
22
|
+
function scan(source, callback, initial) {
|
|
23
|
+
let accumulator = initial;
|
|
24
|
+
let index = 0;
|
|
25
|
+
let emittedInitial = false;
|
|
26
|
+
return iterator(function() {
|
|
27
|
+
if (!emittedInitial) {
|
|
28
|
+
emittedInitial = true;
|
|
29
|
+
return {
|
|
30
|
+
value: accumulator,
|
|
31
|
+
done: false
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
const result = source.next();
|
|
35
|
+
if (result.done) return {
|
|
36
|
+
value: void 0,
|
|
37
|
+
done: true
|
|
38
|
+
};
|
|
39
|
+
accumulator = callback(accumulator, result.value, index++);
|
|
40
|
+
return {
|
|
41
|
+
value: accumulator,
|
|
42
|
+
done: false
|
|
43
|
+
};
|
|
44
|
+
}, () => void source.return?.());
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
export { scan };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/iterator/takeWhile.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Lazily yields elements from `source` as long as `shouldContinue` returns a
|
|
4
|
+
* truthy value, stopping at (and excluding) the first element for which it
|
|
5
|
+
* returns a falsy value. The remaining elements are never pulled from `source`.
|
|
6
|
+
*
|
|
7
|
+
* The native iterator helpers offer `take` (by count) but not a predicate-based
|
|
8
|
+
* `takeWhile`, which is why this is provided.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The type of elements produced by the iterator.
|
|
11
|
+
* @param source - The iterator to take elements from.
|
|
12
|
+
* @param shouldContinue - Called with `(value, index)`; iteration stops once it returns falsy.
|
|
13
|
+
* @returns A lazy iterator over the leading run of matching elements.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* takeWhile([1, 2, 3, 4, 1].values(), x => x < 3).toArray(); // => [1, 2]
|
|
17
|
+
*/
|
|
18
|
+
declare function takeWhile<T>(source: Iterator<T>, shouldContinue: (value: T, index: number) => boolean): IteratorObject<T, undefined>;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { takeWhile };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/iterator/takeWhile.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Lazily yields elements from `source` as long as `shouldContinue` returns a
|
|
4
|
+
* truthy value, stopping at (and excluding) the first element for which it
|
|
5
|
+
* returns a falsy value. The remaining elements are never pulled from `source`.
|
|
6
|
+
*
|
|
7
|
+
* The native iterator helpers offer `take` (by count) but not a predicate-based
|
|
8
|
+
* `takeWhile`, which is why this is provided.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The type of elements produced by the iterator.
|
|
11
|
+
* @param source - The iterator to take elements from.
|
|
12
|
+
* @param shouldContinue - Called with `(value, index)`; iteration stops once it returns falsy.
|
|
13
|
+
* @returns A lazy iterator over the leading run of matching elements.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* takeWhile([1, 2, 3, 4, 1].values(), x => x < 3).toArray(); // => [1, 2]
|
|
17
|
+
*/
|
|
18
|
+
declare function takeWhile<T>(source: Iterator<T>, shouldContinue: (value: T, index: number) => boolean): IteratorObject<T, undefined>;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { takeWhile };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
const require_iterator = require("./_internal/iterator.js");
|
|
2
|
+
//#region src/iterator/takeWhile.ts
|
|
3
|
+
/**
|
|
4
|
+
* Lazily yields elements from `source` as long as `shouldContinue` returns a
|
|
5
|
+
* truthy value, stopping at (and excluding) the first element for which it
|
|
6
|
+
* returns a falsy value. The remaining elements are never pulled from `source`.
|
|
7
|
+
*
|
|
8
|
+
* The native iterator helpers offer `take` (by count) but not a predicate-based
|
|
9
|
+
* `takeWhile`, which is why this is provided.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type of elements produced by the iterator.
|
|
12
|
+
* @param source - The iterator to take elements from.
|
|
13
|
+
* @param shouldContinue - Called with `(value, index)`; iteration stops once it returns falsy.
|
|
14
|
+
* @returns A lazy iterator over the leading run of matching elements.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* takeWhile([1, 2, 3, 4, 1].values(), x => x < 3).toArray(); // => [1, 2]
|
|
18
|
+
*/
|
|
19
|
+
function takeWhile(source, shouldContinue) {
|
|
20
|
+
let index = 0;
|
|
21
|
+
return require_iterator.iterator(function() {
|
|
22
|
+
const result = source.next();
|
|
23
|
+
if (result.done || !shouldContinue(result.value, index++)) return {
|
|
24
|
+
value: void 0,
|
|
25
|
+
done: true
|
|
26
|
+
};
|
|
27
|
+
return {
|
|
28
|
+
value: result.value,
|
|
29
|
+
done: false
|
|
30
|
+
};
|
|
31
|
+
}, () => void source.return?.());
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
exports.takeWhile = takeWhile;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { iterator } from "./_internal/iterator.mjs";
|
|
2
|
+
//#region src/iterator/takeWhile.ts
|
|
3
|
+
/**
|
|
4
|
+
* Lazily yields elements from `source` as long as `shouldContinue` returns a
|
|
5
|
+
* truthy value, stopping at (and excluding) the first element for which it
|
|
6
|
+
* returns a falsy value. The remaining elements are never pulled from `source`.
|
|
7
|
+
*
|
|
8
|
+
* The native iterator helpers offer `take` (by count) but not a predicate-based
|
|
9
|
+
* `takeWhile`, which is why this is provided.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type of elements produced by the iterator.
|
|
12
|
+
* @param source - The iterator to take elements from.
|
|
13
|
+
* @param shouldContinue - Called with `(value, index)`; iteration stops once it returns falsy.
|
|
14
|
+
* @returns A lazy iterator over the leading run of matching elements.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* takeWhile([1, 2, 3, 4, 1].values(), x => x < 3).toArray(); // => [1, 2]
|
|
18
|
+
*/
|
|
19
|
+
function takeWhile(source, shouldContinue) {
|
|
20
|
+
let index = 0;
|
|
21
|
+
return iterator(function() {
|
|
22
|
+
const result = source.next();
|
|
23
|
+
if (result.done || !shouldContinue(result.value, index++)) return {
|
|
24
|
+
value: void 0,
|
|
25
|
+
done: true
|
|
26
|
+
};
|
|
27
|
+
return {
|
|
28
|
+
value: result.value,
|
|
29
|
+
done: false
|
|
30
|
+
};
|
|
31
|
+
}, () => void source.return?.());
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
export { takeWhile };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
//#region src/iterator/uniqBy.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Lazily yields the elements of `source` whose mapped key has not been seen
|
|
4
|
+
* before, preserving the order of first occurrence. Keys are compared with
|
|
5
|
+
* SameValueZero semantics (matching `Set`).
|
|
6
|
+
*
|
|
7
|
+
* Deduplication is streaming: each element is emitted as soon as it is found to
|
|
8
|
+
* be unique, so this works with infinite iterators when bounded by a
|
|
9
|
+
* short-circuiting helper.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type of elements produced by the iterator.
|
|
12
|
+
* @template K - The type of the key used for comparison.
|
|
13
|
+
* @param source - The iterator to deduplicate.
|
|
14
|
+
* @param getKey - Maps an element to the key used to detect duplicates.
|
|
15
|
+
* @returns A lazy iterator over the elements with duplicate keys removed.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* uniqBy([1.1, 1.2, 2.3, 2.4].values(), Math.floor).toArray(); // => [1.1, 2.3]
|
|
19
|
+
*/
|
|
20
|
+
declare function uniqBy<T, K>(source: Iterator<T>, getKey: (value: T) => K): IteratorObject<T, undefined>;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { uniqBy };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
//#region src/iterator/uniqBy.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Lazily yields the elements of `source` whose mapped key has not been seen
|
|
4
|
+
* before, preserving the order of first occurrence. Keys are compared with
|
|
5
|
+
* SameValueZero semantics (matching `Set`).
|
|
6
|
+
*
|
|
7
|
+
* Deduplication is streaming: each element is emitted as soon as it is found to
|
|
8
|
+
* be unique, so this works with infinite iterators when bounded by a
|
|
9
|
+
* short-circuiting helper.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type of elements produced by the iterator.
|
|
12
|
+
* @template K - The type of the key used for comparison.
|
|
13
|
+
* @param source - The iterator to deduplicate.
|
|
14
|
+
* @param getKey - Maps an element to the key used to detect duplicates.
|
|
15
|
+
* @returns A lazy iterator over the elements with duplicate keys removed.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* uniqBy([1.1, 1.2, 2.3, 2.4].values(), Math.floor).toArray(); // => [1.1, 2.3]
|
|
19
|
+
*/
|
|
20
|
+
declare function uniqBy<T, K>(source: Iterator<T>, getKey: (value: T) => K): IteratorObject<T, undefined>;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { uniqBy };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const require_iterator = require("./_internal/iterator.js");
|
|
2
|
+
//#region src/iterator/uniqBy.ts
|
|
3
|
+
/**
|
|
4
|
+
* Lazily yields the elements of `source` whose mapped key has not been seen
|
|
5
|
+
* before, preserving the order of first occurrence. Keys are compared with
|
|
6
|
+
* SameValueZero semantics (matching `Set`).
|
|
7
|
+
*
|
|
8
|
+
* Deduplication is streaming: each element is emitted as soon as it is found to
|
|
9
|
+
* be unique, so this works with infinite iterators when bounded by a
|
|
10
|
+
* short-circuiting helper.
|
|
11
|
+
*
|
|
12
|
+
* @template T - The type of elements produced by the iterator.
|
|
13
|
+
* @template K - The type of the key used for comparison.
|
|
14
|
+
* @param source - The iterator to deduplicate.
|
|
15
|
+
* @param getKey - Maps an element to the key used to detect duplicates.
|
|
16
|
+
* @returns A lazy iterator over the elements with duplicate keys removed.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* uniqBy([1.1, 1.2, 2.3, 2.4].values(), Math.floor).toArray(); // => [1.1, 2.3]
|
|
20
|
+
*/
|
|
21
|
+
function uniqBy(source, getKey) {
|
|
22
|
+
const seen = /* @__PURE__ */ new Set();
|
|
23
|
+
return require_iterator.iterator(function() {
|
|
24
|
+
let result = source.next();
|
|
25
|
+
while (!result.done) {
|
|
26
|
+
const key = getKey(result.value);
|
|
27
|
+
if (!seen.has(key)) {
|
|
28
|
+
seen.add(key);
|
|
29
|
+
return {
|
|
30
|
+
value: result.value,
|
|
31
|
+
done: false
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
result = source.next();
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
value: void 0,
|
|
38
|
+
done: true
|
|
39
|
+
};
|
|
40
|
+
}, () => void source.return?.());
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
exports.uniqBy = uniqBy;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { iterator } from "./_internal/iterator.mjs";
|
|
2
|
+
//#region src/iterator/uniqBy.ts
|
|
3
|
+
/**
|
|
4
|
+
* Lazily yields the elements of `source` whose mapped key has not been seen
|
|
5
|
+
* before, preserving the order of first occurrence. Keys are compared with
|
|
6
|
+
* SameValueZero semantics (matching `Set`).
|
|
7
|
+
*
|
|
8
|
+
* Deduplication is streaming: each element is emitted as soon as it is found to
|
|
9
|
+
* be unique, so this works with infinite iterators when bounded by a
|
|
10
|
+
* short-circuiting helper.
|
|
11
|
+
*
|
|
12
|
+
* @template T - The type of elements produced by the iterator.
|
|
13
|
+
* @template K - The type of the key used for comparison.
|
|
14
|
+
* @param source - The iterator to deduplicate.
|
|
15
|
+
* @param getKey - Maps an element to the key used to detect duplicates.
|
|
16
|
+
* @returns A lazy iterator over the elements with duplicate keys removed.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* uniqBy([1.1, 1.2, 2.3, 2.4].values(), Math.floor).toArray(); // => [1.1, 2.3]
|
|
20
|
+
*/
|
|
21
|
+
function uniqBy(source, getKey) {
|
|
22
|
+
const seen = /* @__PURE__ */ new Set();
|
|
23
|
+
return iterator(function() {
|
|
24
|
+
let result = source.next();
|
|
25
|
+
while (!result.done) {
|
|
26
|
+
const key = getKey(result.value);
|
|
27
|
+
if (!seen.has(key)) {
|
|
28
|
+
seen.add(key);
|
|
29
|
+
return {
|
|
30
|
+
value: result.value,
|
|
31
|
+
done: false
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
result = source.next();
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
value: void 0,
|
|
38
|
+
done: true
|
|
39
|
+
};
|
|
40
|
+
}, () => void source.return?.());
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
export { uniqBy };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/iterator/zip.d.ts
|
|
2
|
+
type IteratorValue<T> = T extends Iterator<infer V> ? V : never;
|
|
3
|
+
/**
|
|
4
|
+
* Lazily combines several iterators into a single iterator of tuples, pairing
|
|
5
|
+
* the elements at matching positions. Iteration stops as soon as the **shortest**
|
|
6
|
+
* source is exhausted.
|
|
7
|
+
*
|
|
8
|
+
* Stopping at the shortest source (rather than padding to the longest, as the
|
|
9
|
+
* array `zip` does) is what makes this safe to use with infinite iterators:
|
|
10
|
+
* `zip(range(0, Infinity), names.values())` ends with `names`. When iteration
|
|
11
|
+
* ends — because a source ran out or the consumer terminated early — every
|
|
12
|
+
* source is closed via its `return` method.
|
|
13
|
+
*
|
|
14
|
+
* @template T - A tuple of the source iterator types.
|
|
15
|
+
* @param sources - The iterators to zip together.
|
|
16
|
+
* @returns A lazy iterator over tuples of the paired elements.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* zip([1, 2, 3].values(), ['a', 'b'].values()).toArray(); // => [[1, 'a'], [2, 'b']]
|
|
20
|
+
*/
|
|
21
|
+
declare function zip<T extends Array<Iterator<unknown>>>(...sources: T): IteratorObject<{ [K in keyof T]: IteratorValue<T[K]> }, undefined>;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { zip };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/iterator/zip.d.ts
|
|
2
|
+
type IteratorValue<T> = T extends Iterator<infer V> ? V : never;
|
|
3
|
+
/**
|
|
4
|
+
* Lazily combines several iterators into a single iterator of tuples, pairing
|
|
5
|
+
* the elements at matching positions. Iteration stops as soon as the **shortest**
|
|
6
|
+
* source is exhausted.
|
|
7
|
+
*
|
|
8
|
+
* Stopping at the shortest source (rather than padding to the longest, as the
|
|
9
|
+
* array `zip` does) is what makes this safe to use with infinite iterators:
|
|
10
|
+
* `zip(range(0, Infinity), names.values())` ends with `names`. When iteration
|
|
11
|
+
* ends — because a source ran out or the consumer terminated early — every
|
|
12
|
+
* source is closed via its `return` method.
|
|
13
|
+
*
|
|
14
|
+
* @template T - A tuple of the source iterator types.
|
|
15
|
+
* @param sources - The iterators to zip together.
|
|
16
|
+
* @returns A lazy iterator over tuples of the paired elements.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* zip([1, 2, 3].values(), ['a', 'b'].values()).toArray(); // => [[1, 'a'], [2, 'b']]
|
|
20
|
+
*/
|
|
21
|
+
declare function zip<T extends Array<Iterator<unknown>>>(...sources: T): IteratorObject<{ [K in keyof T]: IteratorValue<T[K]> }, undefined>;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { zip };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const require_iterator = require("./_internal/iterator.js");
|
|
2
|
+
//#region src/iterator/zip.ts
|
|
3
|
+
/**
|
|
4
|
+
* Lazily combines several iterators into a single iterator of tuples, pairing
|
|
5
|
+
* the elements at matching positions. Iteration stops as soon as the **shortest**
|
|
6
|
+
* source is exhausted.
|
|
7
|
+
*
|
|
8
|
+
* Stopping at the shortest source (rather than padding to the longest, as the
|
|
9
|
+
* array `zip` does) is what makes this safe to use with infinite iterators:
|
|
10
|
+
* `zip(range(0, Infinity), names.values())` ends with `names`. When iteration
|
|
11
|
+
* ends — because a source ran out or the consumer terminated early — every
|
|
12
|
+
* source is closed via its `return` method.
|
|
13
|
+
*
|
|
14
|
+
* @template T - A tuple of the source iterator types.
|
|
15
|
+
* @param sources - The iterators to zip together.
|
|
16
|
+
* @returns A lazy iterator over tuples of the paired elements.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* zip([1, 2, 3].values(), ['a', 'b'].values()).toArray(); // => [[1, 'a'], [2, 'b']]
|
|
20
|
+
*/
|
|
21
|
+
function zip(...sources) {
|
|
22
|
+
return require_iterator.iterator(function() {
|
|
23
|
+
if (sources.length === 0) return {
|
|
24
|
+
value: void 0,
|
|
25
|
+
done: true
|
|
26
|
+
};
|
|
27
|
+
const tuple = new Array(sources.length);
|
|
28
|
+
for (let index = 0; index < sources.length; index++) {
|
|
29
|
+
const result = sources[index].next();
|
|
30
|
+
if (result.done) return {
|
|
31
|
+
value: void 0,
|
|
32
|
+
done: true
|
|
33
|
+
};
|
|
34
|
+
tuple[index] = result.value;
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
value: tuple,
|
|
38
|
+
done: false
|
|
39
|
+
};
|
|
40
|
+
}, () => {
|
|
41
|
+
for (const source of sources) source.return?.();
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
exports.zip = zip;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { iterator } from "./_internal/iterator.mjs";
|
|
2
|
+
//#region src/iterator/zip.ts
|
|
3
|
+
/**
|
|
4
|
+
* Lazily combines several iterators into a single iterator of tuples, pairing
|
|
5
|
+
* the elements at matching positions. Iteration stops as soon as the **shortest**
|
|
6
|
+
* source is exhausted.
|
|
7
|
+
*
|
|
8
|
+
* Stopping at the shortest source (rather than padding to the longest, as the
|
|
9
|
+
* array `zip` does) is what makes this safe to use with infinite iterators:
|
|
10
|
+
* `zip(range(0, Infinity), names.values())` ends with `names`. When iteration
|
|
11
|
+
* ends — because a source ran out or the consumer terminated early — every
|
|
12
|
+
* source is closed via its `return` method.
|
|
13
|
+
*
|
|
14
|
+
* @template T - A tuple of the source iterator types.
|
|
15
|
+
* @param sources - The iterators to zip together.
|
|
16
|
+
* @returns A lazy iterator over tuples of the paired elements.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* zip([1, 2, 3].values(), ['a', 'b'].values()).toArray(); // => [[1, 'a'], [2, 'b']]
|
|
20
|
+
*/
|
|
21
|
+
function zip(...sources) {
|
|
22
|
+
return iterator(function() {
|
|
23
|
+
if (sources.length === 0) return {
|
|
24
|
+
value: void 0,
|
|
25
|
+
done: true
|
|
26
|
+
};
|
|
27
|
+
const tuple = new Array(sources.length);
|
|
28
|
+
for (let index = 0; index < sources.length; index++) {
|
|
29
|
+
const result = sources[index].next();
|
|
30
|
+
if (result.done) return {
|
|
31
|
+
value: void 0,
|
|
32
|
+
done: true
|
|
33
|
+
};
|
|
34
|
+
tuple[index] = result.value;
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
value: tuple,
|
|
38
|
+
done: false
|
|
39
|
+
};
|
|
40
|
+
}, () => {
|
|
41
|
+
for (const source of sources) source.return?.();
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
export { zip };
|
package/fp/iterator.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/fp/iterator';
|