equal-array
equal-array lets the strict equality operator ===
compare arrays by values instead of references. Comparison can be either shallow or deeper.
Purpose
It is mainly useful with an es6 Map or Set, because arrays, used as keys, are compared by references. For example :
const map = ;map;map; // the map will consider [1, 2 ,3] as a new keymapsize; // returns 2 - with equal-array: returns 1map; // returns false - with equal-array: returns true
Installation
npm install equal-array
Usage
;// ES5: var EqualArray = require('equal-array').default; const eq = ;// eq is a function === ; //returns true const map = ;map;map;mapsize; // returns 1map; // returns true
Options
const eq = returnArray: false conversion: true
returnArray
accepted values: false
(default) or true
If returnArray
is true
, the eq
function returns a cloned array, otherwise a unique integer. The default is false
to maximize performances.
conversion
accepted values: true
(default), false
, or a callback
Apply a conversion to the array elements in order to make the comparison. This is important if the array contains objects.
If conversion
is false
then :
!==
If conversion
is true
then :
===
Setting conversion
to true
means that EqualArray
will call the valueOf()
function on each element (when applicable).
The conversion
option also accepts a callback which takes as a parameter an element of an array and returns the value to be compared:
const obj1 = dummy: 1;const obj2 = dummy: 1;const obj3 = dummy: 99;const callback = elementdummy;const eq = conversion: callback; === ; // true === ; // false