fix(filterFilter): let expression object `{$: '...'}` also match prim…
…itive items Closes #10428
- Loading branch information
- +4 −0 src/ng/filter/filter.js
- +27 −0 test/ng/filter/filterSpec.js
| @@ -145,6 +145,7 @@ function filterFilter() { | ||
|
|
||
| // Helper functions for `filterFilter` | ||
| function createPredicateFn(expression, comparator, matchAgainstAnyProp) { | ||
| var shouldMatchPrimitives = isObject(expression) && ('$' in expression); | ||
| var predicateFn; | ||
|
|
||
| if (comparator === true) { | ||
| @@ -163,6 +164,9 @@ function createPredicateFn(expression, comparator, matchAgainstAnyProp) { | ||
| } | ||
|
|
||
| predicateFn = function(item) { | ||
| if (shouldMatchPrimitives && !isObject(item)) { | ||
| return deepCompare(item, expression.$, comparator, false); | ||
| } | ||
This comment has been minimized.
This comment has been minimized.
gkalpak
Author
Member
|
||
| return deepCompare(item, expression, comparator, matchAgainstAnyProp); | ||
| }; | ||
|
|
||
| @@ -65,6 +65,33 @@ describe('Filter: filter', function() { | ||
| }); | ||
|
|
||
|
|
||
| it('should match primitive array values against top-level `$` property in object expression', | ||
| function() { | ||
| var items, expr; | ||
|
|
||
| items = ['something', 'something else', 'another thing']; | ||
| expr = {$: 'some'}; | ||
This comment has been minimized.
This comment has been minimized.
gkalpak
Author
Member
|
||
| expect(filter(items, expr).length).toBe(2); | ||
| expect(filter(items, expr)).toEqual([items[0], items[1]]); | ||
|
|
||
| items = [{val: 'something'}, {val: 'something else'}, {val: 'another thing'}]; | ||
| expr = {$: 'some'}; | ||
| expect(filter(items, expr).length).toBe(2); | ||
| expect(filter(items, expr)).toEqual([items[0], items[1]]); | ||
|
|
||
| items = [123, 456, 789]; | ||
| expr = {$: 1}; | ||
| expect(filter(items, expr).length).toBe(1); | ||
| expect(filter(items, expr)).toEqual([items[0]]); | ||
|
|
||
| items = [true, false, 'true']; | ||
| expr = {$: true, ignored: 'false'}; | ||
| expect(filter(items, expr).length).toBe(2); | ||
| expect(filter(items, expr)).toEqual([items[0], items[2]]); | ||
| } | ||
| ); | ||
|
|
||
|
|
||
| it('should take object as predicate', function() { | ||
| var items = [{first: 'misko', last: 'hevery'}, | ||
| {first: 'adam', last: 'abrons'}]; | ||
Can we use a string '...' instead of an object {$: '...'} to implement this function, I think it is a puzzling code from line 167~169.