Skip to content

Commit 419f560

Browse files
author
Gonzalo Ruiz de Villa
committed
fix(filterFilter): allow array like objects to be filtered
Throw error if filter is not used with an array like object. Closes angular#11782
1 parent 1268b17 commit 419f560

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/ng/filter/filter.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
*/
128128
function filterFilter() {
129129
return function(array, expression, comparator) {
130-
if (!isArray(array)) {
130+
if (!isArrayLike(array)) {
131131
if (array == null) {
132132
return array;
133133
} else {
@@ -157,7 +157,7 @@ function filterFilter() {
157157
return array;
158158
}
159159

160-
return array.filter(predicateFn);
160+
return Array.prototype.filter.call(array, predicateFn);
161161
};
162162
}
163163

test/ng/filter/filterSpec.js

+8
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,14 @@ describe('Filter: filter', function() {
425425
toThrowMinErr('filter', 'notarray', 'Expected array but received: {"toString":null,"valueOf":null}');
426426
});
427427

428+
it('should not throw an error if used with an array like object', function() {
429+
function aux() {
430+
expect(filter(arguments, 'i').length).toBe(2);
431+
}
432+
433+
aux({name: 'Misko'}, {name: 'Igor'}, {name: 'Brad'});
434+
});
435+
428436

429437
it('should return undefined when the array is undefined', function() {
430438
expect(filter(undefined, {})).toBeUndefined();

0 commit comments

Comments
 (0)