or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash--isequal

Deep equality comparison function for JavaScript values

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.isequal@3.0.x

To install, run

npx @tessl/cli install tessl/npm-lodash--isequal@3.0.0

index.mddocs/

lodash.isequal

Deep equality comparison function for JavaScript values. Performs comprehensive comparisons between two values to determine if they are equivalent, supporting arrays, booleans, Date objects, numbers, Object objects, regexes, and strings.

Package Information

  • Package Name: lodash.isequal
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.isequal

Core Imports

const isEqual = require('lodash.isequal');

For ES modules (if supported by your environment):

import isEqual from 'lodash.isequal';

Basic Usage

const isEqual = require('lodash.isequal');

// Basic object comparison
const object = { 'user': 'fred' };
const other = { 'user': 'fred' };

object == other;
// => false

isEqual(object, other);
// => true

// Array comparison
const arr1 = [1, 2, 3];
const arr2 = [1, 2, 3];
isEqual(arr1, arr2);
// => true

// Deep nested comparison
const complex1 = { 
  users: [{ name: 'alice', data: { active: true } }],
  count: 1 
};
const complex2 = { 
  users: [{ name: 'alice', data: { active: true } }],
  count: 1 
};
isEqual(complex1, complex2);
// => true

Capabilities

Deep Equality Comparison

Performs a deep comparison between two values to determine if they are equivalent.

/**
 * Performs a deep comparison between two values to determine if they are
 * equivalent. Supports circular reference detection and custom comparison logic.
 * If customizer is provided it's invoked to compare values. If customizer returns
 * undefined comparisons are handled by the method instead. The customizer is bound
 * to thisArg and invoked with up to three arguments: (value, other [, index|key]).
 *
 * @param {*} value The value to compare.
 * @param {*} other The other value to compare.
 * @param {Function} [customizer] The function to customize value comparisons.
 * @param {*} [thisArg] The this binding of customizer.
 * @returns {boolean} Returns true if the values are equivalent, else false.
 */
function isEqual(value, other, customizer, thisArg);

Supported Types:

  • Arrays (including nested arrays and typed arrays)
  • Booleans
  • Date objects
  • Error objects (compared by name and message properties)
  • Numbers (with special handling for NaN, +0/-0)
  • Object objects (compared by own enumerable properties, not inherited)
  • Regular expressions
  • Strings
  • Arguments objects (treated as regular objects)

Special Behaviors:

  • Circular References: Automatically detected and handled to prevent infinite recursion
  • NaN Comparison: NaN values are considered equal to each other
  • Zero Comparison: Distinguishes between +0 and -0 (they are not equal)
  • Wrapped Objects: Supports lodash wrapped objects via __wrapped__ property

Not Supported (without custom comparator):

  • Functions
  • DOM nodes

Usage Examples:

const isEqual = require('lodash.isequal');

// Different data types
isEqual(1, 1);                    // => true
isEqual('hello', 'hello');        // => true
isEqual(true, true);              // => true
isEqual([1, 2], [1, 2]);          // => true
isEqual({ a: 1 }, { a: 1 });      // => true

// Date objects
const date1 = new Date('2023-01-01');
const date2 = new Date('2023-01-01');
isEqual(date1, date2);            // => true

// Regular expressions
isEqual(/abc/g, /abc/g);          // => true

// Special number handling
isEqual(NaN, NaN);                // => true (special case)
isEqual(+0, -0);                  // => false (distinguishes zero signs)

// Error objects  
const err1 = new Error('test');
const err2 = new Error('test');
isEqual(err1, err2);              // => true (compared by name and message)

// Custom comparator
const array = ['hello', 'goodbye'];
const other = ['hi', 'goodbye'];

isEqual(array, other, function(value, other) {
  if (RegExp.prototype.test.call(/^h(?:i|ello)$/, value) && 
      RegExp.prototype.test.call(/^h(?:i|ello)$/, other)) {
    return true;
  }
});
// => true

Custom Comparison Logic

The optional customizer function allows extending comparison support for additional value types and custom comparison logic.

Customizer Function Signature:

  • Parameters:
    • value - The value from the first object being compared
    • other - The value from the second object being compared
    • index|key (optional) - The index (for arrays) or key (for objects) being compared
  • Returns:
    • boolean - For definitive custom comparison result
    • undefined - To defer to default lodash comparison logic
  • Binding: Bound to thisArg if provided (uses thisArg as the this context)
  • Invocation: Called for every value pair during deep comparison traversal

Example with custom comparator:

function customEqual(a, b) {
  return isEqual(a, b, function(objValue, othValue) {
    // Custom logic for comparing special objects
    if (objValue && objValue.isSpecialType) {
      return objValue.id === othValue.id;
    }
    // Return undefined to use default comparison
  });
}

const obj1 = { isSpecialType: true, id: 'abc', data: 'different' };
const obj2 = { isSpecialType: true, id: 'abc', data: 'values' };
customEqual(obj1, obj2); // => true (compared by id only)

Circular Reference Handling

The isEqual function automatically detects and handles circular references in objects and arrays to prevent infinite recursion:

const isEqual = require('lodash.isequal');

// Create objects with circular references
const obj1 = { name: 'test' };
obj1.self = obj1;

const obj2 = { name: 'test' };
obj2.self = obj2;

isEqual(obj1, obj2); // => true

// Works with arrays too
const arr1 = [1, 2];
arr1[2] = arr1;

const arr2 = [1, 2];
arr2[2] = arr2;

isEqual(arr1, arr2); // => true

// Mixed circular references
const complex1 = { data: { users: [] } };
complex1.data.users.push(complex1);

const complex2 = { data: { users: [] } };
complex2.data.users.push(complex2);

isEqual(complex1, complex2); // => true

Error Handling

The isEqual function itself does not throw errors and will return false for comparisons that cannot be performed. However, custom comparator functions may throw errors if they encounter unexpected input:

const isEqual = require('lodash.isequal');

// Function handles type mismatches gracefully
isEqual(null, undefined);     // => false
isEqual({}, null);           // => false
isEqual(1, "1");             // => false

// Custom comparator errors are not caught
try {
  isEqual(obj1, obj2, function() {
    throw new Error('Custom error');
  });
} catch (error) {
  console.log('Caught:', error.message); // => 'Caught: Custom error'
}