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

Commit bd28c74

Browse files
gkalpaklgalfaso
authored andcommitted
fix(filterFilter): make $ match properties on deeper levels as well
Closes #10401
1 parent cd77c08 commit bd28c74

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

src/ng/filter/filter.js

+22-15
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,26 @@
1414
*
1515
* Can be one of:
1616
*
17-
* - `string`: The string is evaluated as an expression and the resulting value is used for substring match against
18-
* the contents of the `array`. All strings or objects with string properties in `array` that contain this string
19-
* will be returned. The predicate can be negated by prefixing the string with `!`.
17+
* - `string`: The string is used for matching against the contents of the `array`. All strings or
18+
* objects with string properties in `array` that match this string will be returned. This also
19+
* applies to nested object properties.
20+
* The predicate can be negated by prefixing the string with `!`.
2021
*
2122
* - `Object`: A pattern object can be used to filter specific properties on objects contained
2223
* by `array`. For example `{name:"M", phone:"1"}` predicate will return an array of items
2324
* which have property `name` containing "M" and property `phone` containing "1". A special
2425
* property name `$` can be used (as in `{$:"text"}`) to accept a match against any
25-
* property of the object. That's equivalent to the simple substring match with a `string`
26-
* as described above. The predicate can be negated by prefixing the string with `!`.
27-
* For Example `{name: "!M"}` predicate will return an array of items which have property `name`
26+
* property of the object or its nested object properties. That's equivalent to the simple
27+
* substring match with a `string` as described above. The predicate can be negated by prefixing
28+
* the string with `!`.
29+
* For example `{name: "!M"}` predicate will return an array of items which have property `name`
2830
* not containing "M".
2931
*
32+
* Note that a named property will match properties on the same level only, while the special
33+
* `$` property will match properties on the same level or deeper. E.g. an array item like
34+
* `{name: {first: 'John', last: 'Doe'}}` will **not** be matched by `{name: 'John'}`, but
35+
* **will** be matched by `{$: 'John'}`.
36+
*
3037
* - `function(value, index)`: A predicate function can be used to write arbitrary filters. The
3138
* function is called for each element of `array`. The final result is an array of those
3239
* elements that the predicate returned true for.
@@ -39,10 +46,10 @@
3946
*
4047
* - `function(actual, expected)`:
4148
* The function will be given the object value and the predicate value to compare and
42-
* should return true if the item should be included in filtered result.
49+
* should return true if both values should be considered equal.
4350
*
44-
* - `true`: A shorthand for `function(actual, expected) { return angular.equals(expected, actual)}`.
45-
* this is essentially strict comparison of expected and actual.
51+
* - `true`: A shorthand for `function(actual, expected) { return angular.equals(actual, expected)}`.
52+
* This is essentially strict comparison of expected and actual.
4653
*
4754
* - `false|undefined`: A short hand for a function which will look for a substring match in case
4855
* insensitive way.
@@ -173,7 +180,7 @@ function createPredicateFn(expression, comparator, matchAgainstAnyProp) {
173180
return predicateFn;
174181
}
175182

176-
function deepCompare(actual, expected, comparator, matchAgainstAnyProp) {
183+
function deepCompare(actual, expected, comparator, matchAgainstAnyProp, dontMatchWholeObject) {
177184
var actualType = typeof actual;
178185
var expectedType = typeof expected;
179186

@@ -192,21 +199,21 @@ function deepCompare(actual, expected, comparator, matchAgainstAnyProp) {
192199
var key;
193200
if (matchAgainstAnyProp) {
194201
for (key in actual) {
195-
if ((key.charAt(0) !== '$') && deepCompare(actual[key], expected, comparator)) {
202+
if ((key.charAt(0) !== '$') && deepCompare(actual[key], expected, comparator, true)) {
196203
return true;
197204
}
198205
}
199-
return false;
206+
return dontMatchWholeObject ? false : deepCompare(actual, expected, comparator, false);
200207
} else if (expectedType === 'object') {
201208
for (key in expected) {
202209
var expectedVal = expected[key];
203210
if (isFunction(expectedVal)) {
204211
continue;
205212
}
206213

207-
var keyIsDollar = key === '$';
208-
var actualVal = keyIsDollar ? actual : actual[key];
209-
if (!deepCompare(actualVal, expectedVal, comparator, keyIsDollar)) {
214+
var matchAnyProperty = key === '$';
215+
var actualVal = matchAnyProperty ? actual : actual[key];
216+
if (!deepCompare(actualVal, expectedVal, comparator, matchAnyProperty, matchAnyProperty)) {
210217
return false;
211218
}
212219
}

test/ng/filter/filterSpec.js

+22-6
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,30 @@ describe('Filter: filter', function() {
163163
});
164164

165165

166-
it('should respect the depth level of a "$" property', function() {
167-
var items = [{person: {name: 'Annet', email: 'annet@example.com'}},
168-
{person: {name: 'Billy', email: 'me@billy.com'}},
169-
{person: {name: 'Joan', email: {home: 'me@joan.com', work: 'joan@example.net'}}}];
170-
var expr = {person: {$: 'net'}};
166+
it('should match named properties only against named properties on the same level', function() {
167+
var expr = {person: {name: 'John'}};
168+
var items = [{person: 'John'}, // No match (1 level higher)
169+
{person: {name: 'John'}}, // Match (same level)
170+
{person: {name: {first: 'John', last: 'Doe'}}}]; // No match (1 level deeper)
171171

172172
expect(filter(items, expr).length).toBe(1);
173-
expect(filter(items, expr)).toEqual([items[0]]);
173+
expect(filter(items, expr)).toEqual([items[1]]);
174+
});
175+
176+
177+
it('should match any properties on same or deeper level for given "$" property', function() {
178+
var items = [{level1: 'test', foo1: 'bar1'},
179+
{level1: {level2: 'test', foo2:'bar2'}, foo1: 'bar1'},
180+
{level1: {level2: {level3: 'test', foo3: 'bar3'}, foo2: 'bar2'}, foo1: 'bar1'}];
181+
182+
expect(filter(items, {$: 'ES'}).length).toBe(3);
183+
expect(filter(items, {$: 'ES'})).toEqual([items[0], items[1], items[2]]);
184+
185+
expect(filter(items, {level1: {$: 'ES'}}).length).toBe(2);
186+
expect(filter(items, {level1: {$: 'ES'}})).toEqual([items[1], items[2]]);
187+
188+
expect(filter(items, {level1: {level2: {$: 'ES'}}}).length).toBe(1);
189+
expect(filter(items, {level1: {level2: {$: 'ES'}}})).toEqual([items[2]]);
174190
});
175191

176192

0 commit comments

Comments
 (0)