diff options
-rw-r--r-- | lib/index.js | 9 | ||||
-rw-r--r-- | lib/mapValues.js | 4 | ||||
-rw-r--r-- | lib/mapValuesLimit.js | 14 | ||||
-rw-r--r-- | lib/mapValuesSeries.js | 4 | ||||
-rw-r--r-- | mocha_test/mapValues.js | 92 |
5 files changed, 123 insertions, 0 deletions
diff --git a/lib/index.js b/lib/index.js index 1180dcf..091f657 100644 --- a/lib/index.js +++ b/lib/index.js @@ -43,6 +43,9 @@ import log from './log'; import map from './map'; import mapLimit from './mapLimit'; import mapSeries from './mapSeries'; +import mapValues from './mapValues'; +import mapValuesLimit from './mapValuesLimit'; +import mapValuesSeries from './mapValuesSeries'; import memoize from './memoize'; import nextTick from './nextTick'; import parallel from './parallel'; @@ -115,6 +118,9 @@ export default { map: map, mapLimit: mapLimit, mapSeries: mapSeries, + mapValues: mapValues, + mapValuesLimit: mapValuesLimit, + mapValuesSeries: mapValuesSeries, memoize: memoize, nextTick: nextTick, parallel: parallel, @@ -205,6 +211,9 @@ export { map as map, mapLimit as mapLimit, mapSeries as mapSeries, + mapValues as mapValues, + mapValuesLimit as mapValuesLimit, + mapValuesSeries as mapValuesSeries, memoize as memoize, nextTick as nextTick, parallel as parallel, diff --git a/lib/mapValues.js b/lib/mapValues.js new file mode 100644 index 0000000..60b8235 --- /dev/null +++ b/lib/mapValues.js @@ -0,0 +1,4 @@ +import mapValuesLimit from './mapValuesLimit'; +import doLimit from './internal/doLimit'; + +export default doLimit(mapValuesLimit, Infinity); diff --git a/lib/mapValuesLimit.js b/lib/mapValuesLimit.js new file mode 100644 index 0000000..f2698f0 --- /dev/null +++ b/lib/mapValuesLimit.js @@ -0,0 +1,14 @@ +import eachOfLimit from './eachOfLimit'; + +export default function mapValuesLimit(obj, limit, iteratee, callback) { + var newObj = {}; + eachOfLimit(obj, limit, function(val, key, next) { + iteratee(val, key, function (err, result) { + if (err) return next(err); + newObj[key] = result; + next(); + }) + }, function (err) { + callback(err, newObj); + }) +} diff --git a/lib/mapValuesSeries.js b/lib/mapValuesSeries.js new file mode 100644 index 0000000..f093a9e --- /dev/null +++ b/lib/mapValuesSeries.js @@ -0,0 +1,4 @@ +import mapValuesLimit from './mapValuesLimit'; +import doLimit from './internal/doLimit'; + +export default doLimit(mapValuesLimit, 1); diff --git a/mocha_test/mapValues.js b/mocha_test/mapValues.js new file mode 100644 index 0000000..3938be2 --- /dev/null +++ b/mocha_test/mapValues.js @@ -0,0 +1,92 @@ +var async = require('../lib'); +var expect = require('chai').expect; +var assert = require('assert'); + +describe('mapValues', function () { + var obj = {a: 1, b: 2, c: 3}; + + context('mapValuesLimit', function () { + it('basics', function (done) { + var running = 0 + var concurrency = { + a: 2, + b: 2, + c: 1 + } + async.mapValuesLimit(obj, 2, function (val, key, next) { + running++; + async.setImmediate(function () { + expect(running).to.equal(concurrency[key]); + running--; + next(null, key + val); + }); + }, function (err, result) { + expect(running).to.equal(0) + expect(err).to.eql(null); + expect(result).to.eql({a: 'a1', b: 'b2', c: 'c3'}) + done(); + }); + }); + + it('error', function (done) { + async.mapValuesLimit(obj, 1, function(val, key, next) { + if (key === 'b') { + return next(new Error("fail")); + } + next(null, val); + }, function (err, result) { + expect(err).to.not.eql(null); + expect(result).to.eql({a: 1}); + done(); + }); + }); + }); + + context('mapValues', function () { + it('basics', function (done) { + var running = 0 + var concurrency = { + a: 3, + b: 2, + c: 1 + } + async.mapValues(obj, function (val, key, next) { + running++; + async.setImmediate(function () { + expect(running).to.equal(concurrency[key]); + running--; + next(null, key + val); + }); + }, function (err, result) { + expect(running).to.equal(0) + expect(err).to.eql(null); + expect(result).to.eql({a: 'a1', b: 'b2', c: 'c3'}) + done(); + }); + }); + }); + + context('mapValuesSeries', function () { + it('basics', function (done) { + var running = 0 + var concurrency = { + a: 1, + b: 1, + c: 1 + } + async.mapValuesSeries(obj, function (val, key, next) { + running++; + async.setImmediate(function () { + expect(running).to.equal(concurrency[key]); + running--; + next(null, key + val); + }); + }, function (err, result) { + expect(running).to.equal(0) + expect(err).to.eql(null); + expect(result).to.eql({a: 'a1', b: 'b2', c: 'c3'}) + done(); + }); + }); + }); +}); |