Objects.equal()
Syntax
function equal(source: unknown, ...others: unknown[]): boolean
Compares two or more objects for equality and returns true
if all are “equal”.
Parameters | |
source
|
An arbitrary object of any type. |
others
Rest
|
An array of objects of any type you wish to compare with the source object. |
Example
import { Objects } from 'potence';
console.log(Objects.equal(2, 2)); // -> true
const equatable = {
value: 1,
equals: function (other: unknown) { return other != null && this.value === other.value;}
};
const nonEquatable = { value: 1 };
console.log(Objects.equal(equatable, nonEquatable)); // -> true
Remarks
“Equality” in this context means one of three things:
- If the source object is a value type (i.e. most primitives), this function will directly compare them for structural equality, i.e. whether the value is the same, not the reference.
- If the source object is a reference type (i.e. objects and arrays), this function will compare them for referential equality, i.e. whether the reference is the same, not the value.
- If the source object implements
Equatable
, this function will callsource.equals()
and returntrue
if and only ifequals()
returnedtrue
for every other object.
As a result, this function can compare any type with one another.