-
Notifications
You must be signed in to change notification settings - Fork 4
/
async_array.ts
143 lines (133 loc) · 3.53 KB
/
async_array.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import * as Methods from './methods';
/**
* AsyncArray Class
*
* @class AsyncArray
* @extends {Array<T>}
* @template T
*/
export class AsyncArray<T> extends Array<T> {
/**
* Creates an instance of AsyncArray
* @param {...T[]} args
* @memberof AsyncArray
*/
constructor(...args: T[]) {
/***
* Why we need this ?
* if we pass one element array with an number as below,
* const foo = AsyncArray(...[20]);
* Based https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Syntax
* a new Array will be created with 20 empty elements, as below.
* [null, null, ...]
* In order to avoid that, we need the particular workaround
*/
if (args.length === 1 && typeof args[0] === 'number') {
super(1);
super[0] = args[0];
} else {
super(...args);
}
}
/**
* Async Every method
*
* @param {Methods.CallBackFilter<T>} cb
* @returns {Promise<boolean>}
* @memberof AsyncArray
*/
async aEvery(cb: Methods.CallBackFilter<T>): Promise<boolean> {
return Methods.aEvery<T>(this, cb);
}
/**
* Async Filter method
*
* @param {Methods.CallBackFilter<T>} cb
* @returns {Promise<T[]>}
* @memberof AsyncArray
*/
async aFilter(cb: Methods.CallBackFilter<T>): Promise<AsyncArray<T>> {
return new AsyncArray(...(await Methods.aFilter<T>(this, cb)));
}
/**
* Async find method
*
* @param {Methods.CallBackFind<T>} cb
* @returns {Promise<T | undefined>}
* @memberof AsyncArray
*/
async aFind(cb: Methods.CallBackFind<T>): Promise<T | undefined> {
return Methods.aFind<T>(this, cb);
}
/**
* Async findIndex method
*
* @param {Methods.CallBackFindIndex<T>} cb
* @returns {Promise<number>}
* @memberof AsyncArray
*/
async aFindIndex(cb: Methods.CallBackFindIndex<T>): Promise<number> {
return Methods.aFindIndex<T>(this, cb);
}
/**
* Async ForEach method
*
* @param {Methods.CallBackForEach<T>} cb
* @returns {Promise<void>}
* @memberof AsyncArray
*/
async aForEach(cb: Methods.CallBackForEach<T>): Promise<void> {
await Methods.aForEach<T>(this, cb);
}
/**
* Async Map method
*
* @template R
* @param {Methods.CallBackMap<T, R>} cb
* @returns {Promise<R[]>}
* @memberof AsyncArray
*/
async aMap<R>(cb: Methods.CallBackMap<T, R>): Promise<AsyncArray<R>> {
return new AsyncArray(...(await Methods.aMap<T, R>(this, cb)));
}
/**
* Async Reduce method
*
* @template R
* @param {Methods.CallBackReduce<T, R>} cb
* @param {R} [initialValue]
* @returns {Promise<T | R>}
* @memberof AsyncArray
*/
async aReduce<R>(
cb: Methods.CallBackReduce<T, R>,
initialValue?: R
): Promise<T | R> {
return Methods.aReduce(this, cb, initialValue);
}
/**
* Async ReduceRight method
*
* @template R
* @param {Methods.CallBackReduceRight<T, R>} cb
* @param {R} [initialValue]
* @returns {Promise<T | R>}
* @memberof AsyncArray
*/
async aReduceRight<R>(
cb: Methods.CallBackReduceRight<T, R>,
initialValue?: R
): Promise<T | R> {
return Methods.aReduceRight(this, cb, initialValue);
}
/**
* Async Some method
*
* @param {Methods.CallBackFilter<T>} cb
* @returns {Promise<boolean>}
* @memberof AsyncArray
*/
async aSome(cb: Methods.CallBackFilter<T>): Promise<boolean> {
return Methods.aSome<T>(this, cb);
}
}