Skip to content

Commit

Permalink
Rewrite lib/array.js methods
Browse files Browse the repository at this point in the history
Rewrited asyncIter in some methods.
  • Loading branch information
Yehor Khilchenko committed Apr 5, 2019
1 parent 2fe1576 commit 1070a5f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 169 deletions.
183 changes: 52 additions & 131 deletions lib/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,23 @@
const common = require('@metarhia/common');
const { asyncIter } = require('./async-iterator.js');

const copyClass = name => {
switch (name) {
case 'Set':
return Set;
case 'Map':
return Map;
case 'Array':
return Array;
case 'String':
return String;
}
return null;
};

const copyInstance = name => {
switch (name) {
case 'Set':
return new Set();
case 'Map':
return new Map();
case 'Array':
return [];
case 'String':
return '';
}
return null;
};

const map = (
// Asynchronous map (iterate parallel)
items, // array, incoming
fn, // function, (current, callback) => callback(err, value)
// to be executed for each value in the array
// current - current element being processed in the array
// callback - function(err, value)
done // function (optional), on done callback function(err, result)
) => {
done = done || common.emptyness;
const name = items.constructor.name;
const result = copyClass(name);
if (result === null) {
// Asynchronous map (iterate parallel)
// items - <Array>, incoming
// fn - <Function>, to be executed for each value in the array
// current - <any>, current element being processed in the array
// callback - <Function>
// err - <Error> | <null>
// value - <any>
// done - <Function>, on done, optional
// err - <Error> | <null>
// result - <Array>
const map = (items, fn, done) => {
if (!items[Symbol.iterator]) {
done(null, null);
return;
}
done = done || common.emptyness;
const isArray = Array.isArray(items);
const data = asyncIter(items).map(
item =>
new Promise((resolve, reject) => {
Expand All @@ -56,22 +29,8 @@ const map = (
});
})
);
if (name === 'Array') {
data
.toArray()
.then(r => done(null, r))
.catch(e => done(e));
} else if (name === 'String') {
data
.toArray()
.then(r => done(null, r.join('')))
.catch(e => done(e));
} else {
data
.collectTo(result)
.then(r => done(null, r))
.catch(e => done(e));
}
const promise = isArray ? data.toArray() : data.collectTo(items.constructor);
promise.then(res => done(null, res), err => done(err));
};

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

const len = items.length || items.size;
const name = items.constructor.name;
let result = done ? copyInstance(name) : null;
let result = done ? new items.constructor() : null;
const data = common.iter(items);

if (!len || result === null) {
Expand Down Expand Up @@ -158,12 +117,11 @@ const asyncMap = (items, fn, options = {}, done) => {
// result - <Array>
const filter = (items, fn, done) => {
done = done || common.emptyness;
const name = items.constructor.name;
const result = copyClass(name);
if (result === null) {
if (!items[Symbol.iterator]) {
done(null, null);
return;
}
const isArray = Array.isArray(items);
const data = asyncIter(items).filter(
item =>
new Promise((resolve, reject) => {
Expand All @@ -173,22 +131,8 @@ const filter = (items, fn, done) => {
});
})
);
if (name === 'Array') {
data
.toArray()
.then(r => done(null, r))
.catch(e => done(e));
} else if (name === 'String') {
data
.toArray()
.then(r => done(null, r.join('')))
.catch(e => done(e));
} else {
data
.collectTo(result)
.then(r => done(null, r))
.catch(e => done(e));
}
const promise = isArray ? data.toArray() : data.collectTo(items.constructor);
promise.then(res => done(null, res), err => done(err));
};

// Asynchronous reduce
Expand All @@ -210,12 +154,6 @@ const filter = (items, fn, done) => {
// argument in first iteration
const reduce = (items, fn, done, initial) => {
done = done || common.emptyness;
const name = items.constructor.name;
const result = copyClass(name);
if (result === null) {
done(null, null);
return;
}
asyncIter(items)
.reduce(
(prev, cur) =>
Expand Down Expand Up @@ -290,21 +228,17 @@ const reduceRight = (items, fn, done, initial) => {
fn(previous, current, next, count, items);
};

const each = (
// Asynchronous each (iterate in parallel)
items, // array, incoming
fn, // function, (value, callback) => callback(err)
// value - item from items array
// callback - callback function(err)
done // function (optional), on done callback function(err, items)
) => {
// Asynchronous each (iterate in parallel)
// items - <Array>, incoming
// fn - <Function>
// value - <any>, item from items array
// callback - <Function>
// err - <Error> | <null>
// done - <Function>, on done, optional
// err - <Error> | <null>
// items - <Array>
const each = (items, fn, done) => {
done = done || common.emptyness;
const name = items.constructor.name;
const result = copyClass(name);
if (result === null) {
done(null, null);
return;
}
asyncIter(items)
.each(
item =>
Expand All @@ -319,14 +253,16 @@ const each = (
.catch(e => done(e));
};

const series = (
// Asynchronous series
items, // array, incoming
fn, // function, (value, callback) => callback(err)
// value - item from items array
// callback - callback (err)
done // function (optional), on done callback (err, items)
) => {
// Asynchronous series
// items - <Array>, incoming
// fn - <Function>
// value - <any>, item from items array
// callback - <Function>
// err - <Error> | <null>
// done - <Function>, on done, optional
// err - <Error> | <null>
// items - <Array>
const series = (items, fn, done) => {
done = done || common.emptyness;
const len = items.length || items.size;
const data = common.iter(items);
Expand Down Expand Up @@ -361,12 +297,6 @@ const series = (
// result - <any>
const find = (items, fn, done) => {
done = done || common.emptyness;
const name = items.constructor.name;
const result = copyClass(name);
if (result === null) {
done(null, null);
return;
}
asyncIter(items)
.find(
item =>
Expand All @@ -381,21 +311,18 @@ const find = (items, fn, done) => {
.catch(e => done(e));
};

const every = (
// Asynchronous every
items, // array, incoming
fn, // function, (value, callback) => callback(err, fits)
// value - item from items array
// callback - callback function(err, fits)
done // function, optional on done callback function(err, result)
) => {
// Asynchronous every
// items - <Array>, incoming
// fn - <Function>,
// value - <any>, item from items array
// callback - <Function>
// err - <Error> | <null>
// accepted - <boolean>
// done - <Function>, on done, optional
// err - <Error> | <null>
// result - <boolean>
const every = (items, fn, done) => {
done = done || common.emptyness;
const name = items.constructor.name;
const result = copyClass(name);
if (result === null) {
done(null, null);
return;
}
asyncIter(items)
.every(
item =>
Expand All @@ -422,12 +349,6 @@ const every = (
// result - <boolean>
const some = (items, fn, done) => {
done = done || common.emptyness;
const name = items.constructor.name;
const result = copyClass(name);
if (result === null) {
done(null, null);
return;
}
asyncIter(items)
.some(
item =>
Expand Down
31 changes: 0 additions & 31 deletions lib/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,37 +66,6 @@ ArrayChain.prototype.fetch = function(fn) {
return this;
};

// ArrayChain.prototype.execute = function(chain) {
// this.chain = [];
// return new Promise((resolve, reject) => {
// reduce(
// chain,
// (prev, cur, cb) => {
// async(cur.op)(prev, cur.fn, (err, res) => {
// if (err) cb(err);
// else cb(null, res);
// });
// },
// (err, res) => {
// if (err) reject(err);
// else resolve(res);
// },
// this.array.slice()
// );
// });
// };

// ArrayChain.prototype.fetch = function(fn) {
// const next = (err, data) => {
// this.array = data;
// if (err) throw err;
// };
// this.execute(this.chain)
// .then(r => fn(null, r, next))
// .catch(e => fn(e));
// return this;
// };

ArrayChain.prototype.map = function(fn) {
this.chain.push({ op: 'map', fn });
return this;
Expand Down
8 changes: 4 additions & 4 deletions test/array.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ metatests.test('successful filter / map', test => {

metatests.test('successful filter / string', test => {
const string = 'aaabcfeeeeds';
const expectedSet = 'aaaeeee';
const expectedSet = 'a,a,a,e,e,e,e';

metasync.filter(
string,
Expand All @@ -127,7 +127,7 @@ metatests.test('successful filter / string', test => {
}),
(err, res) => {
test.error(err);
test.same(res, expectedSet);
test.same([...res].join(''), expectedSet);
test.end();
}
);
Expand Down Expand Up @@ -187,7 +187,7 @@ metatests.test('filter with empty / string', test => {
(str, callback) => process.nextTick(() => callback(null, str.length < 6)),
(err, res) => {
test.error(err);
test.strictSame(res, expectedStr);
test.strictSame([...res], [...expectedStr]);
test.end();
}
);
Expand All @@ -196,7 +196,7 @@ metatests.test('filter with empty / string', test => {
metatests.test('filter with another object', test => {
const obj = { a: '1', b: '2', c: '3' };

metasync.map(
metasync.filter(
obj,
(x, callback) =>
process.nextTick(() => {
Expand Down
6 changes: 3 additions & 3 deletions test/array.map.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ metatests.test('succesfull map / map', test => {

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

metasync.map(
string,
(x, callback) => process.nextTick(() => callback(null, x.toUpperCase())),
(err, res) => {
test.error(err);
test.strictSame(res, expectedStr);
test.strictSame([...res].join(''), expectedStr);
test.end();
}
);
Expand Down Expand Up @@ -117,7 +117,7 @@ metatests.test('map with empty / string', test => {
(x, callback) => process.nextTick(() => callback(null, x.toUpperCase())),
(err, res) => {
test.error(err);
test.strictSame(res, expectedStr);
test.strictSame([...res], [...expectedStr]);
test.end();
}
);
Expand Down

0 comments on commit 1070a5f

Please sign in to comment.