Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
@thi.ng/equiv
Advanced tools
@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.
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
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 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 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.
This project is part of the @thi.ng/umbrella monorepo.
Extensible deep value equivalence checking for any data types.
Supports:
STABLE - used in production
Search or submit any issues for this package
yarn add @thi.ng/equiv
// ES module
<script type="module" src="https://unpkg.com/@thi.ng/equiv?module" crossorigin></script>
// UMD
<script src="https://unpkg.com/@thi.ng/equiv/lib/index.umd.js" crossorigin></script>
Package sizes (gzipped, pre-treeshake): ESM: 487 bytes / CJS: 543 bytes / UMD: 614 bytes
None
import { equiv } from "@thi.ng/equiv";
equiv(
{ a: { b: [1, 2] } },
{ a: { b: [1, 2] } }
);
// true
This is useful & required for custom types to take part in equiv
checks, by default only plain objects & array are traversed deeply.
Furthermore, by implementing this interface we can better control which
internal values / criteria are required to establish equivalence. In
this example we exclude the meta
property and only check for same type
& children
equality.
import { IEquiv } from "@thi.ng/api";
import { equiv } from "@thi.ng/equiv";
class Node implements IEquiv {
meta: any;
children: any[];
constructor(children: any[], meta?: any) {
this.children = children;
this.meta = meta;
}
equiv(o: any) {
return o instanceof Node && equiv(this.children, o.children);
}
}
equiv(new Node([1,2,3], "foo"), new Node([1,2,3], "bar"));
// true
Karsten Schmidt
If this project contributes to an academic publication, please cite it as:
@misc{thing-equiv,
title = "@thi.ng/equiv",
author = "Karsten Schmidt",
note = "https://thi.ng/equiv",
year = 2016
}
© 2016 - 2021 Karsten Schmidt // Apache Software License 2.0
FAQs
Extensible deep value equivalence checking for any data types
The npm package @thi.ng/equiv receives a total of 90,616 weekly downloads. As such, @thi.ng/equiv popularity was classified as popular.
We found that @thi.ng/equiv demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.