mathiasbynens / Array.from

A robust & optimized ES3-compatible polyfill for the `Array.from` method in ECMAScript 6.

Home Page:https://mths.be/array-from

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Array.from does not work for ES6 Sets

dicksont opened this issue · comments

Hi,

I believe this current implementation does not work for ES6 Sets. For example:

Array.from(new Set([1,2])) // returns [] with the shim but [1,2] on my setup with the native Array.from

The source of problem seems to be demonstrated by the following:

a = Object(new Set([1,2]))
a.length // returns undefined

a.length should be undefined there - Object(new Set()) returns the Set itself - check its size property and it should report 2.

The reason Sets don't work is because this module currently doesn't support Symbol.iterator - which native Sets define.

This is a duplicate of #4.

I am sorry to say this, but you haven't said anything that I don't know already. I know:

  • a.length should be undefined
  • a.size is to be used to return the correct size in the case of a Set
  • this module does not use iterators to do the conversion.
  • and yes, I saw #4.

The problem is that the current code relies on the length property to step through the Array-like object. This breaks down for some types including Sets. I just want to know if there is any intention to fix this. Can we conclude that there will be no intention to fix this and that this module will not work for ES6 sets?

Not supporting iterators means it breaks for anything iterable that isn't array-like.

No, you can not conclude that, because #4 is still open.

Can you provide a timeline for this?

No, no timeline.

For now, you can use https://github.com/paulmillr/es6-shim which has an Array.from that does with with all iterables.

I understand that the scope of the rewrite might be substantial, but FYI, there is another implementation out there that works with ES6 that you can use, and base your work off of, and which I am currently using, instead of this flawed implementation because it works for Sets.

https://github.com/studio-b12/array-from