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

Commit 2c4ffd6

Browse files
ElephantRosepetebacondarwin
authored andcommitted
fix(filterFilter): do not throw an error if property is null when comparing objects
Closes #10991 Closes #10992 Closes #11116
1 parent d7ec5f3 commit 2c4ffd6

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/ng/filter/filter.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ function createPredicateFn(expression, comparator, matchAgainstAnyProp) {
187187
}
188188

189189
function deepCompare(actual, expected, comparator, matchAgainstAnyProp, dontMatchWholeObject) {
190-
var actualType = typeof actual;
191-
var expectedType = typeof expected;
190+
var actualType = (actual !== null) ? typeof actual : 'null';
191+
var expectedType = (expected !== null) ? typeof expected : 'null';
192192

193193
if ((expectedType === 'string') && (expected.charAt(0) === '!')) {
194194
return !deepCompare(actual, expected.substring(1), comparator, matchAgainstAnyProp);

test/ng/filter/filterSpec.js

+26
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,32 @@ describe('Filter: filter', function() {
428428
});
429429

430430

431+
it('should not throw an error if property is null when comparing object', function() {
432+
var items = [
433+
{ office:1, people: {name:'john'}},
434+
{ office:2, people: {name:'jane'}},
435+
{ office:3, people: null}
436+
];
437+
var f = { };
438+
expect(filter(items, f).length).toBe(3);
439+
440+
f = { people:null };
441+
expect(filter(items, f).length).toBe(1);
442+
443+
f = { people: {}};
444+
expect(filter(items, f).length).toBe(2);
445+
446+
f = { people:{ name: '' }};
447+
expect(filter(items, f).length).toBe(2);
448+
449+
f = { people:{ name:'john' }};
450+
expect(filter(items, f).length).toBe(1);
451+
452+
f = { people:{ name:'j' }};
453+
expect(filter(items, f).length).toBe(2);
454+
455+
});
456+
431457
describe('should support comparator', function() {
432458

433459
it('not consider `object === "[object Object]"` in non-strict comparison', function() {

0 commit comments

Comments
 (0)