Skip to content

Commit e0ce9ed

Browse files
roylingIgorMinar
authored andcommitted
refactor(filterFilter): simplify code by a ternary op instead of if-else
- use only one IIFE and a ternary op in it, instead of invoking separate IIFEs in if-else (this also completely fixed the same issue closed by PR angular#3597) - also add a spec to verify usage of '$' property in expression object (e.g. `{$: 'a'}`) Closes angular#5637
1 parent caeb740 commit e0ce9ed

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

src/ng/filter/filter.js

+6-17
Original file line numberDiff line numberDiff line change
@@ -173,23 +173,12 @@ function filterFilter() {
173173
case "object":
174174
// jshint +W086
175175
for (var key in expression) {
176-
if (key == '$') {
177-
(function() {
178-
if (!expression[key]) return;
179-
var path = key;
180-
predicates.push(function(value) {
181-
return search(value, expression[path]);
182-
});
183-
})();
184-
} else {
185-
(function() {
186-
if (typeof(expression[key]) == 'undefined') { return; }
187-
var path = key;
188-
predicates.push(function(value) {
189-
return search(getter(value,path), expression[path]);
190-
});
191-
})();
192-
}
176+
(function(path) {
177+
if (typeof expression[path] == 'undefined') return;
178+
predicates.push(function(value) {
179+
return search(path == '$' ? value : getter(value, path), expression[path]);
180+
});
181+
})(key);
193182
}
194183
break;
195184
case 'function':

test/ng/filter/filterSpec.js

+11
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ describe('Filter: filter', function() {
6060
expect(filter(items, {first:'misko', last:'hevery'})[0]).toEqual(items[0]);
6161
});
6262

63+
it('should match any properties for given "$" property', function() {
64+
var items = [{first: 'tom', last: 'hevery'},
65+
{first: 'adam', last: 'hevery', alias: 'tom', done: false},
66+
{first: 'john', last: 'clark', middle: 'tommy'}];
67+
expect(filter(items, {$: 'tom'}).length).toBe(3);
68+
expect(filter(items, {$: 'a'}).length).toBe(2);
69+
expect(filter(items, {$: false}).length).toBe(1);
70+
expect(filter(items, {$: 10}).length).toBe(0);
71+
expect(filter(items, {$: 'hevery'})[0]).toEqual(items[0]);
72+
});
73+
6374
it('should support boolean properties', function() {
6475
var items = [{name: 'tom', current: true},
6576
{name: 'demi', current: false},

0 commit comments

Comments
 (0)