npm.io
2.0.3 • Published 6d ago

@guanghechen/equal

Licence
MIT
Version
2.0.3
Deps
0
Size
17 kB
Vulns
0
Weekly
0

@guanghechen/equal


The fastest deep equal with ES6 Map, Set and Typed arrays support. Inspired by fast-deep-equal, re-published with ESM support.

Install

  • npm

    npm install --save @guanghechen/equal
  • yarn

    yarn add @guanghechen/equal

Usage

Basic Comparison
import isEqual from '@guanghechen/equal'

// Primitives
isEqual(1, 1)           // true
isEqual('a', 'a')       // true
isEqual(null, null)     // true
isEqual(undefined, undefined) // true

// Objects
isEqual({ a: 1, b: 2 }, { a: 1, b: 2 })  // true
isEqual({ a: 1 }, { a: 2 })              // false

// Arrays
isEqual([1, 2, 3], [1, 2, 3])  // true
isEqual([1, 2], [1, 2, 3])     // false

// Nested structures
isEqual(
  { user: { name: 'John', tags: ['admin'] } },
  { user: { name: 'John', tags: ['admin'] } }
) // true
Named Export
import { isEqual } from '@guanghechen/equal'

isEqual({ foo: 'bar' }, { foo: 'bar' }) // true
Special Types
import isEqual from '@guanghechen/equal'

// RegExp
isEqual(/abc/gi, /abc/gi)  // true
isEqual(/abc/g, /abc/i)    // false

// Date
isEqual(new Date('2024-01-01'), new Date('2024-01-01'))  // true

// Map (keys matched by identity, values compared deeply)
isEqual(new Map([['a', 1]]), new Map([['a', 1]]))  // true
isEqual(new Map([['a', 1]]), new Map([['a', 2]]))  // false

// Set (elements matched by identity)
isEqual(new Set([1, 2, 3]), new Set([3, 2, 1]))  // true
isEqual(new Set([1, 2]), new Set([1, 2, 3]))     // false

// Typed arrays (element-wise)
isEqual(new Uint8Array([1, 2, 3]), new Uint8Array([1, 2, 3]))  // true
isEqual(new Uint8Array([1, 2, 3]), new Uint8Array([1, 2, 4]))  // false

// Custom valueOf/toString
class Point {
  constructor(public x: number, public y: number) {}
  valueOf() { return this.x * 1000 + this.y }
}
isEqual(new Point(1, 2), new Point(1, 2))  // true

Reference

Keywords