Skip to content

Commit 34b3dfa

Browse files
committed
fix(filterFilter): let expression object {$: '...'} also match primitive items
Closes angular#10428
1 parent 6617b42 commit 34b3dfa

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/ng/filter/filter.js

+5
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ function filterFilter() {
133133
//jshint -W086
134134
case 'object':
135135
//jshint +W086
136+
if (!matchAgainstAnyProp && (expression !== null) && ('$' in expression)) {
137+
expression = expression.$;
138+
matchAgainstAnyProp = true;
139+
}
140+
136141
predicateFn = createPredicateFn(expression, comparator, matchAgainstAnyProp);
137142
break;
138143
default:

test/ng/filter/filterSpec.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,28 @@ describe('Filter: filter', function() {
6565
});
6666

6767

68+
it('should match primitive array values against top-level `$` property in object expression',
69+
function() {
70+
var items, expr;
71+
72+
items = ['something', 'something else', 'another thing'];
73+
expr = {$: 'some'};
74+
expect(filter(items, expr).length).toBe(2);
75+
expect(filter(items, expr)).toEqual([items[0], items[1]]);
76+
77+
items = [{val: 'something'}, {val: 'something else'}, {val: 'another thing'}];
78+
expr = {$: 'some'};
79+
expect(filter(items, expr).length).toBe(2);
80+
expect(filter(items, expr)).toEqual([items[0], items[1]]);
81+
82+
items = [123, 456, 789];
83+
expr = {$: 1};
84+
expect(filter(items, expr).length).toBe(1);
85+
expect(filter(items, expr)).toEqual([items[0]]);
86+
}
87+
);
88+
89+
6890
it('should take object as predicate', function() {
6991
var items = [{first: 'misko', last: 'hevery'},
7092
{first: 'adam', last: 'abrons'}];
@@ -313,8 +335,6 @@ describe('Filter: filter', function() {
313335
expect(filter(items, {}).length).toBe(1);
314336
expect(filter(items, {})[0]).toBe(items[1]);
315337

316-
expect(filter(items, {$: 'll'}).length).toBe(0);
317-
318338
// Not affected by `Object.prototype`
319339
expect(filter(items, 'll').length).toBe(1);
320340
expect(filter(items, 'll')[0]).toBe(items[0]);

0 commit comments

Comments
 (0)