What is deep-equal-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.
What are deep-equal-in-any-order's main functionalities?
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);
Other packages similar to deep-equal-in-any-order
deep-equal
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.
lodash.isequal
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.
fast-deep-equal
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.
deep-equal-in-any-order
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...).
install
npm i --save deep-equal-in-any-order
or
yarn add deep-equal-in-any-order
usage
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] }] } });