summaryrefslogtreecommitdiff
path: root/lib/internal/filter.js
blob: e191524c023381ec71548bb38c9af5c1027ab69a (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
25
26
27
import isArrayLike from 'lodash/isArrayLike';
import noop from 'lodash/noop';

import once from './once';
import iterator from './iterator';

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 = [];
        var nextElem = iterator(coll);
        var elem;
        while ((elem = nextElem()) !== null) {
            if (truthValues[elem.key] === true) {
                result.push(elem.value);
            }
        }
        callback(null, result);
    });
}