underscore icon indicating copy to clipboard operation
underscore copied to clipboard

iterator support for _.toArray

Open jdalton opened this issue 8 years ago • 5 comments

I think support for converting iterators to arrays would be a nice to have for _.toArray.

jdalton avatar Feb 27 '16 02:02 jdalton

@jdalton Do you mean Array.from ? I think, it is should be like polyfill Array.from

anthony-redFox avatar Feb 29 '16 07:02 anthony-redFox

Do you mean Array.from ?

I mean something like iteratorToArray which can be used as part of toArray. Browsers like IE11 have support for Map and Set without support for Array.from.

I think, it is should be like polyfill Array.from

Ya I get that, but _.toArray has more features. The addition of iterator support would round it out.

Array.from({ 'a': 1 })
// => []
_.toArray({ 'a': 1 })
// => [1]

jdalton avatar Feb 29 '16 07:02 jdalton

Rather than reimplementing toArray why don't we just call Array.from if it exists? You probably wouldn't / shouldn't be using iterators if you haven't polyfilled Array.from.

akre54 avatar Feb 29 '16 15:02 akre54

You probably wouldn't / shouldn't be using iterators if you haven't polyfilled Array.from.

Because Array.from isn't a requirement for iterator conversion and there are environments without it where maps and sets and other iterables work fine. So it would be inconsistent support.

The lines savings aren't worth the inconsistent support. We're talking about 4 or 5 LOC (less than 100 bytes). Nothing major.

IE11's market share for reference: marke tshare

jdalton avatar Feb 29 '16 15:02 jdalton

Will only make sense if combined with #2147.

jgonggrijp avatar May 09 '20 21:05 jgonggrijp