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

fix(isArrayLike): correctly handle string primitives #3714

Closed
wants to merge 1 commit into from

Conversation

mernen
Copy link
Contributor

@mernen mernen commented Aug 23, 2013

Fix for #3713:

The isArrayLike test in 1.0 worked properly with any string values, returning true (since they can be indexed just like String objects and work with functions like Array.prototype.forEach). The new test in 1.2 (introduced by #3356) raises a TypeError, because the in operator cannot be used on a non-object.

Since there are no tests for the private isArrayLike, I'm including a test for angular.forEach, its main caller.

return typeof obj === 'string';
}

return isArray(obj) || length === 0 ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, functions have a .length property which is why the jQuery implementation was explicitly making sure it wasn't a function. Should probably continue to handle that case so functions aren't treated as ArrayLike

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's now handled by the typeof test above, as callables have their own type even though they are technically objects.

@ghost ghost assigned btford Oct 2, 2013
@btford
Copy link
Contributor

btford commented Oct 2, 2013

isArrayLike should return false for strings.

We discussed this, and here (for the history books) is what @IgorMinar said:

hear ye, hear ye from this point onward, strings shall not be considered array-like -- Igor Minar on Oct 2, in the year 2013

The reason for this is that this helper is used primarily to determine the behavior for ngRepeat, and prevents weird behavior when trying to iterate over characters in a string.

If you do want to iterate over the characters of a string like this, you can simply explode the string into an array of characters with str.split('').

We changed our minds. Strings shall be array-like. Programming is hard.

@btford
Copy link
Contributor

btford commented Oct 2, 2013

Landed as 5b8c788. Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants