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

Commit

Permalink
feat(isArray): support Array subclasses in angular.isArray
Browse files Browse the repository at this point in the history
Closes #15533

BREAKING CHANGE:

`angular.isArray` is used by `angular.copy`, which in turn is used internally by the dirty
checking logic. That's why this change affects the way objects are copied and watched by AngularJS.
Objects that prototypally inherit from `Array` (e.g. MobX observable arrays, see #15533) weren't
previously recognized as arrays, now they are. This change also affects `angular.merge`,
`angular.forEach`, and `angular.equals`.

Previously, `angular.isArray` was an alias for and thus worked exactly as `Array.isArray`.
  • Loading branch information
thorn0 committed Dec 4, 2017
1 parent 8b69d91 commit 362b1cf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ 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 || obj instanceof Array) || typeof obj.item === 'function');
return isNumber(length) && (length >= 0 && (length - 1) in obj || typeof obj.item === 'function');

}

Expand Down Expand Up @@ -635,12 +634,14 @@ function isDate(value) {
* @kind function
*
* @description
* Determines if a reference is an `Array`. Alias of Array.isArray.
* Determines if a reference is an `Array`.
*
* @param {*} value Reference to check.
* @returns {boolean} True if `value` is an `Array`.
*/
var isArray = Array.isArray;
function isArray(arr) {
return arr instanceof Array || Array.isArray(arr);
}

/**
* @description
Expand Down
10 changes: 10 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,16 @@ describe('angular', function() {
});
});

describe('isArray', function() {

it('should return true if passed an object prototypically inherited from Array.prototype', function() {
function FooArray() {}
FooArray.prototype = [];
expect(isArray(new FooArray())).toBe(true);
});

});

describe('isArrayLike', function() {

it('should return false if passed a number', function() {
Expand Down

0 comments on commit 362b1cf

Please sign in to comment.