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 eabbd1d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 130 deletions.
126 changes: 28 additions & 98 deletions lib/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,6 @@
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
Expand All @@ -40,13 +12,12 @@ const map = (
// 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) {
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 +27,17 @@ 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 || items.constructor.name === 'String'
? data.toArray()
: data.collectTo(items.constructor);
promise.then(
res =>
items.constructor.name === 'String'
? done(null, res.join(''))
: done(null, res),
err => done(err)
);
};

const DEFAULT_OPTIONS = { min: 5, percent: 0.7 };
Expand All @@ -95,7 +61,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 +124,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 +138,17 @@ 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 || items.constructor.name === 'String'
? data.toArray()
: data.collectTo(items.constructor);
promise.then(
res =>
items.constructor.name === 'String'
? done(null, res.join(''))
: done(null, res),
err => done(err)
);
};

// Asynchronous reduce
Expand All @@ -210,12 +170,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 @@ -299,12 +253,6 @@ const each = (
done // function (optional), on done callback function(err, items)
) => {
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 Down Expand Up @@ -361,12 +309,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 @@ -390,12 +332,6 @@ const every = (
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) {
done(null, null);
return;
}
asyncIter(items)
.every(
item =>
Expand All @@ -422,12 +358,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
2 changes: 1 addition & 1 deletion test/array.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit eabbd1d

Please sign in to comment.