Skip to content

Commit 1070a5f

Browse files
author
Yehor Khilchenko
committed
Rewrite lib/array.js methods
Rewrited asyncIter in some methods.
1 parent 2fe1576 commit 1070a5f

File tree

4 files changed

+59
-169
lines changed

4 files changed

+59
-169
lines changed

lib/array.js

Lines changed: 52 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,23 @@
33
const common = require('@metarhia/common');
44
const { asyncIter } = require('./async-iterator.js');
55

6-
const copyClass = name => {
7-
switch (name) {
8-
case 'Set':
9-
return Set;
10-
case 'Map':
11-
return Map;
12-
case 'Array':
13-
return Array;
14-
case 'String':
15-
return String;
16-
}
17-
return null;
18-
};
19-
20-
const copyInstance = name => {
21-
switch (name) {
22-
case 'Set':
23-
return new Set();
24-
case 'Map':
25-
return new Map();
26-
case 'Array':
27-
return [];
28-
case 'String':
29-
return '';
30-
}
31-
return null;
32-
};
33-
34-
const map = (
35-
// Asynchronous map (iterate parallel)
36-
items, // array, incoming
37-
fn, // function, (current, callback) => callback(err, value)
38-
// to be executed for each value in the array
39-
// current - current element being processed in the array
40-
// callback - function(err, value)
41-
done // function (optional), on done callback function(err, result)
42-
) => {
43-
done = done || common.emptyness;
44-
const name = items.constructor.name;
45-
const result = copyClass(name);
46-
if (result === null) {
6+
// Asynchronous map (iterate parallel)
7+
// items - <Array>, incoming
8+
// fn - <Function>, to be executed for each value in the array
9+
// current - <any>, current element being processed in the array
10+
// callback - <Function>
11+
// err - <Error> | <null>
12+
// value - <any>
13+
// done - <Function>, on done, optional
14+
// err - <Error> | <null>
15+
// result - <Array>
16+
const map = (items, fn, done) => {
17+
if (!items[Symbol.iterator]) {
4718
done(null, null);
4819
return;
4920
}
21+
done = done || common.emptyness;
22+
const isArray = Array.isArray(items);
5023
const data = asyncIter(items).map(
5124
item =>
5225
new Promise((resolve, reject) => {
@@ -56,22 +29,8 @@ const map = (
5629
});
5730
})
5831
);
59-
if (name === 'Array') {
60-
data
61-
.toArray()
62-
.then(r => done(null, r))
63-
.catch(e => done(e));
64-
} else if (name === 'String') {
65-
data
66-
.toArray()
67-
.then(r => done(null, r.join('')))
68-
.catch(e => done(e));
69-
} else {
70-
data
71-
.collectTo(result)
72-
.then(r => done(null, r))
73-
.catch(e => done(e));
74-
}
32+
const promise = isArray ? data.toArray() : data.collectTo(items.constructor);
33+
promise.then(res => done(null, res), err => done(err));
7534
};
7635

7736
const DEFAULT_OPTIONS = { min: 5, percent: 0.7 };
@@ -95,7 +54,7 @@ const asyncMap = (items, fn, options = {}, done) => {
9554

9655
const len = items.length || items.size;
9756
const name = items.constructor.name;
98-
let result = done ? copyInstance(name) : null;
57+
let result = done ? new items.constructor() : null;
9958
const data = common.iter(items);
10059

10160
if (!len || result === null) {
@@ -158,12 +117,11 @@ const asyncMap = (items, fn, options = {}, done) => {
158117
// result - <Array>
159118
const filter = (items, fn, done) => {
160119
done = done || common.emptyness;
161-
const name = items.constructor.name;
162-
const result = copyClass(name);
163-
if (result === null) {
120+
if (!items[Symbol.iterator]) {
164121
done(null, null);
165122
return;
166123
}
124+
const isArray = Array.isArray(items);
167125
const data = asyncIter(items).filter(
168126
item =>
169127
new Promise((resolve, reject) => {
@@ -173,22 +131,8 @@ const filter = (items, fn, done) => {
173131
});
174132
})
175133
);
176-
if (name === 'Array') {
177-
data
178-
.toArray()
179-
.then(r => done(null, r))
180-
.catch(e => done(e));
181-
} else if (name === 'String') {
182-
data
183-
.toArray()
184-
.then(r => done(null, r.join('')))
185-
.catch(e => done(e));
186-
} else {
187-
data
188-
.collectTo(result)
189-
.then(r => done(null, r))
190-
.catch(e => done(e));
191-
}
134+
const promise = isArray ? data.toArray() : data.collectTo(items.constructor);
135+
promise.then(res => done(null, res), err => done(err));
192136
};
193137

194138
// Asynchronous reduce
@@ -210,12 +154,6 @@ const filter = (items, fn, done) => {
210154
// argument in first iteration
211155
const reduce = (items, fn, done, initial) => {
212156
done = done || common.emptyness;
213-
const name = items.constructor.name;
214-
const result = copyClass(name);
215-
if (result === null) {
216-
done(null, null);
217-
return;
218-
}
219157
asyncIter(items)
220158
.reduce(
221159
(prev, cur) =>
@@ -290,21 +228,17 @@ const reduceRight = (items, fn, done, initial) => {
290228
fn(previous, current, next, count, items);
291229
};
292230

293-
const each = (
294-
// Asynchronous each (iterate in parallel)
295-
items, // array, incoming
296-
fn, // function, (value, callback) => callback(err)
297-
// value - item from items array
298-
// callback - callback function(err)
299-
done // function (optional), on done callback function(err, items)
300-
) => {
231+
// Asynchronous each (iterate in parallel)
232+
// items - <Array>, incoming
233+
// fn - <Function>
234+
// value - <any>, item from items array
235+
// callback - <Function>
236+
// err - <Error> | <null>
237+
// done - <Function>, on done, optional
238+
// err - <Error> | <null>
239+
// items - <Array>
240+
const each = (items, fn, done) => {
301241
done = done || common.emptyness;
302-
const name = items.constructor.name;
303-
const result = copyClass(name);
304-
if (result === null) {
305-
done(null, null);
306-
return;
307-
}
308242
asyncIter(items)
309243
.each(
310244
item =>
@@ -319,14 +253,16 @@ const each = (
319253
.catch(e => done(e));
320254
};
321255

322-
const series = (
323-
// Asynchronous series
324-
items, // array, incoming
325-
fn, // function, (value, callback) => callback(err)
326-
// value - item from items array
327-
// callback - callback (err)
328-
done // function (optional), on done callback (err, items)
329-
) => {
256+
// Asynchronous series
257+
// items - <Array>, incoming
258+
// fn - <Function>
259+
// value - <any>, item from items array
260+
// callback - <Function>
261+
// err - <Error> | <null>
262+
// done - <Function>, on done, optional
263+
// err - <Error> | <null>
264+
// items - <Array>
265+
const series = (items, fn, done) => {
330266
done = done || common.emptyness;
331267
const len = items.length || items.size;
332268
const data = common.iter(items);
@@ -361,12 +297,6 @@ const series = (
361297
// result - <any>
362298
const find = (items, fn, done) => {
363299
done = done || common.emptyness;
364-
const name = items.constructor.name;
365-
const result = copyClass(name);
366-
if (result === null) {
367-
done(null, null);
368-
return;
369-
}
370300
asyncIter(items)
371301
.find(
372302
item =>
@@ -381,21 +311,18 @@ const find = (items, fn, done) => {
381311
.catch(e => done(e));
382312
};
383313

384-
const every = (
385-
// Asynchronous every
386-
items, // array, incoming
387-
fn, // function, (value, callback) => callback(err, fits)
388-
// value - item from items array
389-
// callback - callback function(err, fits)
390-
done // function, optional on done callback function(err, result)
391-
) => {
314+
// Asynchronous every
315+
// items - <Array>, incoming
316+
// fn - <Function>,
317+
// value - <any>, item from items array
318+
// callback - <Function>
319+
// err - <Error> | <null>
320+
// accepted - <boolean>
321+
// done - <Function>, on done, optional
322+
// err - <Error> | <null>
323+
// result - <boolean>
324+
const every = (items, fn, done) => {
392325
done = done || common.emptyness;
393-
const name = items.constructor.name;
394-
const result = copyClass(name);
395-
if (result === null) {
396-
done(null, null);
397-
return;
398-
}
399326
asyncIter(items)
400327
.every(
401328
item =>
@@ -422,12 +349,6 @@ const every = (
422349
// result - <boolean>
423350
const some = (items, fn, done) => {
424351
done = done || common.emptyness;
425-
const name = items.constructor.name;
426-
const result = copyClass(name);
427-
if (result === null) {
428-
done(null, null);
429-
return;
430-
}
431352
asyncIter(items)
432353
.some(
433354
item =>

lib/chain.js

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -66,37 +66,6 @@ ArrayChain.prototype.fetch = function(fn) {
6666
return this;
6767
};
6868

69-
// ArrayChain.prototype.execute = function(chain) {
70-
// this.chain = [];
71-
// return new Promise((resolve, reject) => {
72-
// reduce(
73-
// chain,
74-
// (prev, cur, cb) => {
75-
// async(cur.op)(prev, cur.fn, (err, res) => {
76-
// if (err) cb(err);
77-
// else cb(null, res);
78-
// });
79-
// },
80-
// (err, res) => {
81-
// if (err) reject(err);
82-
// else resolve(res);
83-
// },
84-
// this.array.slice()
85-
// );
86-
// });
87-
// };
88-
89-
// ArrayChain.prototype.fetch = function(fn) {
90-
// const next = (err, data) => {
91-
// this.array = data;
92-
// if (err) throw err;
93-
// };
94-
// this.execute(this.chain)
95-
// .then(r => fn(null, r, next))
96-
// .catch(e => fn(e));
97-
// return this;
98-
// };
99-
10069
ArrayChain.prototype.map = function(fn) {
10170
this.chain.push({ op: 'map', fn });
10271
return this;

test/array.filter.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ metatests.test('successful filter / map', test => {
117117

118118
metatests.test('successful filter / string', test => {
119119
const string = 'aaabcfeeeeds';
120-
const expectedSet = 'aaaeeee';
120+
const expectedSet = 'a,a,a,e,e,e,e';
121121

122122
metasync.filter(
123123
string,
@@ -127,7 +127,7 @@ metatests.test('successful filter / string', test => {
127127
}),
128128
(err, res) => {
129129
test.error(err);
130-
test.same(res, expectedSet);
130+
test.same([...res].join(''), expectedSet);
131131
test.end();
132132
}
133133
);
@@ -187,7 +187,7 @@ metatests.test('filter with empty / string', test => {
187187
(str, callback) => process.nextTick(() => callback(null, str.length < 6)),
188188
(err, res) => {
189189
test.error(err);
190-
test.strictSame(res, expectedStr);
190+
test.strictSame([...res], [...expectedStr]);
191191
test.end();
192192
}
193193
);
@@ -196,7 +196,7 @@ metatests.test('filter with empty / string', test => {
196196
metatests.test('filter with another object', test => {
197197
const obj = { a: '1', b: '2', c: '3' };
198198

199-
metasync.map(
199+
metasync.filter(
200200
obj,
201201
(x, callback) =>
202202
process.nextTick(() => {

test/array.map.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ metatests.test('succesfull map / map', test => {
5050

5151
metatests.test('succesfull map / string', test => {
5252
const string = 'abcdefgh';
53-
const expectedStr = 'ABCDEFGH';
53+
const expectedStr = 'A,B,C,D,E,F,G,H';
5454

5555
metasync.map(
5656
string,
5757
(x, callback) => process.nextTick(() => callback(null, x.toUpperCase())),
5858
(err, res) => {
5959
test.error(err);
60-
test.strictSame(res, expectedStr);
60+
test.strictSame([...res].join(''), expectedStr);
6161
test.end();
6262
}
6363
);
@@ -117,7 +117,7 @@ metatests.test('map with empty / string', test => {
117117
(x, callback) => process.nextTick(() => callback(null, x.toUpperCase())),
118118
(err, res) => {
119119
test.error(err);
120-
test.strictSame(res, expectedStr);
120+
test.strictSame([...res], [...expectedStr]);
121121
test.end();
122122
}
123123
);

0 commit comments

Comments
 (0)