Skip to content

Commit c4b414e

Browse files
committed
fix(filterFilter) fix deeply-nested predicate objects + multiple conditions not returning expected filtered results
Current implementation of `filterFilter` when using deeply nested predicate objects + multiple conditions behaves like if it used _OR_ operators (i.e. it doesn't matter if some conditions don't match as long as one does), and should be behave like if it used _AND_ operators in order to be consistent with the original implementation for non-deeply-nested predicate objects. `filterFilter` working with deeply-nested predicate objects was introduced in 1.2.13 as a result of angular#6215.
1 parent 69d96e8 commit c4b414e

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/ng/filter/filter.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,12 @@ function filterFilter() {
137137
} else {
138138
comparator = function(obj, text) {
139139
if (obj && text && typeof obj === 'object' && typeof text === 'object') {
140-
for (var objKey in obj) {
141-
if (objKey.charAt(0) !== '$' && hasOwnProperty.call(obj, objKey) &&
142-
comparator(obj[objKey], text[objKey])) {
143-
return true;
140+
for (var textKey in text) {
141+
if (textKey.charAt(0) === '$' || !hasOwnProperty.call(text, textKey) ||
142+
!comparator(obj[textKey], text[textKey])) {
143+
return false
144144
}
145145
}
146-
return false;
147146
}
148147
text = (''+text).toLowerCase();
149148
return (''+obj).toLowerCase().indexOf(text) > -1;

test/ng/filter/filterSpec.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,31 @@ describe('Filter: filter', function() {
7474
var items = [{person: {name: 'John'}},
7575
{person: {name: 'Rita'}},
7676
{person: {name: 'Billy'}},
77-
{person: {name: 'Joan'}}];
77+
{person: {name: 'Joan'}},
78+
{person: {name: 'Albert', surname: 'Boada', contact: {country: 'Catalunya'}}}];
7879
expect(filter(items, {person: {name: 'Jo'}}).length).toBe(2);
7980
expect(filter(items, {person: {name: 'Jo'}})).toEqual([
8081
{person: {name: 'John'}}, {person: {name: 'Joan'}}
8182
]);
83+
expect(filter(items, {person: {name: 'Al', surname: 'Bo'}})).toEqual([items[4]]);
84+
expect(filter(items, {person: {name: 'foo', surname: 'Bo'}}).length).toBe(0);
85+
expect(filter(items, {person: {name: 'Al', surname: 'foo'}}).length).toBe(0);
86+
expect(filter(items, {person: {name: 'Al', contact: {country: 'cat'}}})).toEqual([items[4]]);
87+
expect(filter(items, {person: {name: 'Al', contact: {country: 'foo'}}}).length).toBe(0);
8288
});
8389

8490

8591
it('should match any properties for given "$" property', function() {
8692
var items = [{first: 'tom', last: 'hevery'},
8793
{first: 'adam', last: 'hevery', alias: 'tom', done: false},
88-
{first: 'john', last: 'clark', middle: 'tommy'}];
94+
{first: 'john', last: 'clark', middle: 'tommy'},
95+
{first: 'Albert', contact: {country: 'Catalunya'}}];
8996
expect(filter(items, {$: 'tom'}).length).toBe(3);
90-
expect(filter(items, {$: 'a'}).length).toBe(2);
97+
expect(filter(items, {$: 'a'}).length).toBe(3);
9198
expect(filter(items, {$: false}).length).toBe(1);
9299
expect(filter(items, {$: 10}).length).toBe(0);
93100
expect(filter(items, {$: 'hevery'})[0]).toEqual(items[0]);
101+
expect(filter(items, {$: 'Cat'})[0]).toEqual(items[3]);
94102
});
95103

96104
it('should support boolean properties', function() {

0 commit comments

Comments
 (0)