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

Commit 1b0d0fd

Browse files
Gonzalo Ruiz de Villagkalpak
Gonzalo Ruiz de Villa
authored andcommitted
feat(filterFilter): allow array like objects to be filtered
Throw error if filter is not used with an array like object. Closes #11782 Closes #11787
1 parent 4090491 commit 1b0d0fd

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-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

+17
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,23 @@ 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 getArguments() {
430+
return arguments;
431+
}
432+
var argsObj = getArguments({name: 'Misko'}, {name: 'Igor'}, {name: 'Brad'});
433+
434+
var nodeList = jqLite("<p><span>Misko</span><span>Igor</span><span>Brad</span></p>")[0].childNodes;
435+
function nodeFilterPredicate(node) {
436+
return node.innerHTML.indexOf("I") !== -1;
437+
}
438+
439+
expect(filter(argsObj, 'i').length).toBe(2);
440+
expect(filter('abc','b').length).toBe(1);
441+
expect(filter(nodeList, nodeFilterPredicate).length).toBe(1);
442+
443+
});
444+
428445

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

0 commit comments

Comments
 (0)