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
lodash.isequal
Lodash's `isEqual` function provides deep equality checks for JavaScript objects. It is part of the larger Lodash utility library, which offers a wide range of functions for manipulating and inspecting data. Compared to @thi.ng/equiv, lodash.isequal is more widely used and comes with the extensive support and documentation of the Lodash library.
fast-deep-equal
fast-deep-equal is a small and fast library for deep equality checks. It is optimized for performance and has a very small footprint. While it offers similar functionality to @thi.ng/equiv, it may not handle as many edge cases or provide as much flexibility for custom equality checks.
deep-equal
deep-equal is another library for deep equality checks. It is simple to use and provides a straightforward API for comparing complex data structures. Compared to @thi.ng/equiv, deep-equal is more focused on simplicity and ease of use, but may not offer the same level of customization or performance.
@thi.ng/equiv
This project is part of the
@thi.ng/umbrella monorepo.
About
Deep equivalence checking for any data types. Supports:
- JS primitives
- arrays
- plain objects
- ES6 Sets / Maps
- Date
- RegExp
- types with
.equiv()
implementations
This feature was previously part of the
@thi.ng/api
package.
Installation
yarn add @thi.ng/equiv
Usage examples
import { equiv } from "@thi.ng/equiv";
equiv({a: {b: [1, 2]}}, {a: {b: [1, 2]}});
Authors
License
© 2018 Karsten Schmidt // Apache Software License 2.0