Skip to content
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

Merged
merged 4 commits into from
Jun 3, 2016

Conversation

mkhatib
Copy link
Contributor

@mkhatib mkhatib commented May 31, 2016

Separating this from a previous PR.

ITI: #3343

@@ -36,6 +36,22 @@ export function isArray(value) {
}

/**
* Converts an array-like object to an array.
* @param {?HTMLCollection|?NodeList|*} arrayLike
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@muxin
Copy link
Contributor

muxin commented Jun 1, 2016

I found out this
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from
Might be useful in this case?

@muxin
Copy link
Contributor

muxin commented Jun 1, 2016

Never mind, I think the polyfill for Array.from is too much.

@muxin
Copy link
Contributor

muxin commented Jun 1, 2016

How do we know whether we need to polyfill or not?

expect(arr.length).to.equal(4);
expect(Array.isArray(arr)).to.be.true;
});
})
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing semicolon

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@mkhatib
Copy link
Contributor Author

mkhatib commented Jun 2, 2016

@muxin PTAL. I opted not to do the .from polyfill because I thought it was a bit too much. We could probably convert this into a polyfill in the future but for now a utility function should do.

@muxin
Copy link
Contributor

muxin commented Jun 2, 2016

LGTM

@@ -36,6 +36,26 @@ export function isArray(value) {
}

/**
* Converts an array-like object to an array.
* @param {?IArrayLike<*>|string} arrayLike
* @return {!Array.<*>}
Copy link
Contributor

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?

Copy link
Contributor Author

@mkhatib mkhatib Jun 2, 2016

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 * everywhere, @cramforce @erwinmombay any idea? Nevermind, I see we do use @template syntax.

}
const length = arrayLike.length;
const array = new Array(length);
for (let i = 0; i < array.length; i++) {
Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Contributor

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.

@mkhatib mkhatib added LGTM and removed NEEDS REVIEW labels Jun 3, 2016
@mkhatib
Copy link
Contributor Author

mkhatib commented Jun 3, 2016

Adding LGTM label - Yuxi already LGTM'd in comments.

@mkhatib mkhatib merged commit ff813b1 into ampproject:master Jun 3, 2016
@mkhatib mkhatib deleted the add-to-array branch June 3, 2016 00:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants