Skip to content

Commit 22b1906

Browse files
committed
feat(isArray): make angular.isArray support Array subclasses
In particular, this change makes Angular play well with MobX observable arrays. See angular#15533
1 parent 316f60f commit 22b1906

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/Angular.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,7 @@ function isArrayLike(obj) {
232232

233233
// NodeList objects (with `item` method) and
234234
// other objects with suitable length characteristics are array-like
235-
return isNumber(length) &&
236-
(length >= 0 && ((length - 1) in obj || obj instanceof Array) || typeof obj.item === 'function');
235+
return isNumber(length) && (length >= 0 && (length - 1) in obj || typeof obj.item === 'function');
237236

238237
}
239238

@@ -634,12 +633,14 @@ function isDate(value) {
634633
* @kind function
635634
*
636635
* @description
637-
* Determines if a reference is an `Array`. Alias of Array.isArray.
636+
* Determines if a reference is an `Array`.
638637
*
639638
* @param {*} value Reference to check.
640639
* @returns {boolean} True if `value` is an `Array`.
641640
*/
642-
var isArray = Array.isArray;
641+
function isArray(arr) {
642+
return arr instanceof Array || Array.isArray(arr);
643+
}
643644

644645
/**
645646
* @ngdoc function

test/AngularSpec.js

+10
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,16 @@ describe('angular', function() {
12261226
});
12271227
});
12281228

1229+
describe('isArray', function() {
1230+
1231+
it('should return true if passed an object prototypically inherited from Array.prototype', function() {
1232+
function FooArray() {}
1233+
FooArray.prototype = [];
1234+
expect(isArray(new FooArray())).toBe(true);
1235+
});
1236+
1237+
});
1238+
12291239
describe('isArrayLike', function() {
12301240

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

0 commit comments

Comments
 (0)