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

Use Object.entries instead of for…in to iterate over arrays #62

Merged
merged 1 commit into from Nov 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/util.js
Expand Up @@ -27,9 +27,9 @@ function isEmpty(object) {

function getValues(object) {
let result = [];
for (const key in object) {
for (const [, value] of Object.entries(object)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe Object.values might be better suited here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@tiaanduplessis ye I agree - browser support for Object.entries and Object.values appears to be the same, so we might as well use .values:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

An aside - do you know what the best practice is for using "new" Javascript specs in modules?

I generally iterate over Object.keys to avoid adding polyfills, at the expense of having to manually fetch values out of the object.

Is it up to the library to provide support for older browsers, or is it up to the user to add polyfills where necessary?

I imagine the responsible thing is to indicate which polyfills are necessary for older browsers when newer features are used in a module.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@larrybotha I'm not quite sure what is the current best practice for Svelte related modules, but I'd probably go with the user needing to add polyfilling where required and then indicating newer features used in the module README for full transparency.

The reason being that using core-js via babel will allow the user the flexibility to configure exactly what they want via browserlist and since Svelte does not compile down to older versions of JS out of the box the extra configuration will need to be added in any case to handle modern syntax and polyfiliing of popular browser APIs like Promises.

What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@tiaanduplessis ye, sounds good to me!

result = result.concat(
typeof object[key] === 'object' ? getValues(object[key]) : object[key],
typeof value === 'object' ? getValues(value) : value,
);
}
return result;
Expand Down