mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-01-12 22:45:52 +08:00
fix(filterFilter): let expression object {$: '...'} also match primitive items
Closes #10428
This commit is contained in:
committed by
Lucas Galfaso
parent
bdbe4fd34a
commit
fb2c585897
@@ -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'}];
|
||||
|
||||
Reference in New Issue
Block a user