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

fix(filterFilter): fix matching against null/undefined #11445

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/ng/filter/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,16 @@ function filterFilter() {
}
}

var expressionType = getTypeForFilter(expression);
var predicateFn;
var matchAgainstAnyProp;

switch (typeof expression) {
switch (expressionType) {
case 'function':
predicateFn = expression;
break;
case 'boolean':
case 'null':
case 'number':
case 'string':
matchAgainstAnyProp = true;
Expand Down Expand Up @@ -172,6 +174,14 @@ function createPredicateFn(expression, comparator, matchAgainstAnyProp) {
comparator = equals;
} else if (!isFunction(comparator)) {
comparator = function(actual, expected) {
if (isUndefined(actual)) {
// No substring matching against `undefined`
return false;
}
if ((actual === null) || (expected === null)) {
// No substring matching against `null`; only match against `null`
return actual === expected;
}
if (isObject(expected) || (isObject(actual) && !hasCustomToString(actual))) {
// Should not compare primitives against objects, unless they have custom `toString` method
return false;
Expand All @@ -194,8 +204,8 @@ function createPredicateFn(expression, comparator, matchAgainstAnyProp) {
}

function deepCompare(actual, expected, comparator, matchAgainstAnyProp, dontMatchWholeObject) {
var actualType = (actual !== null) ? typeof actual : 'null';
var expectedType = (expected !== null) ? typeof expected : 'null';
var actualType = getTypeForFilter(actual);
var expectedType = getTypeForFilter(expected);

if ((expectedType === 'string') && (expected.charAt(0) === '!')) {
return !deepCompare(actual, expected.substring(1), comparator, matchAgainstAnyProp);
Expand Down Expand Up @@ -241,3 +251,8 @@ function deepCompare(actual, expected, comparator, matchAgainstAnyProp, dontMatc
return comparator(actual, expected);
}
}

// Used for easily differentiating between `null` and actual `object`
function getTypeForFilter(val) {
return (val === null) ? 'null' : typeof val;
}
85 changes: 68 additions & 17 deletions test/ng/filter/filterSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,33 +438,84 @@ describe('Filter: filter', function() {


it('should not throw an error if property is null when comparing object', function() {
var items = [
{ office:1, people: {name:'john'}},
{ office:2, people: {name:'jane'}},
{ office:3, people: null}
];
var f = { };
expect(filter(items, f).length).toBe(3);
var items = [
{ office:1, people: {name:'john'}},
{ office:2, people: {name:'jane'}},
{ office:3, people: null}
];
var f = { };
expect(filter(items, f).length).toBe(3);

f = { people:null };
expect(filter(items, f).length).toBe(1);

f = { people: {}};
expect(filter(items, f).length).toBe(2);

f = { people:{ name: '' }};
expect(filter(items, f).length).toBe(2);

f = { people:{ name:'john' }};
expect(filter(items, f).length).toBe(1);

f = { people:{ name:'j' }};
expect(filter(items, f).length).toBe(2);
});


f = { people:null };
expect(filter(items, f).length).toBe(1);
it('should match `null` against `null` only', function() {
var items = [
{value: null},
{value: undefined},
{value: true},
{value: false},
{value: NaN},
{value: 42},
{value: 'null'},
{value: 'test'},
{value: {}},
{value: new Date()}
];
var flt;

flt = null;
expect(filter(items, flt).length).toBe(1);
expect(filter(items, flt)[0]).toBe(items[0]);

f = { people: {}};
expect(filter(items, f).length).toBe(2);
flt = {value: null};
expect(filter(items, flt).length).toBe(1);
expect(filter(items, flt)[0]).toBe(items[0]);

f = { people:{ name: '' }};
expect(filter(items, f).length).toBe(2);
flt = {value: undefined};
expect(filter(items, flt).length).toBe(items.length);

f = { people:{ name:'john' }};
expect(filter(items, f).length).toBe(1);
flt = {value: NaN};
expect(includes(filter(items, flt), items[0])).toBeFalsy();

f = { people:{ name:'j' }};
expect(filter(items, f).length).toBe(2);
flt = {value: false};
expect(includes(filter(items, flt), items[0])).toBeFalsy();

flt = '';
expect(includes(filter(items, flt), items[0])).toBeFalsy();

flt = {value: 'null'};
expect(includes(filter(items, flt), items[0])).toBeFalsy();
});


describe('should support comparator', function() {

it('not convert `null` or `undefined` to string in non-strict comparison', function() {
var items = [
{value: null},
{value: undefined}
];
var flt = {value: 'u'};

expect(filter(items, flt).length).toBe(0);
});


it('not consider objects without a custom `toString` in non-strict comparison', function() {
var items = [{test: {}}];
var expr = '[object';
Expand Down