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

Commit 95f3869

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

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/ng/filter/filter.js

+9
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ function filterFilter() {
133133
//jshint -W086
134134
case 'object':
135135
//jshint +W086
136+
if (!matchAgainstAnyProp && (expression !== null) && ('$' in expression)) {
137+
var keysCount = 0;
138+
for (var key in expression) { keysCount++; }
139+
if (keysCount === 1) {
140+
expression = expression.$;
141+
matchAgainstAnyProp = true;
142+
}
143+
}
144+
136145
predicateFn = createPredicateFn(expression, comparator, matchAgainstAnyProp);
137146
break;
138147
default:

test/ng/filter/filterSpec.js

+22
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'}];

0 commit comments

Comments
 (0)