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
aandbare equal iffObject.is(a, b). This is similar to===comparison, but treatsNaNas equal toNaNand0as 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.
- Maps are equal iff they have the same set of keys, and their corresponding values are deeply equal. Note that map keys are not deeply compared.
- Partially applied functions (via curry or partial-apply)
are equal iff they originate from the same function and their bound
arguments are equal according to
equals. - 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: pkg/equals/src/index.ts:236
Parameters
| Parameter | Type |
|---|---|
a |
unknown |
b |
unknown |
Returns
boolean
Call Signature
equals(
a:unknown):Curried1<unknown,boolean>
Defined in: pkg/equals/src/index.ts:236
Parameters
| Parameter | Type |
|---|---|
a |
unknown |
Returns
Curried1<unknown, boolean>