Function: equals()
Deeply compares two values, returning true if they're equal and false otherwise. The following criteria are used to determine equality:
- All values are equal to themselves.
- Primitives
a
andb
are equal iffObject.is(a, b)
. This is similar to===
comparison, but treatsNaN
as equal toNaN
and0
as different from-0
. - Dates are equal iff they have the same millisecond-precision timestamp.
- RegExps are equal iff they have the same pattern and flags.
- Errors are equal iff they have the same class and message.
- Arrays are equal iff they have the same length and their corresponding
elements are equal (according to
equals
). - Sets are equal iff they contain the same elements. Note that set elements are not deeply compared.
- Partially applied curried functions are equal iff they originate from
the same curried function and their bound arguments are equal
according to
equals
. See curry. - Other objects are equal iff they have the same prototype (e.g. the same
class) and the same set of enumerable string-keyed properties, and the
values of their corresponding properties are equal (according to
equals
).
You can customize how equals()
compares values of a specific class by
using the $equals symbol to define a method on that
class. For example:
import {$equals} from "@longlast/symbols"
class HttpError extends Error {
private statusCode: number;
constructor(message: string, statusCode: number) {
super(message);
this.statusCode = statusCode;
}
[$equals](other: unknown) {
return other instanceof HttpError &&
other.statusCode === this.statusCode &&
other.message === this.message;
}
}
Note that this makes the comparison asymmetrical: a
is considered equal to
b
iff a[$equals](b)
returns truthy. The $equals
method will always be
called on the first argument to equals()
.
equals()
is curried. See curry.
Limitations
equals()
can throw a RangeError
if one of its arguments contains a
reference cycle. Avoid passing mutable objects to equals()
unless you know
that they do not contain cycles.
Call Signature
equals(
a
:unknown
,b
:unknown
):boolean
Defined in: node_modules/.pnpm/@longlast+curry@0.4.0/node_modules/@longlast/curry/dist/index.d.ts:41
Parameters
Parameter | Type |
---|---|
a |
unknown |
b |
unknown |
Returns
boolean
Call Signature
equals(
a
:unknown
):Curried1
<unknown
,boolean
>
Defined in: node_modules/.pnpm/@longlast+curry@0.4.0/node_modules/@longlast/curry/dist/index.d.ts:41
Parameters
Parameter | Type |
---|---|
a |
unknown |
Returns
Curried1
<unknown
, boolean
>