Skip to content
This repository was 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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,12 @@ function isArrayLike(obj) {
return true;
}

return isArray(obj) || !isFunction(obj) && (
length === 0 || typeof length === "number" && length > 0 && (length - 1) in obj
);
if (typeof obj !== 'object') {
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.

typeof length === "number" && length > 0 && (length - 1) in obj;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,13 @@ describe('angular', function() {
expect(log).toEqual(['0:a', '1:b', '2:c']);
});

it('should handle string values like arrays', function() {
var log = [];

forEach('bar', function(value, key) { log.push(key + ':' + value)});
expect(log).toEqual(['0:b', '1:a', '2:r']);
});


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