Skip to content

Commit 0538dc0

Browse files
author
Shuhei Kagawa
committed
fix(filterFilter): filter deep object by string
Enable filterFilter with string expression to filter objects with deep properties. It used to work like this before angular#9757 and v1.3.6.
1 parent 7fd2dc1 commit 0538dc0

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/ng/filter/filter.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,18 @@ function createPredicateFn(expression, comparator, matchAgainstAnyProp) {
169169
return predicateFn;
170170
}
171171

172-
function deepCompare(actual, expected, comparator, matchAgainstAnyProp) {
172+
function deepCompare(actual, expected, comparator, matchAgainstAnyProp, temporaryAnyProp) {
173173
var actualType = typeof actual;
174174
var expectedType = typeof expected;
175+
var nextMatchAgainstAnyProp = matchAgainstAnyProp && !temporaryAnyProp;
175176

176177
if ((expectedType === 'string') && (expected.charAt(0) === '!')) {
177-
return !deepCompare(actual, expected.substring(1), comparator, matchAgainstAnyProp);
178+
return !deepCompare(actual, expected.substring(1), comparator, nextMatchAgainstAnyProp);
178179
} else if (actualType === 'array') {
179180
// In case `actual` is an array, consider it a match
180181
// if ANY of it's items matches `expected`
181182
return actual.some(function(item) {
182-
return deepCompare(item, expected, comparator, matchAgainstAnyProp);
183+
return deepCompare(item, expected, comparator, nextMatchAgainstAnyProp);
183184
});
184185
}
185186

@@ -188,7 +189,7 @@ function deepCompare(actual, expected, comparator, matchAgainstAnyProp) {
188189
var key;
189190
if (matchAgainstAnyProp) {
190191
for (key in actual) {
191-
if ((key.charAt(0) !== '$') && deepCompare(actual[key], expected, comparator)) {
192+
if ((key.charAt(0) !== '$') && deepCompare(actual[key], expected, comparator, nextMatchAgainstAnyProp)) {
192193
return true;
193194
}
194195
}
@@ -202,7 +203,7 @@ function deepCompare(actual, expected, comparator, matchAgainstAnyProp) {
202203

203204
var keyIsDollar = key === '$';
204205
var actualVal = keyIsDollar ? actual : actual[key];
205-
if (!deepCompare(actualVal, expectedVal, comparator, keyIsDollar)) {
206+
if (!deepCompare(actualVal, expectedVal, comparator, keyIsDollar, keyIsDollar)) {
206207
return false;
207208
}
208209
}

test/ng/filter/filterSpec.js

+7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ describe('Filter: filter', function() {
2828
expect(filter(items, "I don't exist").length).toBe(0);
2929
});
3030

31+
it('should filter deep object by string', function() {
32+
var items = [{person: {name: 'Annet', email: 'annet@example.com'}},
33+
{person: {name: 'Billy', email: 'me@billy.com'}},
34+
{person: {name: 'Joan', email: {home: 'me@joan.com', work: 'joan@example.net'}}}];
35+
expect(filter(items, 'me@joan').length).toBe(1);
36+
expect(filter(items, 'joan@example').length).toBe(1);
37+
});
3138

3239
it('should not read $ properties', function() {
3340
expect(''.charAt(0)).toBe(''); // assumption

0 commit comments

Comments
 (0)