Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@thi.ng/equiv

Package Overview
Dependencies
Maintainers
0
Versions
140
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@thi.ng/equiv

Extensible deep value equivalence checking for any data types

  • 2.1.69
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
143K
increased by3.65%
Maintainers
0
Weekly downloads
 
Created

What is @thi.ng/equiv?

@thi.ng/equiv is a lightweight JavaScript library for deep equality checks. It provides a robust and efficient way to compare complex data structures, including arrays, objects, sets, maps, and more. The library is designed to handle circular references and custom equality checks, making it a versatile tool for various use cases.

What are @thi.ng/equiv's main functionalities?

Basic Equality Check

This feature allows you to perform basic equality checks between primitive values. The function returns true if the values are equal and false otherwise.

const equiv = require('@thi.ng/equiv');

console.log(equiv(1, 1)); // true
console.log(equiv(1, '1')); // false

Deep Equality Check for Arrays

This feature allows you to perform deep equality checks on arrays. It compares the elements of the arrays recursively to determine if they are equal.

const equiv = require('@thi.ng/equiv');

const arr1 = [1, 2, 3];
const arr2 = [1, 2, 3];
const arr3 = [1, 2, 4];

console.log(equiv(arr1, arr2)); // true
console.log(equiv(arr1, arr3)); // false

Deep Equality Check for Objects

This feature allows you to perform deep equality checks on objects. It compares the properties of the objects recursively to determine if they are equal.

const equiv = require('@thi.ng/equiv');

const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 2 } };
const obj3 = { a: 1, b: { c: 3 } };

console.log(equiv(obj1, obj2)); // true
console.log(equiv(obj1, obj3)); // false

Handling Circular References

This feature allows you to handle circular references in objects. The library can detect and correctly compare objects with circular references.

const equiv = require('@thi.ng/equiv');

const a = {};
const b = {};
a.self = a;
b.self = b;

console.log(equiv(a, b)); // true

Custom Equality Checks

This feature allows you to define custom equality checks for your own classes or data structures. By implementing the [Symbol.for('equiv')] method, you can control how instances of your class are compared.

const equiv = require('@thi.ng/equiv');

class Custom {
  constructor(value) {
    this.value = value;
  }

  [Symbol.for('equiv')](other) {
    return other instanceof Custom && this.value === other.value;
  }
}

const obj1 = new Custom(1);
const obj2 = new Custom(1);
const obj3 = new Custom(2);

console.log(equiv(obj1, obj2)); // true
console.log(equiv(obj1, obj3)); // false

Other packages similar to @thi.ng/equiv

Keywords

FAQs

Package last updated on 24 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc