From 30515ef47cf4ac8f68c5e6cc65d5c18e62493535 Mon Sep 17 00:00:00 2001 From: Albert Boada Date: Fri, 2 May 2014 05:44:17 +0200 Subject: [PATCH] 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 #6215. --- src/ng/filter/filter.js | 9 ++++----- test/ng/filter/filterSpec.js | 14 +++++++++++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/ng/filter/filter.js b/src/ng/filter/filter.js index 5c6bf2d79e7e..2a1541194003 100644 --- a/src/ng/filter/filter.js +++ b/src/ng/filter/filter.js @@ -137,13 +137,12 @@ 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; + for (var textKey in text) { + if (textKey.charAt(0) === '$' || !hasOwnProperty.call(text, textKey) || + !comparator(obj[textKey], text[textKey])) { + return false; } } - return false; } text = (''+text).toLowerCase(); return (''+obj).toLowerCase().indexOf(text) > -1; diff --git a/test/ng/filter/filterSpec.js b/test/ng/filter/filterSpec.js index fecfcf78b0c8..5ea9111d27bc 100644 --- a/test/ng/filter/filterSpec.js +++ b/test/ng/filter/filterSpec.js @@ -74,23 +74,31 @@ describe('Filter: filter', function() { var items = [{person: {name: 'John'}}, {person: {name: 'Rita'}}, {person: {name: 'Billy'}}, - {person: {name: 'Joan'}}]; + {person: {name: 'Joan'}}, + {person: {name: 'Albert', surname: 'Boada', contact: {country: 'Catalunya'}}}]; expect(filter(items, {person: {name: 'Jo'}}).length).toBe(2); expect(filter(items, {person: {name: 'Jo'}})).toEqual([ {person: {name: 'John'}}, {person: {name: 'Joan'}} ]); + expect(filter(items, {person: {name: 'Al', surname: 'Bo'}})).toEqual([items[4]]); + expect(filter(items, {person: {name: 'foo', surname: 'Bo'}}).length).toBe(0); + expect(filter(items, {person: {name: 'Al', surname: 'foo'}}).length).toBe(0); + expect(filter(items, {person: {name: 'Al', contact: {country: 'cat'}}})).toEqual([items[4]]); + expect(filter(items, {person: {name: 'Al', contact: {country: 'foo'}}}).length).toBe(0); }); it('should match any properties for given "$" property', function() { var items = [{first: 'tom', last: 'hevery'}, {first: 'adam', last: 'hevery', alias: 'tom', done: false}, - {first: 'john', last: 'clark', middle: 'tommy'}]; + {first: 'john', last: 'clark', middle: 'tommy'}, + {first: 'Albert', contact: {country: 'Catalunya'}}]; expect(filter(items, {$: 'tom'}).length).toBe(3); - expect(filter(items, {$: 'a'}).length).toBe(2); + expect(filter(items, {$: 'a'}).length).toBe(3); expect(filter(items, {$: false}).length).toBe(1); expect(filter(items, {$: 10}).length).toBe(0); expect(filter(items, {$: 'hevery'})[0]).toEqual(items[0]); + expect(filter(items, {$: 'Cat'})[0]).toEqual(items[3]); }); it('should support boolean properties', function() {