1 |
|
2 |
|
3 |
|
4 |
|
5 | const isPromise = (obj) => {
|
6 | return typeof obj === 'object' && typeof obj.then === 'function';
|
7 | };
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | exports.forEach = async (array, callback, thisArg) => {
|
16 | const promiseArray = [];
|
17 | for (let i = 0; i < array.length; i++) {
|
18 | promiseArray.push(callback.call(thisArg || this, array[i], i, array));
|
19 | }
|
20 | await Promise.all(promiseArray);
|
21 | };
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | exports.forEachSeries = async (array, callback, thisArg) => {
|
30 | for (let i = 0; i < array.length; i++) {
|
31 | await callback.call(thisArg || this, array[i], i, array);
|
32 | }
|
33 | };
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | exports.map = async (array, callback, thisArg) => {
|
42 | const promiseArray = [];
|
43 | for (let i = 0; i < array.length; i++) {
|
44 | if (i in array) {
|
45 | promiseArray[i] = callback.call(thisArg || this, array[i], i, array);
|
46 | }
|
47 | }
|
48 | return Promise.all(promiseArray);
|
49 | };
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | exports.mapSeries = async (array, callback, thisArg) => {
|
58 | const result = [];
|
59 | for (let i = 0; i < array.length; i++) {
|
60 | if (i in array) {
|
61 | result[i] = await callback.call(thisArg || this, array[i], i, array);
|
62 | }
|
63 | }
|
64 | return result;
|
65 | };
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | exports.find = (array, callback, thisArg) => {
|
74 | return new Promise((resolve, reject) => {
|
75 | for (let i = 0; i < array.length; i++) {
|
76 | const check = (found) => {
|
77 | if (found) {
|
78 | resolve(array[i]);
|
79 | }
|
80 | };
|
81 | const result = callback.call(thisArg || this, array[i], i, array);
|
82 | if (isPromise(result)) {
|
83 | result.then(check).catch(reject);
|
84 | } else {
|
85 | check(result);
|
86 | }
|
87 | }
|
88 | });
|
89 | };
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | exports.findSeries = async (array, callback, thisArg) => {
|
98 | for (let i = 0; i < array.length; i++) {
|
99 | if (await callback.call(thisArg || this, array[i], i, array)) {
|
100 | return array[i];
|
101 | }
|
102 | }
|
103 | };
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | exports.findIndex = (array, callback, thisArg) => {
|
112 | return new Promise((resolve, reject) => {
|
113 | for (let i = 0; i < array.length; i++) {
|
114 | const check = (found) => {
|
115 | if (found) {
|
116 | resolve(i);
|
117 | }
|
118 | };
|
119 | const result = callback.call(thisArg || this, array[i], i, array);
|
120 | if (isPromise(result)) {
|
121 | result.then(check).catch(reject);
|
122 | } else {
|
123 | check(result);
|
124 | }
|
125 | }
|
126 | });
|
127 | };
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | exports.findIndexSeries = async (array, callback, thisArg) => {
|
136 | for (let i = 0; i < array.length; i++) {
|
137 | if (await callback.call(thisArg || this, array[i], i, array)) {
|
138 | return i;
|
139 | }
|
140 | }
|
141 | };
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | exports.some = (array, callback, thisArg) => {
|
150 | return new Promise((resolve, reject) => {
|
151 | for (let i = 0; i < array.length; i++) {
|
152 | const check = (found) => {
|
153 | if (found) {
|
154 | resolve(true);
|
155 | } else if (i === array.length - 1) {
|
156 | resolve(false);
|
157 | }
|
158 | };
|
159 | const result = callback.call(thisArg || this, array[i], i, array);
|
160 | if (isPromise(result)) {
|
161 | result.then(check).catch(reject);
|
162 | } else {
|
163 | check(result);
|
164 | }
|
165 | }
|
166 | });
|
167 | };
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | exports.someSeries = async (array, callback, thisArg) => {
|
176 | for (let i = 0; i < array.length; i++) {
|
177 | if (await callback.call(thisArg || this, array[i], i, array)) {
|
178 | return true;
|
179 | }
|
180 | }
|
181 | return false;
|
182 | };
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | exports.every = (array, callback, thisArg) => {
|
191 | return new Promise((resolve, reject) => {
|
192 | for (let i = 0; i < array.length; i++) {
|
193 | const check = (found) => {
|
194 | if (!found) {
|
195 | resolve(false);
|
196 | } else if (i === array.length - 1) {
|
197 | resolve(true);
|
198 | }
|
199 | };
|
200 | const result = callback.call(thisArg || this, array[i], i, array);
|
201 | if (isPromise(result)) {
|
202 | result.then(check).catch(reject);
|
203 | } else {
|
204 | check(result);
|
205 | }
|
206 | }
|
207 | });
|
208 | };
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | exports.everySeries = async (array, callback, thisArg) => {
|
217 | for (let i = 0; i < array.length; i++) {
|
218 | if (!await callback.call(thisArg || this, array[i], i, array)) {
|
219 | return false;
|
220 | }
|
221 | }
|
222 | return true;
|
223 | };
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 | exports.filter = async (array, callback, thisArg) => {
|
232 | const promiseArray = [];
|
233 | for (let i = 0; i < array.length; i++) {
|
234 | if (i in array) {
|
235 | promiseArray[i] = callback.call(thisArg || this, array[i], i, array);
|
236 | } else {
|
237 | continue;
|
238 | }
|
239 | }
|
240 | return Promise.all(promiseArray).then(results => {
|
241 | const filteredArray = [];
|
242 | results.forEach((result, index) => {
|
243 | if (result) {
|
244 | filteredArray.push(array[index]);
|
245 | }
|
246 | });
|
247 | return filteredArray;
|
248 | });
|
249 | };
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 | exports.filterSeries = async (array, callback, thisArg) => {
|
258 | const result = [];
|
259 | for (let i = 0; i < array.length; i++) {
|
260 | if (i in array) {
|
261 | if (await callback.call(thisArg || this, array[i], i, array)) {
|
262 | result.push(array[i]);
|
263 | }
|
264 | }
|
265 | }
|
266 | return result;
|
267 | };
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 | exports.reduce = async (array, callback, initialValue) => {
|
276 | let previousValue;
|
277 | for (let i = 0; i < array.length; i++) {
|
278 | if (i === 0) {
|
279 | if (initialValue) {
|
280 | previousValue = initialValue;
|
281 | } else {
|
282 | previousValue = array[i];
|
283 | continue;
|
284 | }
|
285 | }
|
286 | if (i in array) {
|
287 | previousValue = await callback(previousValue, array[i], i, array);
|
288 | }
|
289 | }
|
290 | return previousValue;
|
291 | };
|