Permalink
Browse files

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
  • Loading branch information...
1 parent 4090491 commit 1b0d0fd8d00b42dffd798845fe0947d594372613 @gonzaloruizdevilla gonzaloruizdevilla committed with gkalpak May 1, 2015
Showing with 19 additions and 2 deletions.
  1. +2 −2 src/ng/filter/filter.js
  2. +17 −0 test/ng/filter/filterSpec.js
@@ -127,7 +127,7 @@
*/
function filterFilter() {
return function(array, expression, comparator) {
- if (!isArray(array)) {
+ if (!isArrayLike(array)) {
if (array == null) {
return array;
} else {
@@ -157,7 +157,7 @@ function filterFilter() {
return array;
}
- return array.filter(predicateFn);
+ return Array.prototype.filter.call(array, predicateFn);
};
}
@@ -425,6 +425,23 @@ describe('Filter: filter', function() {
toThrowMinErr('filter', 'notarray', 'Expected array but received: {"toString":null,"valueOf":null}');
});
+ it('should not throw an error if used with an array like object', function() {
+ function getArguments() {
+ return arguments;
+ }
+ var argsObj = getArguments({name: 'Misko'}, {name: 'Igor'}, {name: 'Brad'});
+
+ var nodeList = jqLite("<p><span>Misko</span><span>Igor</span><span>Brad</span></p>")[0].childNodes;
+ function nodeFilterPredicate(node) {
+ return node.innerHTML.indexOf("I") !== -1;
+ }
+
+ expect(filter(argsObj, 'i').length).toBe(2);
+ expect(filter('abc','b').length).toBe(1);
+ expect(filter(nodeList, nodeFilterPredicate).length).toBe(1);
+
+ });
+
it('should return undefined when the array is undefined', function() {
expect(filter(undefined, {})).toBeUndefined();

0 comments on commit 1b0d0fd

Please sign in to comment.