-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator-prototypes.js
27 lines (24 loc) · 1010 Bytes
/
iterator-prototypes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/** @type {Omit<IterableIterator<any>, 'next' | 'return' | 'throw'>} */
export const IteratorPrototype = Object.getPrototypeOf(
Object.getPrototypeOf([][Symbol.iterator]())
);
/** @type {Omit<AsyncIterableIterator<any>, 'next' | 'return' | 'throw'>} */
export const AsyncIteratorPrototype = Object.getPrototypeOf(
Object.getPrototypeOf(async function* () {}).prototype
);
/**
* @template T
* @template {Pick<IterableIterator<T>, 'next' | 'return' | 'throw'>} Iter
* @param {Partial<{[key: PropertyKey]: any}> & Pick<Iter, 'next' | 'return' | 'throw'>} iter
* @returns {Iter}
*/
export const createIterator = (iter) =>
Object.assign(Object.create(IteratorPrototype), iter);
/**
* @template T
* @template {Pick<AsyncIterableIterator<T>, 'next' | 'return' | 'throw'>} Iter
* @param {Partial<{[key: PropertyKey]: any}> & Pick<Iter, 'next' | 'return' | 'throw'>} iter
* @returns {Iter}
*/
export const createAsyncIterator = (iter) =>
Object.assign(Object.create(AsyncIteratorPrototype), iter);