summaryrefslogtreecommitdiff
path: root/lib/internal/filter.js
blob: 35527e20a9a7f03c8ff58088de2cfb538c36b868 (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
28
29
30
31
import arrayMap from 'lodash/_arrayMap';
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 = [];
    eachfn(arr, function (x, index, callback) {
        iteratee(x, function (err, v) {
            if (err) {
                callback(err);
            }
            else {
                if (v) {
                    results.push({index: index, value: x});
                }
                callback();
            }
        });
    }, function (err) {
        if (err) {
            callback(err);
        }
        else {
            callback(null, arrayMap(results.sort(function (a, b) {
                return a.index - b.index;
            }), property('value')));
        }
    });
}