diff options
author | Graeme Yeates <yeatesgraeme@gmail.com> | 2016-11-17 09:08:54 -0500 |
---|---|---|
committer | Graeme Yeates <yeatesgraeme@gmail.com> | 2016-11-17 09:08:54 -0500 |
commit | 060508a260a5c650f10f2ed38944f78c9406692a (patch) | |
tree | 2c94899d50d26c7cc205cdac2a95db5adf9923ca | |
parent | 665327e07e2437ef7959af6544ed02594cfe76dd (diff) | |
download | async-060508a260a5c650f10f2ed38944f78c9406692a.tar.gz |
Implement array specific filter functionfilter-array-specific
-rw-r--r-- | lib/internal/filter.js | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/lib/internal/filter.js b/lib/internal/filter.js index 34cd722..5af788c 100644 --- a/lib/internal/filter.js +++ b/lib/internal/filter.js @@ -1,13 +1,29 @@ import arrayMap from 'lodash/_arrayMap'; +import isArrayLike from 'lodash/isArrayLike'; import property from 'lodash/_baseProperty'; import noop from 'lodash/noop'; -import once from './once'; -export default function _filter(eachfn, arr, iteratee, callback) { - callback = once(callback || noop); - var results = []; +function filterArray(eachfn, arr, iteratee, callback) { + var truthValues = new Array(arr.length); eachfn(arr, function (x, index, callback) { iteratee(x, function (err, v) { + truthValues[index] = !!v; + callback(err); + }); + }, function (err) { + if (err) return callback(err); + var results = []; + for (var i = 0; i < arr.length; i++) { + if (truthValues[i]) results.push(arr[i]); + } + callback(null, results); + }); +} + +function filterGeneric(eachfn, coll, iteratee, callback) { + var results = []; + eachfn(coll, function (x, index, callback) { + iteratee(x, function (err, v) { if (err) { callback(err); } else { @@ -27,3 +43,8 @@ export default function _filter(eachfn, arr, iteratee, callback) { } }); } + +export default function _filter(eachfn, coll, iteratee, callback) { + var filter = isArrayLike(coll) ? filterArray : filterGeneric; + filter(eachfn, coll, iteratee, callback || noop); +} |