|
1 |
| -import isArrayLike from 'lodash/isArrayLike'; |
| 1 | +import arrayMap from 'lodash/_arrayMap'; |
| 2 | +import property from 'lodash/_baseProperty'; |
2 | 3 | import noop from 'lodash/noop';
|
3 |
| - |
4 | 4 | import once from './once';
|
5 |
| -import iterator from './iterator'; |
6 | 5 |
|
7 |
| -export default function _filter(eachfn, coll, iteratee, callback) { |
| 6 | +export default function _filter(eachfn, arr, iteratee, callback) { |
8 | 7 | callback = once(callback || noop);
|
9 |
| - var truthValues = isArrayLike(coll) ? new Array(coll.length) : {}; |
10 |
| - eachfn(coll, function (x, index, callback) { |
| 8 | + var results = []; |
| 9 | + eachfn(arr, function (x, index, callback) { |
11 | 10 | iteratee(x, function (err, v) {
|
12 |
| - truthValues[index] = !!v; |
13 |
| - callback(err); |
| 11 | + if (err) { |
| 12 | + callback(err); |
| 13 | + } else { |
| 14 | + if (v) { |
| 15 | + results.push({index: index, value: x}); |
| 16 | + } |
| 17 | + callback(); |
| 18 | + } |
14 | 19 | });
|
15 | 20 | }, function (err) {
|
16 |
| - if (err) return callback(err); |
17 |
| - var result = []; |
18 |
| - var nextElem = iterator(coll); |
19 |
| - var elem; |
20 |
| - while ((elem = nextElem()) !== null) { |
21 |
| - if (truthValues[elem.key] === true) { |
22 |
| - result.push(elem.value); |
23 |
| - } |
| 21 | + if (err) { |
| 22 | + callback(err); |
| 23 | + } else { |
| 24 | + callback(null, arrayMap(results.sort(function (a, b) { |
| 25 | + return a.index - b.index; |
| 26 | + }), property('value'))); |
24 | 27 | }
|
25 |
| - callback(null, result); |
26 | 28 | });
|
27 | 29 | }
|
0 commit comments