-
Notifications
You must be signed in to change notification settings - Fork 1
/
array-async.js
107 lines (94 loc) · 2.75 KB
/
array-async.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
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
/*jshint -W054 */
(function (exports) {
'use strict';
var forEachAsync = exports.forEachAsync || require('forEachAsync').forEachAsync
, ArrayAsync
;
ArrayAsync = {
forEach: forEachAsync
, every: function (arr, fn, thisArg) {
function everyFn(next, e, i, a) {
function everyNext(value) {
if (value) {
next(undefined, !!value);
} else {
next(forEachAsync.__BREAK, !!value);
}
}
fn.call(thisArg, everyNext, e, i, a);
}
return forEachAsync(arr, everyFn, thisArg);
}
, some: function (arr, fn, thisArg) {
function someFn(next, e, i, a) {
function someNext(value) {
if (value) {
next(forEachAsync.__BREAK, !!value);
} else {
next(undefined, !!value);
}
}
fn.call(thisArg, someNext, e, i, a);
}
return forEachAsync(arr, someFn, thisArg);
}
, filter: function (arr, fn, thisArg) {
var newArr = []
;
function filterFn(next, e, i, a) {
function filterNext(value) {
if (value) {
newArr.push(e);
}
next(undefined, newArr);
}
fn.call(thisArg, filterNext, e, i, a);
}
return forEachAsync(arr, filterFn, thisArg);
}
, map: function (arr, fn, thisArg) {
var newArr = []
;
function mapFn(next, e, i, a) {
function mapNext(value) {
newArr.push(value);
next(undefined, newArr);
}
fn.call(thisArg, mapNext, e, i, a);
}
return forEachAsync(arr, mapFn, thisArg);
}
, reduce: function (arr, fn, thisArg) {
var result = arr[0]
;
function reduceFn(next, e, i, a) {
function reduceNext(value) {
result = value;
next(undefined, result);
}
fn.call(thisArg, reduceNext, result, e, i + 1, a);
}
return forEachAsync(arr.slice(1), reduceFn, thisArg);
}
, reduceRight: function (arr, fn, thisArg) {
var result = arr[arr.length - 1]
;
function reduceRightFn(next, e, i, a) {
function reduceRightNext(value) {
result = value;
next(undefined, result);
}
fn.call(thisArg, reduceRightNext, result, e, a.length - (i + 1), a);
}
return forEachAsync(arr.slice(0, arr.length - 1).reverse(), reduceRightFn, thisArg);
}
};
ArrayAsync.__methods = Object.keys(ArrayAsync);
ArrayAsync.infect = function (thing) {
ArrayAsync.__methods.forEach(function (key) {
thing[key + 'Async'] = ArrayAsync[key];
});
};
ArrayAsync.infect(exports);
exports.ArrayAsync = ArrayAsync;
}('undefined' !== typeof exports && exports || new Function('return this')()));