-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
72 lines (65 loc) · 2.29 KB
/
index.js
File metadata and controls
72 lines (65 loc) · 2.29 KB
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
/* global define */
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory)
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory()
} else {
// Browser globals (root is window)
root.AsyncArray = factory()
}
}(this, function () {
// Private functions
function reduce (data, callback, initialValue) {
// Return a promise
return new Promise((resolve, reject) => {
// Initialize the index
let i = 0
const _reduce = (currentValue) => {
// If we have finish to reduce, resolve with the last value computed
if (data.length === 0) {
resolve(currentValue)
}
// Extract a value from the copied array
const value = data.shift()
// Call the function
callback(currentValue, value, i++, this).then(_reduce, reject)
}
_reduce(initialValue)
})
}
function transformToAsyncParallel (method) {
return function (callback) {
// Take a copy of the array, it might mutate by the time we've finished
const data = this.constructor.from(this)
// Transform all the elements into an array of promises using the predicate
// as the promise
return Promise.all(data.map((element, index) => callback(element, index, data))).then(result => {
return data[ method ]((element, index) => {
return result[ index ]
})
})
}
}
// The main class
class AsyncArray extends Array {
reduceAsync (callback, initialValue) {
// Take a copy of the array, it might mutate by the time we've finished
const data = Array.from(this)
return reduce(data, callback, initialValue)
}
reduceRightAsync (callback, initialValue) {
// Take a copy of the array, it might mutate by the time we've finished
const data = Array.from(this).reverse()
return reduce(data, callback, initialValue)
}
}
for (const method of [ 'every', 'filter', 'find', 'findIndex', 'includes', 'some', 'map' ]) {
AsyncArray.prototype[ `${method}Async` ] = transformToAsyncParallel(method)
}
return AsyncArray
}))