Skip to content

Commit

Permalink
fixup! Add support for iterable objects
Browse files Browse the repository at this point in the history
  • Loading branch information
o-rumiantsev committed May 27, 2019
1 parent 5e42355 commit 42ebb93
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 106 deletions.
134 changes: 46 additions & 88 deletions lib/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@ const { promisify } = require('util');
// result - <Iterable>
const map = (items, fn, done = common.emptiness) => {
const isArray = Array.isArray(items);
try {
asyncIter(items)
.parallel(promisify(fn))
.then(res => done(null, isArray ? res : new items.constructor(res)))
.catch(done);
} catch (err) {
done(err);
}
asyncIter(items)
.parallel(promisify(fn))
.then(res => done(null, isArray ? res : new items.constructor(res)))
.catch(done);
};

// Non-blocking synchronous map
Expand All @@ -44,17 +40,11 @@ const asyncMap = (items, fn, options = {}, done = common.emptiness) => {
options = {};
}
const isArray = Array.isArray(items);
try {
const iter = asyncIter(items)
.map(promisify(fn))
.throttle(options.percent, options.min);
const collect = isArray
? iter.toArray()
: iter.collectTo(items.constructor);
collect.then(res => done(null, res)).catch(done);
} catch (err) {
done(err);
}
const iter = asyncIter(items)
.map(promisify(fn))
.throttle(options.percent, options.min);
const collect = isArray ? iter.toArray() : iter.collectTo(items.constructor);
collect.then(res => done(null, res)).catch(done);
};

// Asynchronous filter (iterate parallel)
Expand All @@ -76,19 +66,15 @@ const asyncMap = (items, fn, options = {}, done = common.emptiness) => {
// );
const filter = (items, fn, done = common.emptiness) => {
const isArray = Array.isArray(items);
try {
asyncIter(items)
.parallel(async item => [await promisify(fn)(item), item])
.then(res => {
const filtered = res
.filter(([predicateResult]) => predicateResult)
.map(([, item]) => item);
done(null, isArray ? filtered : new items.constructor(filtered));
})
.catch(done);
} catch (err) {
done(err);
}
asyncIter(items)
.parallel(async item => [await promisify(fn)(item), item])
.then(res => {
const filtered = res
.filter(([predicateResult]) => predicateResult)
.map(([, item]) => item);
done(null, isArray ? filtered : new items.constructor(filtered));
})
.catch(done);
};

// Asynchronous reduce
Expand All @@ -110,14 +96,10 @@ const filter = (items, fn, done = common.emptiness) => {
// initial - <any>, optional value to be used as first
// argument in first iteration
const reduce = (items, fn, done = common.emptiness, initial) => {
try {
asyncIter(items)
.reduce((prev, cur) => promisify(fn)(prev, cur), initial)
.then(res => done(null, res))
.catch(done);
} catch (err) {
done(err);
}
asyncIter(items)
.reduce((prev, cur) => promisify(fn)(prev, cur), initial)
.then(res => done(null, res))
.catch(done);
};

// Asynchronous reduceRight
Expand All @@ -139,14 +121,10 @@ const reduce = (items, fn, done = common.emptiness, initial) => {
// initial - <any>, optional value to be used as first
// argument in first iteration
const reduceRight = (items, fn, done = common.emptiness, initial) => {
try {
asyncIter(items)
.reduceRight((prev, cur) => promisify(fn)(prev, cur), initial)
.then(res => done(null, res))
.catch(done);
} catch (err) {
done(err);
}
asyncIter(items)
.reduceRight((prev, cur) => promisify(fn)(prev, cur), initial)
.then(res => done(null, res))
.catch(done);
};

// Asynchronous each (iterate in parallel)
Expand All @@ -169,14 +147,10 @@ const reduceRight = (items, fn, done = common.emptiness, initial) => {
// (err, data) => console.dir('each done')
// );
const each = (items, fn, done = common.emptiness) => {
try {
asyncIter(items)
.parallel(promisify(fn))
.then(res => done(null, res))
.catch(done);
} catch (err) {
done(err);
}
asyncIter(items)
.parallel(promisify(fn))
.then(res => done(null, res))
.catch(done);
};

// Asynchronous series
Expand All @@ -201,14 +175,10 @@ const each = (items, fn, done = common.emptiness) => {
// }
// );
const series = (items, fn, done = common.emptiness) => {
try {
asyncIter(items)
.each(promisify(fn))
.then(res => done(null, res))
.catch(done);
} catch (err) {
done(err);
}
asyncIter(items)
.each(promisify(fn))
.then(res => done(null, res))
.catch(done);
};

// Asynchronous find (iterate in series)
Expand All @@ -231,14 +201,10 @@ const series = (items, fn, done = common.emptiness) => {
// }
// );
const find = (items, fn, done = common.emptiness) => {
try {
asyncIter(items)
.find(promisify(fn))
.then(res => done(null, res))
.catch(done);
} catch (err) {
done(err);
}
asyncIter(items)
.find(promisify(fn))
.then(res => done(null, res))
.catch(done);
};

// Asynchronous every
Expand All @@ -252,14 +218,10 @@ const find = (items, fn, done = common.emptiness) => {
// err - <Error> | <null>
// result - <boolean>
const every = (items, fn, done = common.emptiness) => {
try {
asyncIter(items)
.parallel(promisify(fn))
.then(res => done(null, res.every(e => e)))
.catch(done);
} catch (err) {
done(err);
}
asyncIter(items)
.parallel(promisify(fn))
.then(res => done(null, res.every(e => e)))
.catch(done);
};

// Asynchronous some (iterate in series)
Expand All @@ -273,14 +235,10 @@ const every = (items, fn, done = common.emptiness) => {
// err - <Error> | <null>
// result - <boolean>
const some = (items, fn, done = common.emptiness) => {
try {
asyncIter(items)
.some(promisify(fn))
.then(res => done(null, res))
.catch(done);
} catch (err) {
done(err);
}
asyncIter(items)
.some(promisify(fn))
.then(res => done(null, res))
.catch(done);
};

module.exports = {
Expand Down
12 changes: 6 additions & 6 deletions test/array.asyncMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ metatests.test('asyncMap with error', test => {

metatests.test('asyncMap with not iterable', test => {
const obj = { a: '1', b: '2', c: '3' };
const expectedError = new TypeError('Base is not Iterable');

metasync.asyncMap(obj, test.mustNotCall(), (err, res) => {
test.isError(err, expectedError);
test.assertNot(res);
test.end();
});
test.throws(
() => metasync.asyncMap(obj, test.mustNotCall(), test.mustNotCall()),
new TypeError('Base is not Iterable')
);

test.end();
});
12 changes: 6 additions & 6 deletions test/array.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ metatests.test('filter with error', test => {

metatests.test('filter with not iterable', test => {
const obj = { a: '1', b: '2', c: '3' };
const expectedError = new TypeError('Base is not Iterable');

metasync.filter(obj, test.mustNotCall(), (err, res) => {
test.isError(err, expectedError);
test.assertNot(res);
test.end();
});
test.throws(
() => metasync.filter(obj, test.mustNotCall(), test.mustNotCall()),
new TypeError('Base is not Iterable')
);

test.end();
});
12 changes: 6 additions & 6 deletions test/array.map.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ metatests.test('map with error', test => {

metatests.test('map with not iterable', test => {
const obj = { a: '1', b: '2', c: '3' };
const expectedError = new TypeError('Base is not Iterable');

metasync.map(obj, test.mustNotCall(), (err, res) => {
test.isError(err, expectedError);
test.strictSame(res, undefined);
test.end();
});
test.throws(
() => metasync.map(obj, test.mustNotCall(), test.mustNotCall()),
new TypeError('Base is not Iterable')
);

test.end();
});

0 comments on commit 42ebb93

Please sign in to comment.