Skip to content

qsymmachus/valmap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A drop-in replacement for `Map` that considers object and array keys equal if they have the same value.

Resources

Stars

Watchers

Forks