aurelia / polyfills

The minimal set of polyfills needed to run Aurelia.
MIT License
26 stars 27 forks source link

Add support for iterators to Array.from #65

Closed RomkeVdMeulen closed 6 years ago

RomkeVdMeulen commented 6 years ago

The current Array.from polyfill accepts iterable objects and 'array-like' objects, but not iterators. This is correct according to the ES6 specification. However, calling Array.from() with an iterator rather than an iterable using the native implementations in Edge 42, Chrome 70 and Firefox 63 does give the result you would expect: all the values pointed to by the iterator are placed into a new array. This results in a difference in behavior between IE and other browsers. I suggest adding support for iterators to the polyfill.

Case in point (this caused our team quite some headaches to hunt down):

const headers = new Headers([["Content-Type", "text/plain"]]);
console.log(Array.from(headers.entries()));

In IE, using the polyfill, this gives an empty array. In browsers with native implementations you get [["content-type", "text/plain"]].

RomkeVdMeulen commented 6 years ago

Ah, I seem to have misdiagnosed the problem: the reason that this code works in modern browsers is because they have a Header implementation whose entries() returns an iterator that is also an iterable (its Symbol.iterator method returns itself). The Headers polyfill from polyfill.io doesn't seem to have this. I'll report the issue there. Apologies for the false alarm.