summaryrefslogtreecommitdiff
path: root/lib/internal/createTester.js
blob: 36223e6199b2fa749c1925b316f31f43e09156e7 (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
32
33
34
35
36
'use strict';
import noop from 'lodash/noop';
import breakLoop from './breakLoop';

export default function _createTester(eachfn, check, getResult) {
    return function(arr, limit, iteratee, cb) {
        function done() {
            if (cb) {
                cb(null, getResult(false));
            }
        }
        function wrappedIteratee(x, _, callback) {
            if (!cb) return callback();
            iteratee(x, function (err, v) {
                // Check cb as another iteratee may have resolved with a
                // value or error since we started this iteratee
                if (cb && (err || check(v))) {
                    if (err) cb(err);
                    else cb(err, getResult(true, x));
                    cb = iteratee = false;
                    return callback(err, breakLoop);
                }
                callback();
            });
        }
        if (arguments.length > 3) {
            cb = cb || noop;
            eachfn(arr, limit, wrappedIteratee, done);
        } else {
            cb = iteratee;
            cb = cb || noop;
            iteratee = limit;
            eachfn(arr, wrappedIteratee, done);
        }
    };
}