1. muggle-deep-equal
A simple and generic implementation of deep equal
muggle-deep-equal
Package: muggle-deep-equal
Created by: kayleepop
Last modified: Tue, 10 May 2022 00:03:18 GMT
Version: 1.0.2
License: MIT
Downloads: 12
Repository: https://github.com/kayleepop/muggle-deep-equal

Install

npm install muggle-deep-equal
yarn add muggle-deep-equal

muggle-deep-equal

Greenkeeper badge Travis badge standard badge npm

A simple and generic implementation of deep equal using Iterables.

Used in muggle-assert, the assertion library for muggle

Goals

  • Generic and predictable behavior
  • Simple and readable source code
  • Fully tested

Installation

$ npm install muggle-deep-equal

Example

 const deepEqual = require('muggle-deep-equal')

// primitives are compared with ===
deepEqual('penguin', 'penguin') // returns true
deepEqual(100, 50) // returns false
deepEqual(1, true) // returns false

deepEqual([1, 2, 3, 4], [1, 2, 3, 4]) // returns true

deepEqual(
  {
    array: [1, 2, 3],
    object: {
      animal: 'penguin'
    }
  },
  {
    array: [1, 2, 3],
    object: {
      animal: 'penguin'
    }
  }
) // returns true

// if either string or array was different, deepEqual would return false

What's deep equal?

If two objects are deeply equal, then their actual data are equal rather than just their reference. It's useful for comparing separate instances of arrays and objects for example.

In muggle-deep-equal, equality is determined by these rules (in order):

1. If either value is a primitive, then equality is determined using strict equality ===

  • String, Number, Boolean, Function, Symbol, undefined, or null
  • NaN is considered equal to NaN

2. Both objects must have the same class.

  • object1.constructor.name === object2.constructor.name

3. If either object is an Iterable, then equality is determined by checking that both contain the same values in the same order.

  • Values are compared by applying these deep equal rules recursively.
  • Every index is compared 1 at a time in order using the iterator protocol
  • Rules #4 and #5 aren't applied to iterables

4. Both objects must have the same properties and values.

  • Compared by applying these deep equal rules recursively on every value using a for...in loop

5. Both objects must return the same string representation from object.toString()

  • This allows many other objects to be compared as expected such as Error, Date, and RegExp

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code