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,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;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { iterator } from "./_internal/iterator.mjs";
|
|
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 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
|
+
export { dropWhile };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/iterator/head.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Returns the first element produced by `source`, or `undefined` if it is empty.
|
|
4
|
+
* This pulls a single element and then stops, so it is safe to use on an
|
|
5
|
+
* infinite iterator.
|
|
6
|
+
*
|
|
7
|
+
* This consumes the iterator rather than peeking at it: after reading the first
|
|
8
|
+
* element, `source` is closed via its `return` method (matching the native
|
|
9
|
+
* `Iterator.prototype.find`), so it cannot be iterated further.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type of elements produced by the iterator.
|
|
12
|
+
* @param source - The iterator to read the first element from.
|
|
13
|
+
* @returns The first element, or `undefined` when the iterator yields nothing.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* head([1, 2, 3].values()); // => 1
|
|
17
|
+
* head([].values()); // => undefined
|
|
18
|
+
*/
|
|
19
|
+
declare function head<T>(source: Iterator<T>): T | undefined;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { head };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/iterator/head.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Returns the first element produced by `source`, or `undefined` if it is empty.
|
|
4
|
+
* This pulls a single element and then stops, so it is safe to use on an
|
|
5
|
+
* infinite iterator.
|
|
6
|
+
*
|
|
7
|
+
* This consumes the iterator rather than peeking at it: after reading the first
|
|
8
|
+
* element, `source` is closed via its `return` method (matching the native
|
|
9
|
+
* `Iterator.prototype.find`), so it cannot be iterated further.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type of elements produced by the iterator.
|
|
12
|
+
* @param source - The iterator to read the first element from.
|
|
13
|
+
* @returns The first element, or `undefined` when the iterator yields nothing.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* head([1, 2, 3].values()); // => 1
|
|
17
|
+
* head([].values()); // => undefined
|
|
18
|
+
*/
|
|
19
|
+
declare function head<T>(source: Iterator<T>): T | undefined;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { head };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//#region src/iterator/head.ts
|
|
2
|
+
/**
|
|
3
|
+
* Returns the first element produced by `source`, or `undefined` if it is empty.
|
|
4
|
+
* This pulls a single element and then stops, so it is safe to use on an
|
|
5
|
+
* infinite iterator.
|
|
6
|
+
*
|
|
7
|
+
* This consumes the iterator rather than peeking at it: after reading the first
|
|
8
|
+
* element, `source` is closed via its `return` method (matching the native
|
|
9
|
+
* `Iterator.prototype.find`), so it cannot be iterated further.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type of elements produced by the iterator.
|
|
12
|
+
* @param source - The iterator to read the first element from.
|
|
13
|
+
* @returns The first element, or `undefined` when the iterator yields nothing.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* head([1, 2, 3].values()); // => 1
|
|
17
|
+
* head([].values()); // => undefined
|
|
18
|
+
*/
|
|
19
|
+
function head(source) {
|
|
20
|
+
const result = source.next();
|
|
21
|
+
if (result.done) return;
|
|
22
|
+
source.return?.();
|
|
23
|
+
return result.value;
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
26
|
+
exports.head = head;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//#region src/iterator/head.ts
|
|
2
|
+
/**
|
|
3
|
+
* Returns the first element produced by `source`, or `undefined` if it is empty.
|
|
4
|
+
* This pulls a single element and then stops, so it is safe to use on an
|
|
5
|
+
* infinite iterator.
|
|
6
|
+
*
|
|
7
|
+
* This consumes the iterator rather than peeking at it: after reading the first
|
|
8
|
+
* element, `source` is closed via its `return` method (matching the native
|
|
9
|
+
* `Iterator.prototype.find`), so it cannot be iterated further.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type of elements produced by the iterator.
|
|
12
|
+
* @param source - The iterator to read the first element from.
|
|
13
|
+
* @returns The first element, or `undefined` when the iterator yields nothing.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* head([1, 2, 3].values()); // => 1
|
|
17
|
+
* head([].values()); // => undefined
|
|
18
|
+
*/
|
|
19
|
+
function head(source) {
|
|
20
|
+
const result = source.next();
|
|
21
|
+
if (result.done) return;
|
|
22
|
+
source.return?.();
|
|
23
|
+
return result.value;
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
26
|
+
export { head };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { chunk } from "./chunk.mjs";
|
|
2
|
+
import { count } from "./count.mjs";
|
|
3
|
+
import { dropWhile } from "./dropWhile.mjs";
|
|
4
|
+
import { head } from "./head.mjs";
|
|
5
|
+
import { iterate } from "./iterate.mjs";
|
|
6
|
+
import { partition } from "./partition.mjs";
|
|
7
|
+
import { range } from "./range.mjs";
|
|
8
|
+
import { scan } from "./scan.mjs";
|
|
9
|
+
import { takeWhile } from "./takeWhile.mjs";
|
|
10
|
+
import { uniqBy } from "./uniqBy.mjs";
|
|
11
|
+
import { zip } from "./zip.mjs";
|
|
12
|
+
export { chunk, count, dropWhile, head, iterate, partition, range, scan, takeWhile, uniqBy, zip };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { chunk } from "./chunk.js";
|
|
2
|
+
import { count } from "./count.js";
|
|
3
|
+
import { dropWhile } from "./dropWhile.js";
|
|
4
|
+
import { head } from "./head.js";
|
|
5
|
+
import { iterate } from "./iterate.js";
|
|
6
|
+
import { partition } from "./partition.js";
|
|
7
|
+
import { range } from "./range.js";
|
|
8
|
+
import { scan } from "./scan.js";
|
|
9
|
+
import { takeWhile } from "./takeWhile.js";
|
|
10
|
+
import { uniqBy } from "./uniqBy.js";
|
|
11
|
+
import { zip } from "./zip.js";
|
|
12
|
+
export { chunk, count, dropWhile, head, iterate, partition, range, scan, takeWhile, uniqBy, zip };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_chunk = require("./chunk.js");
|
|
3
|
+
const require_count = require("./count.js");
|
|
4
|
+
const require_dropWhile = require("./dropWhile.js");
|
|
5
|
+
const require_head = require("./head.js");
|
|
6
|
+
const require_partition = require("./partition.js");
|
|
7
|
+
const require_scan = require("./scan.js");
|
|
8
|
+
const require_takeWhile = require("./takeWhile.js");
|
|
9
|
+
const require_uniqBy = require("./uniqBy.js");
|
|
10
|
+
const require_zip = require("./zip.js");
|
|
11
|
+
const require_iterate = require("./iterate.js");
|
|
12
|
+
const require_range = require("./range.js");
|
|
13
|
+
exports.chunk = require_chunk.chunk;
|
|
14
|
+
exports.count = require_count.count;
|
|
15
|
+
exports.dropWhile = require_dropWhile.dropWhile;
|
|
16
|
+
exports.head = require_head.head;
|
|
17
|
+
exports.iterate = require_iterate.iterate;
|
|
18
|
+
exports.partition = require_partition.partition;
|
|
19
|
+
exports.range = require_range.range;
|
|
20
|
+
exports.scan = require_scan.scan;
|
|
21
|
+
exports.takeWhile = require_takeWhile.takeWhile;
|
|
22
|
+
exports.uniqBy = require_uniqBy.uniqBy;
|
|
23
|
+
exports.zip = require_zip.zip;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { chunk } from "./chunk.mjs";
|
|
2
|
+
import { count } from "./count.mjs";
|
|
3
|
+
import { dropWhile } from "./dropWhile.mjs";
|
|
4
|
+
import { head } from "./head.mjs";
|
|
5
|
+
import { partition } from "./partition.mjs";
|
|
6
|
+
import { scan } from "./scan.mjs";
|
|
7
|
+
import { takeWhile } from "./takeWhile.mjs";
|
|
8
|
+
import { uniqBy } from "./uniqBy.mjs";
|
|
9
|
+
import { zip } from "./zip.mjs";
|
|
10
|
+
import { iterate } from "./iterate.mjs";
|
|
11
|
+
import { range } from "./range.mjs";
|
|
12
|
+
export { chunk, count, dropWhile, head, iterate, partition, range, scan, takeWhile, uniqBy, zip };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/iterator/iterate.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Creates an infinite lazy iterator that starts with `seed` and repeatedly
|
|
4
|
+
* applies `getNext` to produce the following value: `seed`, `getNext(seed)`,
|
|
5
|
+
* `getNext(getNext(seed))`, and so on.
|
|
6
|
+
*
|
|
7
|
+
* Because the iterator is infinite, it must be bounded by a short-circuiting
|
|
8
|
+
* helper such as the native `take` or {@link takeWhile} before being consumed.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The type of values produced by the iterator.
|
|
11
|
+
* @param seed - The first value of the sequence.
|
|
12
|
+
* @param getNext - Computes the next value from the current one.
|
|
13
|
+
* @returns An infinite lazy iterator over the generated sequence.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* iterate(1, x => x * 2).take(5).toArray(); // => [1, 2, 4, 8, 16]
|
|
17
|
+
*/
|
|
18
|
+
declare function iterate<T>(seed: T, getNext: (value: T) => T): IteratorObject<T, undefined>;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { iterate };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/iterator/iterate.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Creates an infinite lazy iterator that starts with `seed` and repeatedly
|
|
4
|
+
* applies `getNext` to produce the following value: `seed`, `getNext(seed)`,
|
|
5
|
+
* `getNext(getNext(seed))`, and so on.
|
|
6
|
+
*
|
|
7
|
+
* Because the iterator is infinite, it must be bounded by a short-circuiting
|
|
8
|
+
* helper such as the native `take` or {@link takeWhile} before being consumed.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The type of values produced by the iterator.
|
|
11
|
+
* @param seed - The first value of the sequence.
|
|
12
|
+
* @param getNext - Computes the next value from the current one.
|
|
13
|
+
* @returns An infinite lazy iterator over the generated sequence.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* iterate(1, x => x * 2).take(5).toArray(); // => [1, 2, 4, 8, 16]
|
|
17
|
+
*/
|
|
18
|
+
declare function iterate<T>(seed: T, getNext: (value: T) => T): IteratorObject<T, undefined>;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { iterate };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const require_iterator = require("./_internal/iterator.js");
|
|
2
|
+
//#region src/iterator/iterate.ts
|
|
3
|
+
/**
|
|
4
|
+
* Creates an infinite lazy iterator that starts with `seed` and repeatedly
|
|
5
|
+
* applies `getNext` to produce the following value: `seed`, `getNext(seed)`,
|
|
6
|
+
* `getNext(getNext(seed))`, and so on.
|
|
7
|
+
*
|
|
8
|
+
* Because the iterator is infinite, it must be bounded by a short-circuiting
|
|
9
|
+
* helper such as the native `take` or {@link takeWhile} before being consumed.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type of values produced by the iterator.
|
|
12
|
+
* @param seed - The first value of the sequence.
|
|
13
|
+
* @param getNext - Computes the next value from the current one.
|
|
14
|
+
* @returns An infinite lazy iterator over the generated sequence.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* iterate(1, x => x * 2).take(5).toArray(); // => [1, 2, 4, 8, 16]
|
|
18
|
+
*/
|
|
19
|
+
function iterate(seed, getNext) {
|
|
20
|
+
let current = seed;
|
|
21
|
+
let started = false;
|
|
22
|
+
return require_iterator.iterator(function() {
|
|
23
|
+
if (started) current = getNext(current);
|
|
24
|
+
else started = true;
|
|
25
|
+
return {
|
|
26
|
+
value: current,
|
|
27
|
+
done: false
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
exports.iterate = iterate;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { iterator } from "./_internal/iterator.mjs";
|
|
2
|
+
//#region src/iterator/iterate.ts
|
|
3
|
+
/**
|
|
4
|
+
* Creates an infinite lazy iterator that starts with `seed` and repeatedly
|
|
5
|
+
* applies `getNext` to produce the following value: `seed`, `getNext(seed)`,
|
|
6
|
+
* `getNext(getNext(seed))`, and so on.
|
|
7
|
+
*
|
|
8
|
+
* Because the iterator is infinite, it must be bounded by a short-circuiting
|
|
9
|
+
* helper such as the native `take` or {@link takeWhile} before being consumed.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type of values produced by the iterator.
|
|
12
|
+
* @param seed - The first value of the sequence.
|
|
13
|
+
* @param getNext - Computes the next value from the current one.
|
|
14
|
+
* @returns An infinite lazy iterator over the generated sequence.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* iterate(1, x => x * 2).take(5).toArray(); // => [1, 2, 4, 8, 16]
|
|
18
|
+
*/
|
|
19
|
+
function iterate(seed, getNext) {
|
|
20
|
+
let current = seed;
|
|
21
|
+
let started = false;
|
|
22
|
+
return iterator(function() {
|
|
23
|
+
if (started) current = getNext(current);
|
|
24
|
+
else started = true;
|
|
25
|
+
return {
|
|
26
|
+
value: current,
|
|
27
|
+
done: false
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
export { iterate };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/iterator/partition.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Consumes `source` and splits its elements into two arrays: the first holds the
|
|
4
|
+
* elements for which `predicate` returns truthy, the second holds the rest.
|
|
5
|
+
* Relative order is preserved within each group. This is a terminal operation:
|
|
6
|
+
* it pulls every element, so it must not be used on an infinite iterator.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The type of elements produced by the iterator.
|
|
9
|
+
* @param source - The iterator to partition.
|
|
10
|
+
* @param predicate - Called with `(value, index)`; truthy sends the element to the first array.
|
|
11
|
+
* @returns A two-element tuple of `[matched, unmatched]` arrays.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* partition([1, 2, 3, 4].values(), x => x % 2 === 0); // => [[2, 4], [1, 3]]
|
|
15
|
+
*/
|
|
16
|
+
declare function partition<T>(source: Iterator<T>, predicate: (value: T, index: number) => boolean): [T[], T[]];
|
|
17
|
+
//#endregion
|
|
18
|
+
export { partition };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/iterator/partition.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Consumes `source` and splits its elements into two arrays: the first holds the
|
|
4
|
+
* elements for which `predicate` returns truthy, the second holds the rest.
|
|
5
|
+
* Relative order is preserved within each group. This is a terminal operation:
|
|
6
|
+
* it pulls every element, so it must not be used on an infinite iterator.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The type of elements produced by the iterator.
|
|
9
|
+
* @param source - The iterator to partition.
|
|
10
|
+
* @param predicate - Called with `(value, index)`; truthy sends the element to the first array.
|
|
11
|
+
* @returns A two-element tuple of `[matched, unmatched]` arrays.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* partition([1, 2, 3, 4].values(), x => x % 2 === 0); // => [[2, 4], [1, 3]]
|
|
15
|
+
*/
|
|
16
|
+
declare function partition<T>(source: Iterator<T>, predicate: (value: T, index: number) => boolean): [T[], T[]];
|
|
17
|
+
//#endregion
|
|
18
|
+
export { partition };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
//#region src/iterator/partition.ts
|
|
2
|
+
/**
|
|
3
|
+
* Consumes `source` and splits its elements into two arrays: the first holds the
|
|
4
|
+
* elements for which `predicate` returns truthy, the second holds the rest.
|
|
5
|
+
* Relative order is preserved within each group. This is a terminal operation:
|
|
6
|
+
* it pulls every element, so it must not be used on an infinite iterator.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The type of elements produced by the iterator.
|
|
9
|
+
* @param source - The iterator to partition.
|
|
10
|
+
* @param predicate - Called with `(value, index)`; truthy sends the element to the first array.
|
|
11
|
+
* @returns A two-element tuple of `[matched, unmatched]` arrays.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* partition([1, 2, 3, 4].values(), x => x % 2 === 0); // => [[2, 4], [1, 3]]
|
|
15
|
+
*/
|
|
16
|
+
function partition(source, predicate) {
|
|
17
|
+
const matched = [];
|
|
18
|
+
const unmatched = [];
|
|
19
|
+
let index = 0;
|
|
20
|
+
let next = source.next();
|
|
21
|
+
while (!next.done) {
|
|
22
|
+
let isMatch;
|
|
23
|
+
try {
|
|
24
|
+
isMatch = predicate(next.value, index++);
|
|
25
|
+
} catch (error) {
|
|
26
|
+
source.return?.();
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
if (isMatch) matched.push(next.value);
|
|
30
|
+
else unmatched.push(next.value);
|
|
31
|
+
next = source.next();
|
|
32
|
+
}
|
|
33
|
+
return [matched, unmatched];
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
exports.partition = partition;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
//#region src/iterator/partition.ts
|
|
2
|
+
/**
|
|
3
|
+
* Consumes `source` and splits its elements into two arrays: the first holds the
|
|
4
|
+
* elements for which `predicate` returns truthy, the second holds the rest.
|
|
5
|
+
* Relative order is preserved within each group. This is a terminal operation:
|
|
6
|
+
* it pulls every element, so it must not be used on an infinite iterator.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The type of elements produced by the iterator.
|
|
9
|
+
* @param source - The iterator to partition.
|
|
10
|
+
* @param predicate - Called with `(value, index)`; truthy sends the element to the first array.
|
|
11
|
+
* @returns A two-element tuple of `[matched, unmatched]` arrays.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* partition([1, 2, 3, 4].values(), x => x % 2 === 0); // => [[2, 4], [1, 3]]
|
|
15
|
+
*/
|
|
16
|
+
function partition(source, predicate) {
|
|
17
|
+
const matched = [];
|
|
18
|
+
const unmatched = [];
|
|
19
|
+
let index = 0;
|
|
20
|
+
let next = source.next();
|
|
21
|
+
while (!next.done) {
|
|
22
|
+
let isMatch;
|
|
23
|
+
try {
|
|
24
|
+
isMatch = predicate(next.value, index++);
|
|
25
|
+
} catch (error) {
|
|
26
|
+
source.return?.();
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
if (isMatch) matched.push(next.value);
|
|
30
|
+
else unmatched.push(next.value);
|
|
31
|
+
next = source.next();
|
|
32
|
+
}
|
|
33
|
+
return [matched, unmatched];
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
export { partition };
|