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

Commit fb2c585

Browse files
gkalpaklgalfaso
authored andcommittedDec 16, 2014
fix(filterFilter): let expression object {$: '...'} also match primitive items
Closes #10428
1 parent bdbe4fd commit fb2c585

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
 

‎src/ng/filter/filter.js

+4
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ function filterFilter() {
145145

146146
// Helper functions for `filterFilter`
147147
function createPredicateFn(expression, comparator, matchAgainstAnyProp) {
148+
var shouldMatchPrimitives = isObject(expression) && ('$' in expression);
148149
var predicateFn;
149150

150151
if (comparator === true) {
@@ -163,6 +164,9 @@ function createPredicateFn(expression, comparator, matchAgainstAnyProp) {
163164
}
164165

165166
predicateFn = function(item) {
167+
if (shouldMatchPrimitives && !isObject(item)) {
168+
return deepCompare(item, expression.$, comparator, false);
169+
}
Has conversations. Original line has conversations.
166170
return deepCompare(item, expression, comparator, matchAgainstAnyProp);
167171
};
168172

‎test/ng/filter/filterSpec.js

+27
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,33 @@ 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'};
Has conversations. Original line has conversations.
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+
items = [true, false, 'true'];
88+
expr = {$: true, ignored: 'false'};
89+
expect(filter(items, expr).length).toBe(2);
90+
expect(filter(items, expr)).toEqual([items[0], items[2]]);
91+
}
92+
);
93+
94+
6895
it('should take object as predicate', function() {
6996
var items = [{first: 'misko', last: 'hevery'},
7097
{first: 'adam', last: 'abrons'}];

0 commit comments

Comments
 (0)
This repository has been archived.