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

Commit 5b8c788

Browse files
mernenbtford
authored andcommitted
fix(isArrayLike): correctly handle string primitives
Closes #3356
1 parent fc8034b commit 5b8c788

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/Angular.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,21 @@ if (isNaN(msie)) {
8787
/**
8888
* @private
8989
* @param {*} obj
90-
* @return {boolean} Returns true if `obj` is an array or array-like object (NodeList, Arguments, ...)
90+
* @return {boolean} Returns true if `obj` is an array or array-like object (NodeList, Arguments, String ...)
9191
*/
9292
function isArrayLike(obj) {
9393
if (obj == null || isWindow(obj)) {
9494
return false;
9595
}
96-
96+
9797
var length = obj.length;
9898

9999
if (obj.nodeType === 1 && length) {
100100
return true;
101101
}
102102

103-
return isArray(obj) || !isFunction(obj) && (
104-
length === 0 || typeof length === "number" && length > 0 && (length - 1) in obj
105-
);
103+
return isString(obj) || isArray(obj) || length === 0 ||
104+
typeof length === 'number' && length > 0 && (length - 1) in obj;
106105
}
107106

108107
/**
@@ -599,7 +598,7 @@ function isLeafNode (node) {
599598
* * If a destination is provided, all of its elements (for array) or properties (for objects)
600599
* are deleted and then all elements/properties from the source are copied to it.
601600
* * If `source` is not an object or array (inc. `null` and `undefined`), `source` is returned.
602-
* * If `source` is identical to 'destination' an exception will be thrown.
601+
* * If `source` is identical to 'destination' an exception will be thrown.
603602
*
604603
* Note: this function is used to augment the Object type in Angular expressions. See
605604
* {@link ng.$filter} for more information about Angular arrays.

test/AngularSpec.js

+7
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,13 @@ describe('angular', function() {
461461
expect(log).toEqual(['0:a', '1:b', '2:c']);
462462
});
463463

464+
it('should handle string values like arrays', function() {
465+
var log = [];
466+
467+
forEach('bar', function(value, key) { log.push(key + ':' + value)});
468+
expect(log).toEqual(['0:b', '1:a', '2:r']);
469+
});
470+
464471

465472
it('should handle objects with length property as objects', function() {
466473
var obj = {

0 commit comments

Comments
 (0)