montagejs / collections

This package contains JavaScript implementations of common data structures with idiomatic interfaces.
http://www.collectionsjs.com
Other
2.09k stars 185 forks source link

Array.prototype.find() returns -1 instead of undefined #178

Closed theetrain closed 6 years ago

theetrain commented 7 years ago

The default behaviour of Array.prototype.find is to return the first matching value or undefined if not found. Collections.js returns -1 instead of undefined, which causes an unexpected issue with truthiness and coercion:

Standard behaviour:

[].find(a => true) // undefined
!![].find(a => true) // false

Collections.js behaviour:

[].find(a => true) // -1
!![].find(a => true) // true

I'm only curious why this decision was made.

kriskowal commented 7 years ago

I wrote Collections before the ink dried on the spec. Versions 1 and 3+ seem to be stuck with my mistake. @asolove and I fixed it in version 2.

This comes up often enough that @Stuk and I are considering forking collections and making an @collections/ org in npm rooted on version 2. That involves a lot of work and it is not on my evening and weekend roadmap. If there are people interested in sinking effort in that project, let us know. It would be great to have a community around such a central concern.

Collections is a Montage project so compatibility with Montage and all its inertia are the primary concern that dictates that Collections never stray far from backward compatibility with a host of API mispredictions.

kennethklee commented 7 years ago

why not, not modify native Array.prototype. This destroys any optimisations JS can do. Rather, create a new class based off Array.

kriskowal commented 7 years ago

@kennethklee as mentioned, Version 2 does not monkey-patch Array.prototype at all.

kriskowal commented 7 years ago

Alright, this has become enough of a problem that we will need broader support for collections in general. Please drop a line in here if you’ve got cycles to help move collections for JavaScript into a community supported suite to get around Montage’s compatibility limitations. We can talk about breaking the collections package into an @collections organization and how we can build a group of maintainers to make that migration.

https://docs.google.com/forms/d/e/1FAIpQLSd4j9-Uwy4Yoc9djU9Yh6RyhucjDhoyG58N4Fck7OZgHAtL-Q/viewform?c=0&w=1

hthetiot commented 6 years ago

185

codebling commented 5 years ago

See also issues #36 #70 #94 #95 #116 #139 #145 #162 #165 #169 #178 #182 #185 #197 #215 #220 and PRs #94 #95 #116 #173 #189 #212. Branch v2 fixes these issues by avoiding global object modification.