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

Commit

Permalink
fix(isArrayLike): recognize empty instances of an Array subclass
Browse files Browse the repository at this point in the history
Fixes #13560
Closes #13708
  • Loading branch information
gkalpak authored and petebacondarwin committed Jan 7, 2016
1 parent 5ba4419 commit 323f9ab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ function isArrayLike(obj) {
// NodeList objects (with `item` method) and
// other objects with suitable length characteristics are array-like
return isNumber(length) &&
(length >= 0 && (length - 1) in obj || typeof obj.item == 'function');
(length >= 0 && ((length - 1) in obj || obj instanceof Array) || typeof obj.item == 'function');

}

/**
Expand Down
29 changes: 25 additions & 4 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1119,17 +1119,38 @@ describe('angular', function() {
});

it('should return true if passed a nodelist', function() {
var nodes = document.body.childNodes;
expect(isArrayLike(nodes)).toBe(true);
var nodes1 = document.body.childNodes;
expect(isArrayLike(nodes1)).toBe(true);

var nodes2 = document.getElementsByTagName('nonExistingTagName');
expect(isArrayLike(nodes2)).toBe(true);
});

it('should return false for objects with `length` but no matching indexable items', function() {
var obj = {
var obj1 = {
a: 'a',
b:'b',
length: 10
};
expect(isArrayLike(obj)).toBe(false);
expect(isArrayLike(obj1)).toBe(false);

var obj2 = {
length: 0
};
expect(isArrayLike(obj2)).toBe(false);
});

it('should return true for empty instances of an Array subclass', function() {
function ArrayLike() {}
ArrayLike.prototype = Array.prototype;

var arrLike = new ArrayLike();
expect(arrLike.length).toBe(0);
expect(isArrayLike(arrLike)).toBe(true);

arrLike.push(1, 2, 3);
expect(arrLike.length).toBe(3);
expect(isArrayLike(arrLike)).toBe(true);
});
});

Expand Down

0 comments on commit 323f9ab

Please sign in to comment.