valmap
v1.1.0
Published
ValueMap is an extension of the built-in Map type that keys objects and arrays by value instead of by reference.
Downloads
3
Readme
valmap
ValueMap
is a drop-in replacement for Map
that considers object and array keys equal if they have the same value.
Examples
A ValueMap
has the same interface as ordinary an Map
:
import { ValueMap } from 'valmap';
const valueMap = new ValueMap();
valueMap.set('wow', 'neat');
valueMap.get('wow'); // => 'neat'
It differs from an ordinary Map
in one important way – when you use an object or array as a key, key equality will be based on its value:
valueMap.set({ foo: 'bar' }, 'this is stored with an object key');
valueMap.get({ foo: 'bar' }); // => 'this is stored with an object key';
Object and array keys will be considered equal if they contain the same data, regardless of property or element order.
Constrast this behavior with an ordinary map, where object and array keys are only considered equal if they have the same reference:
const ordinaryMap = new Map();
ordinaryMap.set({ foo: 'bar' }, 'object key in ordinary map');
ordinaryMap.get({ foo: 'bar' }); // => undefined
Installation
Depending on your preferred package manager:
npm install valmap
yarn add valmap