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

fix(filterFilter): comparator should return true if deep object key is undefined #6623

Closed
wants to merge 3 commits into from
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
6 changes: 4 additions & 2 deletions src/ng/filter/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,10 @@ function filterFilter() {
comparator = function(obj, text) {
if (obj && text && typeof obj === 'object' && typeof text === 'object') {
for (var objKey in obj) {
if (objKey.charAt(0) !== '$' && hasOwnProperty.call(obj, objKey) &&
comparator(obj[objKey], text[objKey])) {
if (objKey.charAt(0) !== '$' &&
hasOwnProperty.call(obj, objKey) &&
(!isDefined(text[objKey]) || comparator(obj[objKey], text[objKey]))
) {
return true;
}
}
Expand Down
23 changes: 13 additions & 10 deletions test/ng/filter/filterSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ describe('Filter: filter', function() {
expect(filter(items, function(i) {return i.done;}).length).toBe(1);
});

it('should take object as perdicate', function() {
it('should take object as predicate', function() {
var items = [{first: 'misko', last: 'hevery'},
{first: 'adam', last: 'abrons'}];

expect(filter(items, {first: undefined}).length).toBe(2);
expect(filter(items, {first:'', last:''}).length).toBe(2);
expect(filter(items, {first:'', last:'hevery'}).length).toBe(1);
expect(filter(items, {first:'adam', last:'hevery'}).length).toBe(0);
Expand All @@ -61,7 +62,7 @@ describe('Filter: filter', function() {
});


it('should support predicat object with dots in the name', function() {
it('should support predicate object with dots in the name', function() {
var items = [{'first.name': 'misko', 'last.name': 'hevery'},
{'first.name': 'adam', 'last.name': 'abrons'}];

Expand All @@ -75,6 +76,8 @@ describe('Filter: filter', function() {
{person: {name: 'Rita'}},
{person: {name: 'Billy'}},
{person: {name: 'Joan'}}];

expect(filter(items, {person: {name : undefined}}).length).toBe(4);
expect(filter(items, {person: {name: 'Jo'}}).length).toBe(2);
expect(filter(items, {person: {name: 'Jo'}})).toEqual([
{person: {name: 'John'}}, {person: {name: 'Joan'}}]);
Expand All @@ -94,8 +97,8 @@ describe('Filter: filter', function() {

it('should support boolean properties', function() {
var items = [{name: 'tom', current: true},
{name: 'demi', current: false},
{name: 'sofia'}];
{name: 'demi', current: false},
{name: 'sofia'}];

expect(filter(items, {current:true}).length).toBe(1);
expect(filter(items, {current:true})[0].name).toBe('tom');
Expand All @@ -118,26 +121,26 @@ describe('Filter: filter', function() {
expect(filter(items, expr, true)).toEqual([items[1]]);
expect(filter(items, expr, false)).toEqual([items[1], items[2]]);

var items = [
items = [
{key: 'value1', nonkey: 1},
{key: 'value2', nonkey: 2},
{key: 'value12', nonkey: 3},
{key: 'value1', nonkey:4},
{key: 'Value1', nonkey:5}
];
var expr = {key: 'value1'};
expr = {key: 'value1'};
expect(filter(items, expr, true)).toEqual([items[0], items[3]]);

var items = [
items = [
{key: 1, nonkey: 1},
{key: 2, nonkey: 2},
{key: 12, nonkey: 3},
{key: 1, nonkey:4}
];
var expr = { key: 1 };
expr = { key: 1 };
expect(filter(items, expr, true)).toEqual([items[0], items[3]]);

var expr = 12;
expr = 12;
expect(filter(items, expr, true)).toEqual([items[2]]);
});

Expand All @@ -151,7 +154,7 @@ describe('Filter: filter', function() {
var expr = {key: 10};
var comparator = function (obj,value) {
return obj > value;
}
};
expect(filter(items, expr, comparator)).toEqual([items[2]]);

expr = 10;
Expand Down