|
6 | 6 | * @kind function
|
7 | 7 | *
|
8 | 8 | * @description
|
9 |
| - * Selects a subset of items from `array` and returns it as a new array. |
| 9 | + * Selects a subset of items from `collection` and returns it as a new array. |
10 | 10 | *
|
11 |
| - * @param {Array} array The source array. |
| 11 | + * @param {Array|Object} collection The source collection. |
12 | 12 | * @param {string|Object|function()} expression The predicate to be used for selecting items from
|
13 |
| - * `array`. |
| 13 | + * `collection`. |
14 | 14 | *
|
15 | 15 | * Can be one of:
|
16 | 16 | *
|
|
19 | 19 | * will be returned. The predicate can be negated by prefixing the string with `!`.
|
20 | 20 | *
|
21 | 21 | * - `Object`: A pattern object can be used to filter specific properties on objects contained
|
22 |
| - * by `array`. For example `{name:"M", phone:"1"}` predicate will return an array of items |
| 22 | + * by `collection`. For example `{name:"M", phone:"1"}` predicate will return an array of items |
23 | 23 | * which have property `name` containing "M" and property `phone` containing "1". A special
|
24 | 24 | * property name `$` can be used (as in `{$:"text"}`) to accept a match against any
|
25 | 25 | * property of the object. That's equivalent to the simple substring match with a `string`
|
|
28 | 28 | * not containing "M".
|
29 | 29 | *
|
30 | 30 | * - `function(value, index)`: A predicate function can be used to write arbitrary filters. The
|
31 |
| - * function is called for each element of `array`. The final result is an array of those |
32 |
| - * elements that the predicate returned true for. |
| 31 | + * function is called for each element of `collection`. The final result is an array of those |
| 32 | + * values that the predicate returned true for. |
33 | 33 | *
|
34 | 34 | * @param {function(actual, expected)|true|undefined} comparator Comparator which is used in
|
35 | 35 | * determining if the expected value (from the filter expression) and actual value (from
|
36 |
| - * the object in the array) should be considered a match. |
| 36 | + * the object in the collection) should be considered a match. |
37 | 37 | *
|
38 | 38 | * Can be one of:
|
39 | 39 | *
|
|
116 | 116 | </example>
|
117 | 117 | */
|
118 | 118 | function filterFilter() {
|
119 |
| - return function(array, expression, comparator) { |
120 |
| - if (!isArray(array)) return array; |
| 119 | + return function(collection, expression, comparator) { |
| 120 | + if (!isObject(collection)) return collection; |
121 | 121 |
|
122 | 122 | var comparatorType = typeof(comparator),
|
123 | 123 | predicates = [];
|
@@ -208,15 +208,14 @@ function filterFilter() {
|
208 | 208 | predicates.push(expression);
|
209 | 209 | break;
|
210 | 210 | default:
|
211 |
| - return array; |
| 211 | + return collection; |
212 | 212 | }
|
213 | 213 | var filtered = [];
|
214 |
| - for ( var j = 0; j < array.length; j++) { |
215 |
| - var value = array[j]; |
216 |
| - if (predicates.check(value, j)) { |
| 214 | + forEach(collection, function(value, index) { |
| 215 | + if (predicates.check(value, index)) { |
217 | 216 | filtered.push(value);
|
218 | 217 | }
|
219 |
| - } |
| 218 | + }, this); |
220 | 219 | return filtered;
|
221 | 220 | };
|
222 | 221 | }
|
0 commit comments