- +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); | ||
| } | ||
| 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'}; | ||
| 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'}]; | ||
Showing you all comments on commits in this comparison.
This comment has been minimized.
This comment has been minimized.
LiZhangmei
commented on fb2c585
Sep 10, 2015
|
Can we use a string '...' instead of an object {$: '...'} to implement this function, I think it is a puzzling code from line 167~169. |
This comment has been minimized.
This comment has been minimized.
LiZhangmei
commented on fb2c585
Sep 10, 2015
|
not sure why not just use a simple string: expr = 'some'? |
This comment has been minimized.
This comment has been minimized.
|
I am not sure what you are asking. If the question is "Why not use If the question is "Why support this behaviour, since on could just use If the question is something else, then I need more clarification :) |
This comment has been minimized.
This comment has been minimized.
|
In your code, you probably should indeed use a string if you want to match against primitive items (not objects). I am not sure why it was implemented like this in the first place, but changing this behaviour (even if unintuitive) would break people's apps, so we re-instated it for backwards compatibility. See the discussion on #10428 for more context. (FWIW, I wouldn't use it in my app.) |
This comment has been minimized.
This comment has been minimized.
LiZhangmei
commented on fb2c585
Sep 10, 2015
|
my question is "Why support this behaviour, since on could just use
we could use |
This comment has been minimized.
This comment has been minimized.
LiZhangmei
commented on fb2c585
Sep 10, 2015
|
thanks very much, I get it by reading the discussion on #10428 |
This comment has been minimized.
This comment has been minimized.
|
I assume you wrote this before reading fb2c585#commitcomment-13152973 :) Hopefully things are clear now (let me know if they aren't). |
This comment has been minimized.
This comment has been minimized.
LiZhangmei
commented on fb2c585
Sep 10, 2015
|
yes, thanks |