forked from achingbrain/it
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (66 loc) · 1.41 KB
/
index.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict'
/**
* @template T
* @typedef {Object} Peek
* @property {() => IteratorResult<T, void>} peek
*/
/**
* @template T
* @typedef {Object} AsyncPeek
* @property {() => Promise<IteratorResult<T, void>>} peek
*/
/**
* @template T
* @typedef {Object} Push
* @property {(value:T) => void} push
*/
/**
* @template T
* @typedef {Iterable<T> & Peek<T> & Push<T> & Iterator<T>} Peekable<T>
*/
/**
* @template T
* @typedef {AsyncIterable<T> & AsyncPeek<T> & Push<T> & AsyncIterator<T>} AsyncPeekable<T>
*/
/**
* @template {Iterable<any> | AsyncIterable<any>} I
* @param {I} iterable
* @returns {I extends Iterable<infer T>
* ? Peekable<T>
* : I extends AsyncIterable<infer T>
* ? AsyncPeekable<T>
* : never
* }
*/
function peekableIterator (iterable) {
// @ts-ignore
const [iterator, symbol] = iterable[Symbol.asyncIterator]
// @ts-ignore
? [iterable[Symbol.asyncIterator](), Symbol.asyncIterator]
// @ts-ignore
: [iterable[Symbol.iterator](), Symbol.iterator]
/** @type {any[]} */
const queue = []
// @ts-ignore
return {
peek: () => {
return iterator.next()
},
push: (value) => {
queue.push(value)
},
next: () => {
if (queue.length) {
return {
done: false,
value: queue.shift()
}
}
return iterator.next()
},
[symbol] () {
return this
}
}
}
module.exports = peekableIterator