
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
deep-equal-in-any-order
Advanced tools
chai plugin to match objects and arrays deep equality with arrays (including nested ones) being in any order
The 'deep-equal-in-any-order' npm package is a utility for performing deep equality checks on JavaScript objects, arrays, and other data structures, with the added capability of ignoring the order of elements in arrays and objects. This is particularly useful for testing and validation scenarios where the order of elements should not affect the equality check.
Deep Equality Check Ignoring Order
This feature allows you to perform deep equality checks on objects and arrays while ignoring the order of elements. The code sample demonstrates how to use the package with Chai for assertions.
const deepEqualInAnyOrder = require('deep-equal-in-any-order');
const chai = require('chai');
chai.use(deepEqualInAnyOrder);
const { expect } = chai;
const obj1 = { a: 1, b: [2, 3] };
const obj2 = { b: [3, 2], a: 1 };
expect(obj1).to.deep.equalInAnyOrder(obj2);
Array Equality Ignoring Order
This feature allows you to check the equality of arrays without considering the order of elements. The code sample shows how to use the package to assert that two arrays with the same elements in different orders are equal.
const deepEqualInAnyOrder = require('deep-equal-in-any-order');
const chai = require('chai');
chai.use(deepEqualInAnyOrder);
const { expect } = chai;
const arr1 = [1, 2, 3];
const arr2 = [3, 2, 1];
expect(arr1).to.deep.equalInAnyOrder(arr2);
Object Equality Ignoring Order
This feature allows you to check the equality of objects without considering the order of their properties. The code sample demonstrates how to use the package to assert that two objects with the same properties in different orders are equal.
const deepEqualInAnyOrder = require('deep-equal-in-any-order');
const chai = require('chai');
chai.use(deepEqualInAnyOrder);
const { expect } = chai;
const obj1 = { a: 1, b: 2, c: 3 };
const obj2 = { c: 3, b: 2, a: 1 };
expect(obj1).to.deep.equalInAnyOrder(obj2);
The 'deep-equal' package provides deep equality checks for JavaScript objects and arrays. Unlike 'deep-equal-in-any-order', it does not ignore the order of elements in arrays or objects. It is useful when the order of elements is significant in your equality checks.
The 'lodash.isequal' function from the Lodash library performs deep equality checks on objects and arrays. Similar to 'deep-equal', it does not ignore the order of elements. It is a part of the larger Lodash utility library, which provides a wide range of utility functions for JavaScript.
The 'fast-deep-equal' package is a highly performant deep equality check library for JavaScript. It is optimized for speed and is often used in performance-critical applications. Like 'deep-equal' and 'lodash.isequal', it does not ignore the order of elements.
Chai plugin to match objects and arrays deep equality with arrays (including nested ones) being in any order.
It works in similar way as deep.equal
but it doesn't checks the arrays order (at any level of nested objects and arrays). The array elements can be any JS entity (boolean, null, number, string, object, array...).
npm i --save deep-equal-in-any-order
or
yarn add deep-equal-in-any-order
expect
const deepEqualInAnyOrder = require('deep-equal-in-any-order');
const chai = require('chai');
chai.use(deepEqualInAnyOrder);
const { expect } = chai;
expect([1, 2]).to.deep.equalInAnyOrder([2, 1]);
expect([1, 2]).to.not.deep.equalInAnyOrder([2, 1, 3]);
expect({ foo: [1, 2], bar: [4, 89, 22] }).to.deep.equalInAnyOrder({ foo: [2, 1], bar: [4, 22, 89] });
expect({ foo: ['foo-1', 'foo-2', [1, 2], null ] }).to.deep.equalInAnyOrder({ foo: [null, [1, 2], 'foo-1', 'foo-2'] });
expect({ foo: [1, 2], bar: { baz: ['a', 'b', { lorem: [5, 6] }] } }).to.deep.equalInAnyOrder({ foo: [2, 1], bar: { baz: ['b', 'a', { lorem: [6, 5] }] } });
assert
const deepEqualInAnyOrder = require('deep-equal-in-any-order');
const chai = require('chai');
chai.use(deepEqualInAnyOrder);
const { assert } = chai;
assert.deepEqualInAnyOrder([1, 2], [2, 1]);
assert.notDeepEqualInAnyOrder(1, 2], [2, 1, 3]);
assert.deepEqualInAnyOrder({ foo: [1, 2], bar: [4, 89, 22] }, { foo: [2, 1], bar: [4, 22, 89] });
assert.deepEqualInAnyOrder({ foo: ['foo-1', 'foo-2', [1, 2], null ] }, { foo: [null, [1, 2], 'foo-1', 'foo-2'] });
assert.deepEqualInAnyOrder({ foo: [1, 2], bar: { baz: ['a', 'b', { lorem: [5, 6] }] } }, { foo: [2, 1], bar: { baz: ['b', 'a', { lorem: [6, 5] }] } });
FAQs
chai plugin to match objects and arrays deep equality with arrays (including nested ones) being in any order
We found that deep-equal-in-any-order demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.