-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add toArray utility function to convert array-like objects to arrays. #3389
Conversation
@@ -36,6 +36,22 @@ export function isArray(value) { | |||
} | |||
|
|||
/** | |||
* Converts an array-like object to an array. | |||
* @param {?HTMLCollection|?NodeList|*} arrayLike |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reference, Closure's doc comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Used the closure compiler IArrayLike
type here.
I found out this |
Never mind, I think the polyfill for |
How do we know whether we need to polyfill or not? |
expect(arr.length).to.equal(4); | ||
expect(Array.isArray(arr)).to.be.true; | ||
}); | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing semicolon
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@muxin PTAL. I opted not to do the |
LGTM |
@@ -36,6 +36,26 @@ export function isArray(value) { | |||
} | |||
|
|||
/** | |||
* Converts an array-like object to an array. | |||
* @param {?IArrayLike<*>|string} arrayLike | |||
* @return {!Array.<*>} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No .
. Also, should we not be using the @template T
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, we seem to be using Nevermind, I see we do use *
everywhere, @cramforce @erwinmombay any idea?@template
syntax.
} | ||
const length = arrayLike.length; | ||
const array = new Array(length); | ||
for (let i = 0; i < array.length; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
arrayLike.length
. No idea how the browser will optimize when you're "iterating" the object you're modifying.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not modifying the length but updated to use arrayLike.length
just in case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not modifying the length
Right, but the JS engine may not know that.
Adding LGTM label - Yuxi already LGTM'd in comments. |
Separating this from a previous PR.
ITI: #3343