-
Notifications
You must be signed in to change notification settings - Fork 5
/
prray.ts
438 lines (366 loc) · 15.5 KB
/
prray.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import { prraypromise, PrrayPromise } from './prraypromise'
import * as methods from './methods'
// TODO: thisArg
export default class Prray<T> extends Array<T> {
/**
_Compatible with [`Array.of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of) but returns a Prray instance._
The Prray.of() method creates a new Prray instance from a variable number of arguments, regardless of number or type of the arguments.
```javascript
const prr = Prray.of(1, 2, 3, 4)
```
* @param args
*/
static of<T>(...args: T[]): Prray<T> {
return Prray.from(args)
}
/**
The Prray.isArray() method determines whether the passed value is a Prray instance.
```javascript
Prray.isPrray([1, 2, 3]) // false
Prray.isPrray(new Prray(1, 2, 3)) // true
```
* @param obj
*/
static isPrray(obj: any): boolean {
return obj instanceof Prray
}
/**
_Compatible with [`Array.from`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from) but returns a Prray instance._
The Prray.from() method creates a new, shallow-copied Prray instance from an array-like or iterable object.
```javascript
const prr = Prray.from([1, 2, 3, 4])
```
* @param arrayLike
*/
static from<T>(arrayLike: Iterable<T> | ArrayLike<T>): Prray<T>
static from<T, U>(arrayLike: Iterable<T> | ArrayLike<T>, mapFunc: (v: T, ix: number) => U, thisArg?: any): Prray<U>
static from<T, U>(
arrayLike: Iterable<T> | ArrayLike<T>,
mapFunc?: (v: T, ix: number) => U,
thisArg?: any,
): Prray<any> {
const arr = mapFunc === undefined ? super.from(arrayLike) : super.from(arrayLike, mapFunc, thisArg)
const prr = new Prray()
for (let i = arr.length - 1; i >= 0; i--) {
prr[i] = arr[i]
}
return prr
}
/**
The Prray.delay() method returns a promise (`PrrayPromise` exactly) that will be resolved after given ms milliseconds.
```javascript
await Prray.delay(1000) // resolve after 1 second
const prr = Prray.from([1,2,3])
await prr
.mapAsync(action1)
.delay(500) // delay 500ms between two iterations
.forEach(action2)
```
* @param ms
*/
static delay<T>(ms: number): PrrayPromise<T> {
const prray = new Prray<T>()
return new PrrayPromise(resolve => setTimeout(() => resolve(prray), ms))
}
constructor(length: number)
constructor(...args: T[])
constructor(...args: any[]) {
super(...args)
}
/**
_Think of it as an async version of method `map`_
The mapAsync() method returns a promise (`PrrayPromise` exactly) that resolved with a new prray with the resolved results of calling a provided async function on every element in the calling prray, or rejected immediately if any of the promises reject.
The provided async function is called on every element concurrently. You may optionally specify a concurrency limit.
- `func(currentValue, index, prray)`
- options
- `concurrency` Number of concurrently pending promises returned by provided function. Default: `Infinity`
```javascript
const urls = Prray.from(urlArray)
const jsons = await urls.mapAsync(fetch).mapAsync(res => res.json())
await jsons.mapAsync(insertToDB, { concurrency: 2 })
```
* @param func
* @param opts
*/
mapAsync<U>(
func: (currentValue: T, index: number, prray: Prray<T>) => Promise<U> | U,
opts?: { concurrency: number },
): PrrayPromise<U> {
const promise = methods.mapAsync(this, func, opts)
return prraypromise(promise.then(arr => Prray.from(arr)))
}
map<U>(func: (currentValue: T, index: number, prray: Prray<T>) => U): Prray<U> {
return _ensurePrray(methods.map(this, func))
}
/**
_Think of it as an async version of method `filter`_
The filterAsync() method returns a promise (`PrrayPromise` exactly) that resolved with a new prray with all elements that pass the test implemented by the provided async function, or rejected immediately if any of the promises reject.
The provided async function is called on every element concurrently. You may optionally specify a concurrency limit.
- `func(currentValue, index, prray)`
- options
- `concurrency` Number of concurrently pending promises returned by provided function. Default: `Infinity`
```javascript
const files = Prray.from(fileArray)
await files.filterAsync(isExisted).mapAsync(removeFile)
await files.filterAsync(isExisted, { concurrency: 2 })
```
* @param func
* @param opts
*/
filterAsync(
func: (currentValue: T, index: number, prray: Prray<T>) => Promise<boolean> | boolean,
opts?: { concurrency: number },
): PrrayPromise<T> {
const promise = methods.filterAsync(this, func, opts)
return prraypromise(promise.then(arr => Prray.from(arr)))
}
filter(func: (currentValue: T, index: number, prray: Prray<T>) => boolean): Prray<T> {
return _ensurePrray(methods.filter(this, func))
}
/**
_Think of it as an async version of method `reduce`_
The reduceAsync() method executes a async reducer function (that you provide) on each element of the prray, resulting in a single output value resolved by a promise (`PrrayPromise` exactly).
```javascript
const productIds = Prray.from(idArray)
const total = await productIds.reduceAsync(async (total, id) => {
const price = await getPrice(id)
return total + price
}, 0)
```
* @param func
*/
reduceAsync(func: (accumulator: T, currentValue: T, index: number, prray: Prray<T>) => T | Promise<T>): Promise<T>
reduceAsync(
func: (accumulator: T, currentValue: T, index: number, prray: Prray<T>) => T | Promise<T>,
initialValue: T,
): Promise<T>
reduceAsync<U>(
func: (accumulator: U, currentValue: T, index: number, prray: Prray<T>) => U | Promise<U>,
initialValue: U,
): Promise<U>
reduceAsync(
func: (accumulator: any, currentValue: T, index: number, prray: Prray<T>) => any | Promise<any>,
initialValue?: any,
): Promise<any> {
const promise = methods.reduceAsync(this, func, initialValue)
return promise
}
reduce(func: (accumulator: T, currentValue: T, index: number, prray: Prray<T>) => T): T
reduce(func: (accumulator: T, currentValue: T, index: number, prray: Prray<T>) => T, initialValue: T): T
reduce<U>(func: (accumulator: U, currentValue: T, index: number, prray: Prray<T>) => U, initialValue: U): U
reduce(func: (accumulator: any, currentValue: T, index: number, prray: Prray<T>) => any, initialValue?: any): any {
return methods.reduce(this, func, initialValue)
}
/**
_Think of it as an async version of method `reduceRight`_
The reduceRightAsync() method applies an async function against an accumulator and each value of the prray (from right-to-left) to reduce it to a single value.
```javascript
const productIds = Prray.from(idArray)
const total = await productIds.reduceRightAsync(async (total, id) => {
const price = await getPrice(id)
return total + price
}, 0)
```
* @param func
*/
reduceRightAsync(
func: (accumulator: T, currentValue: T, index: number, prray: Prray<T>) => T | Promise<T>,
): Promise<T>
reduceRightAsync(
func: (accumulator: T, currentValue: T, index: number, prray: Prray<T>) => T | Promise<T>,
initialValue: T,
): Promise<T>
reduceRightAsync<U>(
func: (accumulator: U, currentValue: T, index: number, prray: Prray<T>) => U | Promise<U>,
initialValue: U,
): Promise<U>
reduceRightAsync(
func: (accumulator: any, currentValue: T, index: number, prray: Prray<T>) => any | Promise<any>,
initialValue?: any,
): Promise<any> {
const promise = methods.reduceRightAsync(this, func, initialValue)
return promise
}
reduceRight(func: (accumulator: T, currentValue: T, index: number, prray: Prray<T>) => T): T
reduceRight(func: (accumulator: T, currentValue: T, index: number, prray: Prray<T>) => T, initialValue: T): T
reduceRight<U>(func: (accumulator: U, currentValue: T, index: number, prray: Prray<T>) => U, initialValue: U): U
reduceRight(
func: (accumulator: any, currentValue: T, index: number, prray: Prray<T>) => any,
initialValue?: any,
): any {
return methods.reduceRight(this, func, initialValue)
}
/**
_Think of it as an async version of method `sort`_
The sortAsync() method sorts the elements of a prray in place and returns a promise (`PrrayPromise` exactly) resolved with the sorted prray. The provided function can be an async function that returns a promise resolved with a number.
```javascript
const students = Prray.from(idArray)
const rank = await students.sortAsync((a, b) => {
const scoreA = await getScore(a)
const scoreB = await getScore(b)
return scoreA - scoreB
})
```
* @param func
*/
sortAsync(func?: (a: T, b: T) => Promise<number> | number): PrrayPromise<T> {
const promise = methods.sortAsync(this, func)
return prraypromise(promise)
}
/**
_Think of it as an async version of method `find`_
The findAsync() method returns a promise (`PrrayPromise` exactly) resolved with the first element in the prray that satisfies the provided async testing function.
```javascript
const workers = Prray.from(workerArray)
const unhealthy = await workers.findAsync(checkHealth)
```
* @param func
*/
findAsync(
func: (currentValue: T, index: number, prray: Prray<T>) => Promise<boolean> | boolean,
): Promise<T | undefined> {
return methods.findAsync(this, func)
}
find(func: (currentValue: T, index: number, prray: Prray<T>) => boolean): T | undefined {
return methods.find(this, func)
}
/**
_Think of it as an async version of method `findIndex`_
The findIndexAsync() method returns a promise (`PrrayPromise` exactly) resolved with the index of the first element in the prray that satisfies the provided async testing function. Otherwise, it returns promise resolved with -1, indicating that no element passed the test.
```javascript
const workers = Prray.from(workerArray)
const ix = await workers.findIndexAsync(checkHealth)
const unhealthy = workers[ix]
```
*/
findIndexAsync(
func: (currentValue: T, index: number, prray: Prray<T>) => Promise<boolean> | boolean,
): Promise<number> {
return methods.findIndexAsync(this, func)
}
findIndex(func: (currentValue: T, index: number, prray: Prray<T>) => boolean): number {
return methods.findIndex(this, func)
}
/**
_Think of it as an async version of method `every`_
The everyAsync() method tests whether all elements in the prray pass the test implemented by the provided async function. It returns a promise (`PrrayPromise` exactly) that resolved with a Boolean value, or rejected immediately if any of the promises reject.
The provided async function is called on every element concurrently. You may optionally specify a concurrency limit.
- `func(currentValue, index, prray)`
- options
- `concurrency` Number of concurrently pending promises returned by provided function. Default: `Infinity`
```javascript
const filenames = Prray.from(fileNameArray)
const isAllFileExisted = await filenames.everyAsync(isExisted)
if (isAllFileExisted) {
// do some things
}
```
*/
everyAsync(
func: (currentValue: T, index: number, prray: Prray<T>) => Promise<boolean> | boolean,
opts?: { concurrency: number },
): Promise<boolean> {
return methods.everyAsync(this, func, opts)
}
every(func: (currentValue: T, index: number, prray: Prray<T>) => boolean): boolean {
return methods.every(this, func)
}
/**
_Think of it as an async version of method `some`_
The some() method tests whether at least one element in the prray passes the test implemented by the provided async function. It returns a promise (`PrrayPromise` exactly) that resolved with Boolean value, or rejected immediately if any of the promises reject.
The provided async function is called on every element concurrently. You may optionally specify a concurrency limit.
- `func(currentValue, index, prray)`
- options
- `concurrency` Number of concurrently pending promises returned by provided function. Default: `Infinity`
```javascript
const filenames = Prray.from(fileNameArray)
const hasExistedFile = await filenames.someAsync(isExisted)
if (hasExistedFile) {
// do some things
}
```
*/
someAsync(
func: (currentValue: T, index: number, prray: Prray<T>) => Promise<boolean> | boolean,
opts?: { concurrency: number },
): Promise<boolean> {
return methods.someAsync(this, func, opts)
}
some(func: (currentValue: T, index: number, prray: Prray<T>) => boolean): boolean {
return methods.some(this, func)
}
/**
_Think of it as an async version of method `forEach`_
The forEachAsync() method executes a provided async function once for each prray element concurrently. It returns a promise (`PrrayPromise` exactly) that resolved after all iteration promises resolved, or rejected immediately if any of the promises reject.
The provided async function is called on every element concurrently. You may optionally specify a concurrency limit.
- `func(currentValue, index, prray)`
- options
- `concurrency` Number of concurrently pending promises returned by provided function. Default: `Infinity`
```javascript
const emails = Prray.from(emailArray)
await emails.forEachAsync(sendAsync)
await emails.forEachAsync(sendAsync, { concurrency: 20 })
```
* @param func
* @param opts
*/
forEachAsync(
func: (currentValue: T, index: number, prray: Prray<T>) => Promise<any> | any,
opts?: { concurrency: number },
): Promise<undefined> {
return methods.forEachAsync(this, func, opts)
}
forEach(func: (currentValue: T, index: number, prray: Prray<T>) => any): undefined {
return methods.forEach(this, func)
}
slice(start?: number, end?: number): Prray<T> {
const result: T[] = super.slice(start, end)
return _ensurePrray(result)
}
concat(...items: ConcatArray<T>[]): Prray<T>
concat(...items: (ConcatArray<T> | T)[]): Prray<T>
concat(...items: any[]): Prray<T> {
return _ensurePrray(super.concat(...items))
}
reverse(): Prray<T> {
return super.reverse() as Prray<T>
}
splice(start: number): Prray<T>
splice(start: number, deleteCount: number): Prray<T>
splice(start: number, deleteCount: number, ...items: T[]): Prray<T>
splice(start: number, deleteCount?: number, ...items: T[]): Prray<T> {
// Why? If pass parameter deleteCount as undefined directly, the delete count will be zero actually :(
const result = deleteCount === undefined ? super.splice(start) : super.splice(start, deleteCount, ...items)
return _ensurePrray(result)
}
/**
The toArray() method returns a new normal array with every element in the prray.
```javascript
const prr = new Prray(1, 2, 3)
prr.toArray() // [1,2,3]
```
*/
toArray(): T[] {
return [...this]
}
/**
The delay() method returns a promise (`PrrayPromise` exactly) that will be resolved with current prray instance after given ms milliseconds.
```javascript
const emails = Prray.from(emailArray)
await emails
.mapAsync(registerReceiver)
.delay(1000)
.forEachAsync(send)
```
*/
delay(ms: number): PrrayPromise<T> {
return new PrrayPromise(resolve => setTimeout(() => resolve(this), ms))
}
}
export function _ensurePrray<T>(arr: T[]): Prray<T> {
if (arr instanceof Prray) {
return arr
} else {
return Prray.from(arr)
}
}