🧬 Performs a deep comparison between two values to determine if they are equivalent.
The following types are supported for deep comparison:
- Primitive types (String, Number, Boolean, BigInt, Symbol, null, undefined)
- Plain Objects
- Arrays
- Maps & Sets (Order matters for Maps and Sets)
- Dates
- RegExps
- Errors
- ArrayBuffers & TypedArrays
- DataViews
✅
Circular References: Correctly handles circular references.
Other Types: Checked by reference (e.g., Functions).
Syntax
import { isEql } from '@opentf/std';
isEql(
val1: unknown,
val2: unknown,
options?: { shallow?: boolean }
): booleanParameters
val1: The first value to compare.val2: The second value to compare.options:shallow: Iftrue, performs a shallow comparison instead of deep.
Returns
true if the values are equivalent, false otherwise.
Examples
isEql({a: 1, b: 2}, {a: 1, b: 2}) //=> true
const mapA = new Map([
['a', 1],
['b', 2],
]);
const mapB = new Map([
['b', 2],
['a', 1],
]);
isEql(mapA, mapB); //=> false (Order matters in Maps)
const re = new RegExp('ab+c');
const re2 = new RegExp('ab+d');
isEql(re, re2); //=> false
const ta1 = new Uint8Array([42, 43]);
const ta2 = new Uint8Array([42, 43]);
const ta3 = new Uint8Array([42, 45]);
isEql(ta1, ta2); //=> true
isEql(ta2, ta3); //=> false
isEql({a: {b: 1}}, {a: {b: 1}}, {shallow: true}) //=> false