Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 167131c

Browse files
Justin Ridgewelljridgewell
Justin Ridgewell
authored andcommitted
feat(filter): allow filtering of an object
Allow ng.filter to filter an object as well as an array. Before, an object would need to be converted into an array before filtering. Closes #2694.
1 parent 47e15aa commit 167131c

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

src/ng/filter/filter.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
* @kind function
77
*
88
* @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.
1010
*
11-
* @param {Array} array The source array.
11+
* @param {Array|Object} collection The source collection.
1212
* @param {string|Object|function()} expression The predicate to be used for selecting items from
13-
* `array`.
13+
* `collection`.
1414
*
1515
* Can be one of:
1616
*
@@ -19,7 +19,7 @@
1919
* will be returned. The predicate can be negated by prefixing the string with `!`.
2020
*
2121
* - `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
2323
* which have property `name` containing "M" and property `phone` containing "1". A special
2424
* property name `$` can be used (as in `{$:"text"}`) to accept a match against any
2525
* property of the object. That's equivalent to the simple substring match with a `string`
@@ -28,12 +28,12 @@
2828
* not containing "M".
2929
*
3030
* - `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.
3333
*
3434
* @param {function(actual, expected)|true|undefined} comparator Comparator which is used in
3535
* 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.
3737
*
3838
* Can be one of:
3939
*
@@ -116,8 +116,8 @@
116116
</example>
117117
*/
118118
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;
121121

122122
var comparatorType = typeof(comparator),
123123
predicates = [];
@@ -208,15 +208,14 @@ function filterFilter() {
208208
predicates.push(expression);
209209
break;
210210
default:
211-
return array;
211+
return collection;
212212
}
213213
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)) {
217216
filtered.push(value);
218217
}
219-
}
218+
}, this);
220219
return filtered;
221220
};
222221
}

test/ng/filter/filterSpec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ describe('Filter: filter', function() {
4444
expect(filter(items, {name: 'b'})[0].name).toBe('abc');
4545
});
4646

47+
it('should filter an object', function() {
48+
var items = {0: {name: 'a'}, 1: {name: 'abc', done: true}};
49+
expect(filter(items, function(i) {return i.done;}).length).toBe(1);
50+
expect(filter(items, 'a').length).toBe(2);
51+
});
52+
4753
it('should take function as predicate', function() {
4854
var items = [{name: 'a'}, {name: 'abc', done: true}];
4955
expect(filter(items, function(i) {return i.done;}).length).toBe(1);

0 commit comments

Comments
 (0)