diff options
author | Alexander Early <alexander.early@gmail.com> | 2016-05-31 16:36:09 -0700 |
---|---|---|
committer | Alexander Early <alexander.early@gmail.com> | 2016-05-31 16:36:09 -0700 |
commit | 5f6e76d9659c992a37b9ec488d6aff58652b918a (patch) | |
tree | 6384cad083b051c9576b41a0be43c288375e1512 | |
parent | 89f8fe4c3874e421f10c5d9ed5e9e1df98f8aaea (diff) | |
download | async-5f6e76d9659c992a37b9ec488d6aff58652b918a.tar.gz |
have map always return arrays
-rw-r--r-- | lib/internal/map.js | 12 | ||||
-rw-r--r-- | lib/map.js | 6 | ||||
-rw-r--r-- | mocha_test/map.js | 18 |
3 files changed, 22 insertions, 14 deletions
diff --git a/lib/internal/map.js b/lib/internal/map.js index 45c3eae..b587f59 100644 --- a/lib/internal/map.js +++ b/lib/internal/map.js @@ -1,15 +1,23 @@ import isArrayLike from 'lodash/isArrayLike'; import getIterator from './getIterator'; import noop from 'lodash/noop'; +import okeys from 'lodash/keys'; +import indexOf from 'lodash/_baseIndexOf'; import once from './once'; export default function _asyncMap(eachfn, arr, iteratee, callback) { callback = once(callback || noop); arr = arr || []; - var results = isArrayLike(arr) || getIterator(arr) ? [] : {}; + var results = []; + var keys; + if (!isArrayLike(arr) && !getIterator(arr)) { + keys = okeys(arr); + } + eachfn(arr, function (value, index, callback) { iteratee(value, function (err, v) { - results[index] = v; + var idx = keys ? indexOf(keys, index, 0) : index; + results[idx] = v; callback(err); }); }, function (err) { @@ -14,6 +14,10 @@ import doLimit from './internal/doLimit'; * in order. However, the results array will be in the same order as the * original `coll`. * + * If `map` is passed an Object, the results will be an Array. The results + * will roughly be in the order of the original Objects' keys (but this can + * vary across JavaScript engines) + * * @name map * @static * @memberOf async @@ -24,7 +28,7 @@ import doLimit from './internal/doLimit'; * once it has completed with an error (which can be `null`) and a * transformed item. Invoked with (item, callback). * @param {Function} [callback] - A callback which is called when all `iteratee` - * functions have finished, or an error occurs. Results is an array of the + * functions have finished, or an error occurs. Results is an Array of the * transformed items from the `coll`. Invoked with (err, results). * @example * diff --git a/mocha_test/map.js b/mocha_test/map.js index a537427..e7d4fa3 100644 --- a/mocha_test/map.js +++ b/mocha_test/map.js @@ -122,12 +122,10 @@ describe("map", function() { callback(null, val * 2); }, function(err, result) { if (err) throw err; - expect(Object.prototype.toString.call(result)).to.equal('[object Object]'); - expect(result).to.eql({ - a: 2, - b: 4, - c: 6 - }); + expect(Object.prototype.toString.call(result)).to.equal('[object Array]'); + expect(result).to.contain(2); + expect(result).to.contain(4); + expect(result).to.contain(6); done(); }); }); @@ -170,11 +168,9 @@ describe("map", function() { callback(null, val * 2); }, function(err, result) { if (err) throw err; - expect(result).to.eql({ - a: 2, - b: 4, - c: 6 - }); + expect(result).to.contain(2); + expect(result).to.contain(4); + expect(result).to.contain(6); done(); }); }); |