import { equals } from "https://deno.land/x/ayonli_jsext@v0.9.72/object/index.ts";
Performs a deep comparison between two values to see if they are equivalent or contain the same data.
- Primitive values, functions and circular references are compared using the
===
operator or theObject.is
function. - Object wrappers such as
String
,Number
andBoolean
are compared both by their primitive values and their constructors. - Objects should have the same
constructor
in order to be considered potentially equal, unless they implement the Comparable interface. - Plain objects are compared by their own enumerable properties, unless they implement the Comparable interface.
- Arrays are compared one by one by their items.
- ArrayBuffers and views are compared by their underlying buffers.
- Map and Set items are compared unordered.
- RegExp
source
,flags
andlastIndex
properties are compared. - Error
name
,message
,stack
,cause
, anderrors
properties are always compared, if there are other enumerable properties, they are compared as well. - URL and URLSearchParams objects are compared by their string representations.
- Headers and FormData are compared by their entries unordered.
- Objects that implements the Comparable interface are compared using
the
compareTo
method. - Objects whose
valueOf
method returns primitive values other thanNaN
are also supported and use their primitive values for comparison. - In others cases, values are compared using the
===
operator.
NOTE: This function returns true
if the two values are both NaN
.
Examples
Example 1
Example 1
import { equals } from "@ayonli/jsext/object";
console.log(equals("Hello", "Hello")); // true
console.log(equals(42, 42)); // true
console.log(equals([1, 2, 3], [1, 2, 3])); // true
console.log(equals({ foo: "bar" }, { foo: "bar" })); // true
// deep comparison
console.log(equals(
{ foo: { bar: "baz" } },
{ foo: { bar: "baz" } }
)); // true