hash-set
The original Set uses Same-value-zero equality.
Use this package if you need custom comparison behavior.
Install
$ npm install --save hash-set
Usage
; // Create Set class which compares objects with JSON.stringifyconst JSONSet = ;// Create instance of JSONSetconst mySet = ; mySet;mySet; mySet; // truemySet; // truemySet; // false, `{ c: 3 }` has not been added to the set mySetsize; // 2 mySet; // removes `{ a: 1 }` from the setmySet; // false, `{ a: 1 }` has been removed mySetsize; // 1
API
hashSet(hashFn)
Returns Set
class with custom equality comparisons.
hashFn
Type: function
The function to determine the unique of value.
HashSet
executes a provided function every time you call add(value)
, has(value)
, delete(value)
.
The result of hashFn(value)
will be used for comparison with the values of HashSet
. For comparison will be used Same-value-zero.
Example
const mySet = ; mySet; // value has been added to the setmySet; // value has been added to the set // because `Object.is(1, '1')` is `false` console; // Set { 1, '1' }
; { return value;} const StringSet = ;const mySet = ; mySet; // value has been added to the setmySet; // value has not been added to the set // because `Object.is(hashFn(1), hashFn('1'))` is `true` console; // Set { 1 }
License
MIT © Andrew Abramov