-
Notifications
You must be signed in to change notification settings - Fork 0
/
append.ts
93 lines (83 loc) · 2.78 KB
/
append.ts
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import type { FixedPromise, IteratorReturnValue, UniversalIterable } from '@/types'
import { IterableTypeException, isAsyncIterable, isIterable, isPromise } from '@/utils'
import { concurrent as concurrentFn, isConcurrent } from './concurrent'
function* sync<A>(args: A, iterable: Iterable<A>) {
yield* iterable
yield args
}
function asyncSequential<A>(args: FixedPromise<A>, iterable: AsyncIterable<A>): AsyncIterableIterator<A> {
const iterator = iterable[Symbol.asyncIterator]()
let finished = false
return {
[Symbol.asyncIterator]() {
return this
},
async next() {
if (finished) {
return { done: true, value: undefined }
}
const { value, done } = await iterator.next()
if (finished) {
return { done: true, value: undefined }
}
if (done) {
finished = true
return { done: false, value: await args }
} else {
return { done, value }
}
}
}
}
function async<A>(args: FixedPromise<A>, iterable: AsyncIterable<A>): AsyncIterableIterator<A> {
let iterator: AsyncIterator<A>
return {
[Symbol.asyncIterator]() {
return this
},
async next(concurrent: any) {
if (iterator === undefined) {
iterator = isConcurrent(concurrent)
? asyncSequential(args, concurrentFn(concurrent.length, iterable))
: asyncSequential(args, iterable)
}
return iterator.next(concurrent)
}
}
}
/**
* Return Iterable/AsyncIterable containing the contents of the given iterable, followed by the given element.
*
* @example
* ```typescript
* const iter = append(4, [1,2,3]);
* iter.next() // {done: false, value: 1}
* iter.next() // {done: false, value: 2}
* iter.next() // {done: false, value: 3}
* iter.next() // {done: false, value: 4}
* iter.next() // {done: true, value: undefined}
* ```
*
* {@link https://github.com/niuiic/fx-flow/blob/main/packages/test/src/pipe/lazy/append.spec.ts | More examples}
*/
function append<A, B extends UniversalIterable<Awaited<A>>>(
args: A
): (iterable: B) => IteratorReturnValue<B, Awaited<A>>
function append<A>(args: A, iterable: Iterable<A>): IterableIterator<A>
function append<A>(args: A, iterable: AsyncIterable<A>): AsyncIterableIterator<A>
function append<A, B extends UniversalIterable<A>>(
args: A,
iterable?: B
): ((iterable: B) => IteratorReturnValue<B, A>) | IteratorReturnValue<B, A> {
if (iterable === undefined) {
return (iterable) => append(args, iterable as any) as IteratorReturnValue<B, A>
}
if (isAsyncIterable(iterable)) {
return async(isPromise(args) ? args : Promise.resolve(args), iterable) as IteratorReturnValue<B, A>
}
if (isIterable(iterable)) {
return sync(args, iterable) as IteratorReturnValue<B, A>
}
throw new IterableTypeException()
}
export { append }