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

Commit

Permalink
feat(filterFilter): allow array like objects to be filtered
Browse files Browse the repository at this point in the history
Throw error if filter is not used with an array like object.

Closes #11782
Closes #11787
  • Loading branch information
Gonzalo Ruiz de Villa authored and gkalpak committed May 19, 2015
1 parent 4090491 commit 1b0d0fd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ng/filter/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
*/
function filterFilter() {
return function(array, expression, comparator) {
if (!isArray(array)) {
if (!isArrayLike(array)) {
if (array == null) {
return array;
} else {
Expand Down Expand Up @@ -157,7 +157,7 @@ function filterFilter() {
return array;
}

return array.filter(predicateFn);
return Array.prototype.filter.call(array, predicateFn);
};
}

Expand Down
17 changes: 17 additions & 0 deletions test/ng/filter/filterSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 1b0d0fd

Please sign in to comment.