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

Commit d4b60ad

Browse files
fealaercaitp
authored andcommitted
fix(filterFilter): use isArray() to determine array type
In JavaScript, an array is a special type of object, therefore typeof [] returns object. Added corresponding unit tests. Changed condition for array type to isArray. Closes #10621
1 parent b839f73 commit d4b60ad

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/ng/filter/filter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ function deepCompare(actual, expected, comparator, matchAgainstAnyProp, dontMatc
186186

187187
if ((expectedType === 'string') && (expected.charAt(0) === '!')) {
188188
return !deepCompare(actual, expected.substring(1), comparator, matchAgainstAnyProp);
189-
} else if (actualType === 'array') {
189+
} else if (isArray(actual)) {
190190
// In case `actual` is an array, consider it a match
191191
// if ANY of it's items matches `expected`
192192
return actual.some(function(item) {

test/ng/filter/filterSpec.js

+33
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,39 @@ describe('Filter: filter', function() {
9292
);
9393

9494

95+
it('should match items with array properties containing one or more matching items', function() {
96+
var items, expr;
97+
98+
items = [
99+
{tags: ['web', 'html', 'css', 'js']},
100+
{tags: ['hybrid', 'html', 'css', 'js', 'ios', 'android']},
101+
{tags: ['mobile', 'ios', 'android']}
102+
];
103+
expr = {tags: 'html'};
104+
expect(filter(items, expr).length).toBe(2);
105+
expect(filter(items, expr)).toEqual([items[0], items[1]]);
106+
107+
items = [
108+
{nums: [1, 345, 12]},
109+
{nums: [0, 46, 78]},
110+
{nums: [123, 4, 67]}
111+
];
112+
expr = {nums: 12};
113+
expect(filter(items, expr).length).toBe(2);
114+
expect(filter(items, expr)).toEqual([items[0], items[2]]);
115+
116+
items = [
117+
{customers: [{name: 'John'}, {name: 'Elena'}, {name: 'Bill'}]},
118+
{customers: [{name: 'Sam'}, {name: 'Klara'}, {name: 'Bill'}]},
119+
{customers: [{name: 'Molli'}, {name: 'Elena'}, {name: 'Lora'}]}
120+
];
121+
expr = {customers: {name: 'Bill'}};
122+
expect(filter(items, expr).length).toBe(2);
123+
expect(filter(items, expr)).toEqual([items[0], items[1]]);
124+
}
125+
);
126+
127+
95128
it('should take object as predicate', function() {
96129
var items = [{first: 'misko', last: 'hevery'},
97130
{first: 'adam', last: 'abrons'}];

0 commit comments

Comments
 (0)