summaryrefslogtreecommitdiff
path: root/lib/internal/filter.js
blob: 454cc543d11e5e40fa9a35bfa4ed4858f51c5199 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import each from 'lodash/each';
import isArrayLike from 'lodash/isArrayLike';
import noop from 'lodash/noop';
import once from './once';

export default function _filter(eachfn, coll, iteratee, callback) {
    callback = once(callback || noop);
    var truthValues = isArrayLike(coll) ? new Array(coll.length) : {};
    eachfn(coll, function (x, index, callback) {
        iteratee(x, function (err, v) {
            truthValues[index] = !!v;
            callback(err);
        });
    }, function (err) {
        if (err) return callback(err);
        var result = [];
        each(coll, function(_, index) {
            if (truthValues[index] === true) {
                result.push(coll[index]);
            }
        });
        callback(null, result);
    });
}