diff options
author | Alexander Early <alexander.early@gmail.com> | 2018-09-02 17:14:40 -0700 |
---|---|---|
committer | Alexander Early <alexander.early@gmail.com> | 2018-09-02 17:14:40 -0700 |
commit | ab7ccd148fcbbd0e56acca628a5c78b419852410 (patch) | |
tree | b77156a6d6faf8b50013d40149d9a96b92e38c16 | |
parent | 68363dc92e4601418873d974d08c763c9f969432 (diff) | |
download | async-ab7ccd148fcbbd0e56acca628a5c78b419852410.tar.gz |
awaitable groupBy
-rw-r--r-- | lib/groupBy.js | 6 | ||||
-rw-r--r-- | lib/groupByLimit.js | 9 | ||||
-rw-r--r-- | lib/groupBySeries.js | 7 | ||||
-rw-r--r-- | test/es2017/awaitableFunctions.js | 19 |
4 files changed, 34 insertions, 7 deletions
diff --git a/lib/groupBy.js b/lib/groupBy.js index 860a01f..2993367 100644 --- a/lib/groupBy.js +++ b/lib/groupBy.js @@ -1,4 +1,3 @@ -import doLimit from './internal/doLimit'; import groupByLimit from './groupByLimit'; /** @@ -25,6 +24,7 @@ import groupByLimit from './groupByLimit'; * @param {Function} [callback] - A callback which is called when all `iteratee` * functions have finished, or an error occurs. Result is an `Object` whoses * properties are arrays of values which returned the corresponding key. + * @returns {Promise} a promise, if no callback is passed * @example * * async.groupBy(['userId1', 'userId2', 'userId3'], function(userId, callback) { @@ -37,4 +37,6 @@ import groupByLimit from './groupByLimit'; * // e.g. { 30: ['userId1', 'userId3'], 42: ['userId2']}; * }); */ -export default doLimit(groupByLimit, Infinity); +export default function groupBy (coll, iteratee, callback) { + return groupByLimit(coll, Infinity, iteratee, callback) +} diff --git a/lib/groupByLimit.js b/lib/groupByLimit.js index f0402aa..5663637 100644 --- a/lib/groupByLimit.js +++ b/lib/groupByLimit.js @@ -1,6 +1,8 @@ import noop from './internal/noop'; import mapLimit from './mapLimit'; import wrapAsync from './internal/wrapAsync'; +import awaitify from './internal/awaitify' + /** * The same as [`groupBy`]{@link module:Collections.groupBy} but runs a maximum of `limit` async operations at a time. * @@ -19,11 +21,12 @@ import wrapAsync from './internal/wrapAsync'; * @param {Function} [callback] - A callback which is called when all `iteratee` * functions have finished, or an error occurs. Result is an `Object` whoses * properties are arrays of values which returned the corresponding key. + * @returns {Promise} a promise, if no callback is passed */ -export default function(coll, limit, iteratee, callback) { +function groupByLimit(coll, limit, iteratee, callback) { callback = callback || noop; var _iteratee = wrapAsync(iteratee); - mapLimit(coll, limit, (val, iterCb) => { + return mapLimit(coll, limit, (val, iterCb) => { _iteratee(val, (err, key) => { if (err) return iterCb(err); return iterCb(null, {key, val}); @@ -49,3 +52,5 @@ export default function(coll, limit, iteratee, callback) { return callback(err, result); }); } + +export default awaitify(groupByLimit, 4); diff --git a/lib/groupBySeries.js b/lib/groupBySeries.js index a093d7f..ee673e1 100644 --- a/lib/groupBySeries.js +++ b/lib/groupBySeries.js @@ -1,4 +1,3 @@ -import doLimit from './internal/doLimit'; import groupByLimit from './groupByLimit'; /** @@ -11,7 +10,6 @@ import groupByLimit from './groupByLimit'; * @see [async.groupBy]{@link module:Collections.groupBy} * @category Collection * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over. - * @param {number} limit - The maximum number of async operations at a time. * @param {AsyncFunction} iteratee - An async function to apply to each item in * `coll`. * The iteratee should complete with a `key` to group the value under. @@ -19,5 +17,8 @@ import groupByLimit from './groupByLimit'; * @param {Function} [callback] - A callback which is called when all `iteratee` * functions have finished, or an error occurs. Result is an `Object` whoses * properties are arrays of values which returned the corresponding key. + * @returns {Promise} a promise, if no callback is passed */ -export default doLimit(groupByLimit, 1); +export default function groupBySeries (coll, iteratee, callback) { + return groupByLimit(coll, 1, iteratee, callback) +} diff --git a/test/es2017/awaitableFunctions.js b/test/es2017/awaitableFunctions.js index 56cf07a..31d158b 100644 --- a/test/es2017/awaitableFunctions.js +++ b/test/es2017/awaitableFunctions.js @@ -163,4 +163,23 @@ module.exports = function () { await async.filterLimit(inputObj, 1, async (...args) => { calls.push(args) }); expect(calls).to.eql([[1], [2], [3]]) }); + + it('should return a Promise: groupBy', async () => { + expect (async.groupBy.name).to.contain('groupBy') + const calls = [] + await async.groupBy(inputObj, async (...args) => { calls.push(args) }); + expect(calls).to.eql([[1], [2], [3]]) + }); + it('should return a Promise: groupBySeries', async () => { + expect (async.groupBySeries.name).to.contain('groupBySeries') + const calls = [] + await async.groupBySeries(inputObj, async (...args) => { calls.push(args) }); + expect(calls).to.eql([[1], [2], [3]]) + }); + it('should return a Promise: groupByLimit', async () => { + expect (async.groupByLimit.name).to.contain('groupByLimit') + const calls = [] + await async.groupByLimit(inputObj, 1, async (...args) => { calls.push(args) }); + expect(calls).to.eql([[1], [2], [3]]) + }); }; |