Skip to content

Commit

Permalink
feat(filterFilter): support deeply nested predicate objects
Browse files Browse the repository at this point in the history
Due to 339a165, it became impossible to filter nested properties of an object using the filterFilter.
A proposed solution to this was to enable the use of nested predicate objects. This change enables the
use of these nested predicate objects.

Example:

```html
<div ng-repeat="it in items | filter:{ address: { country: 'Canuckistan'}}"></div>
```

Or

```js
$filter('filter')(items, { address: { country: 'Canuckistan' } });
```

Closes angular#6215
Related to angular#6009
  • Loading branch information
caitp authored and Sebastien Armand - sa250111 committed Feb 19, 2014
1 parent b58a439 commit 8962504
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
-assertNotHasOwnProperty,
-getter,
-getBlockElements,
-hasOwnProperty,
*/

Expand All @@ -96,7 +97,7 @@
* @returns {string} Lowercased string.
*/
var lowercase = function(string){return isString(string) ? string.toLowerCase() : string;};

var hasOwnProperty = Object.prototype.hasOwnProperty;

/**
* @ngdoc function
Expand Down
9 changes: 9 additions & 0 deletions src/ng/filter/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ function filterFilter() {
};
} else {
comparator = function(obj, text) {
if (obj && text && typeof obj === 'object' && typeof text === 'object') {
for (var objKey in obj) {
if (objKey.charAt(0) !== '$' && hasOwnProperty.call(obj, objKey) &&
comparator(obj[objKey], text[objKey])) {
return true;
}
}
return false;
}
text = (''+text).toLowerCase();
return (''+obj).toLowerCase().indexOf(text) > -1;
};
Expand Down
11 changes: 11 additions & 0 deletions test/ng/filter/filterSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ describe('Filter: filter', function() {
});


it('should support deep predicate objects', function() {
var items = [{person: {name: 'John'}},
{person: {name: 'Rita'}},
{person: {name: 'Billy'}},
{person: {name: 'Joan'}}];
expect(filter(items, {person: {name: 'Jo'}}).length).toBe(2);
expect(filter(items, {person: {name: 'Jo'}})).toEqual([
{person: {name: 'John'}}, {person: {name: 'Joan'}}]);
});


it('should match any properties for given "$" property', function() {
var items = [{first: 'tom', last: 'hevery'},
{first: 'adam', last: 'hevery', alias: 'tom', done: false},
Expand Down

0 comments on commit 8962504

Please sign in to comment.