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

tree: use more ES6 iterators in the API #15

Open
dominicbarnes opened this issue Jul 1, 2017 · 3 comments
Open

tree: use more ES6 iterators in the API #15

dominicbarnes opened this issue Jul 1, 2017 · 3 comments

Comments

@dominicbarnes
Copy link
Member

From @dominicbarnes on October 15, 2016 5:57

As I've used ES6 iterators more, I've come to like them more than arrays for their flexibility. (not to mention you can easily use Array.from() if you want an array)

I'm pretty sure this would be a breaking change. (so planned for 2.x)

Copied from original issue: makojs/tree#30

@dominicbarnes
Copy link
Member Author

I'm thinking this would apply to any API that returns a list of File objects, making code that looks like this:

tree.getFiles().forEach(file => {
  // do stuff
})

become more like this:

for (const file of tree.getFiles()) {
  // do stuff
}

@dominicbarnes
Copy link
Member Author

From @darsain on October 15, 2016 8:32

Arrays are already iterable, so the 2nd example is already possible :) In fact, almost everything is iterable. Strings, TypedArrays, function arguments, even those damn NodeLists.

The only thing that you actually need to make iterable are objects. So maybe this:

for (const file of tree) {
  // do stuff
}

@dominicbarnes
Copy link
Member Author

Haha, funny you should bring that up, I started doing that in what I was working on tonight.

Even still, I like to think that I can save on some performance/memory if I'm able to avoid buffering the entire collection with Array.from() before returning, especially since graph.js uses iterators as well.

@dominicbarnes dominicbarnes changed the title Use more ES6 iterators in the API tree: use more ES6 iterators in the API Jul 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant