Skip to content

Commit 6edbe87

Browse files
committed
feat(util): 🎸 add basic Iterator polyfill
1 parent 6b179a1 commit 6edbe87

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/util/iterator.ts renamed to src/util/iterator/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import './polyfill';
2+
13
/**
24
* Next function which returns `undefined` or a value of type `T`.
35
* This is used in iterators that can end with an `undefined` value, which
46
* indicates the end of iteration.
5-
*
6-
* @todo Rename to `UndEndNext`.
77
*/
88
export type UndEndNext<T> = () => undefined | T;
99

src/util/iterator/polyfill.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
if (typeof Iterator === 'undefined' && typeof globalThis === 'object') {
2+
class Iterator<T> {
3+
[Symbol.iterator]() {
4+
return this;
5+
}
6+
7+
public find(predicate: (value: unknown) => boolean): T | undefined {
8+
for (const value of this as any) if (predicate(value)) return value;
9+
return;
10+
}
11+
}
12+
13+
/**
14+
* The `Iterator` global class is new, so we need to check if it exists.
15+
*/
16+
(globalThis as any).Iterator = Iterator;
17+
}

0 commit comments

Comments
 (0)