tc39 / proposal-array-equality Goto Github PK
View Code? Open in Web Editor NEWDetermining Array Equality
License: MIT License
Determining Array Equality
License: MIT License
Can be promoted to ArrayBuffer
and TypedArray
?
Compare two ArrayBuffer
/ TypedArray
byte-wise for equality.
I feel this is ripe for a symbol that allows extending and modifying the equality check. Library precedent is rather lacking (unless you consider Fantasy Land's Setoid spec and its implementers as precedent), but there is a common .equals
idiom that's emerged that I've noticed.
For language precedent, it's pretty strong. Here's a few right off the top of my head:
public boolean equals(Object other)
boolean operator==(T other)
std::cmp::Eq<T>
, used by its ==
operatorData.Eq
For naming, I propose Symbol.equals
. This goes along with the idiom most are already using. And for standard library containers:
/gi
and /ig
are considered equivalent).this
is deep-equal with a key in other
and their corresponding entries are also deep-equal
this
is deep-equal with a key in other
The default behavior should still of course be the standard algorithm, and it's left implied that types of course also need to be equal.
I could also see HTML wanting to hook into this as well:
detail
deep-equal, eventPhase
and mutable properties ignored, all other properties identicalI feel very strongly that deep equality should be performed with a static function, not an array method or anything similar. Library and language precedent strongly argues in favor of this model:
_.isEqual(a, b)
R.equals(a, b)
assert.isDeepStrictEqual
's comparison function was factored out into util.isDeepStrictEqual
after modules like deep-equal
(10M downloads the past week as of today) and fast-deep-equal
(27M as of today) took off.=
, Java's a.equals(b)
method used practically everywhere as well as its java.util.Objects.equals
that papers over the awkwardness with null
, C++'s ==
behavior for all of its standard containers and wrappers, idiomatic std::cmp::Eq
implementations in Rust, among many others.JS Arrars have the weird quirk of supporting "empty" elements. For the most part, a = [0, , 2]
behaves same as b = [0, undefined, 2]
— both a[1] and b[1] are undefined
— but there are observable differences, e.g. Object.values(a)
is [0, 2]
while Object.values(b)
is [0, undefined, 2]
!
This means that some definitions of array equality can not be reduced to "pair-wise element equality" someLogic(a[i], b[i])
— there is no JS representation of "empty" you could pass to someLogic.
Also, due to how JS ignores function arity mismatch by passing undefined
, it's natural to wonder what the comparison will do on ['a', 'b', undefined]
vs ['a', 'b']
. Are different-length arrays where the extra trailing elements are all undefined
considered different?
(IMHO they should not be equal, but I think it's important to say specifically as JS programmers might plausibly guess either way.)
I guess this should be informed by prior art in popular deep-equal libraries.
My intuition differs here, and so does precedent in most other languages with dedicated array-oriented operators: https://github.com/isiahmeadows/es-stdlib-proposals/blob/master/proposals/array/equals.md
For deep matching, restricting it to just arrays seems like a very bad idea IMHO. Here's a concept I made for that, generalized to all values: https://github.com/isiahmeadows/es-stdlib-proposals/blob/master/proposals/value-matching.md
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.