From 060508a260a5c650f10f2ed38944f78c9406692a Mon Sep 17 00:00:00 2001 From: Graeme Yeates Date: Thu, 17 Nov 2016 09:08:54 -0500 Subject: Implement array specific filter function --- lib/internal/filter.js | 29 +++++++++++++++++++++++++---- 1 file 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,12 +1,28 @@ 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); @@ -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); +} -- cgit v1.2.1