MultiBiMap
Bi-directional Multimap for nodejs
Allows you to map multiple keys and values with quick access whether searching by key or value.
Install
$ npm install --save multibimap
or
yarn add multibimap
Usage
Initialisation
const map = ; OR const map = iterableKey: false // default iterableValue: false // default;
Literals vs Iterables
Args passed in to add(k, v)
are by default treated as literal arguments. So if you pass an array (let's call it 'A') in as a value, it will be stored against the key as that array. Calling getKey(k)
will returns an array of values, with the original array 'A' as one element within the returned result.
If you want the array to be treated as a set of items to be stored separetly against the key, you can pass {iterableValue: true}
for the opt
argument. If you do this with an Array value (let's call it 'B'), then when you call getKey(k)
it will return an array of results, with all elements of 'B' as elements in the returned array.
If you pass {iterableValue: true}
as a constructor option it will become the default behaviour for the map. So you can set a default or override for individual calls to add(k, v)
.
The same holds true in reverse for the iterableKey
option.
add(k, v)
map;// adds a->b map
has(k, v)
map; map;// true map;// false map;// false
hasKey(k)
map; map;// true map;// false
hasVal(v)
map; map;// true map;// false
getKey(k)
map;map;// ['b'] map; map;// ['b', 'c'] map;// false
getVal(v)
map;map;// ['a'] map;map;// ['a', 'c'] map;// false
delete(k, v)
map;map;map; map; map// false map;// ['c'] map;// ['d'] map;map;// falsemap;// falsemap;// falsemap;// false map;// truemap;// ['b']
deleteKey(k)
map;map;map; map; map;// false map;// false map;// ['d'] map; map;// false
deleteVal(v)
map;map;map; map; map;// false map;// falsemap;// false map;// ['b']
Testing
$ npm test
Building
This project is written in es6 and uses babel to compile
$ npm build
Environments
This has only been tested in node 6.10.0
WARNINGS
Unexpected Values
This has not been tested for key or value arguments that could lead to unspecified behaviour. e.g. passing in undefined
, null
, NaN
etc will likely give strange behaviour. The underlying code uses a mirrored pair of es6 Map
objects, so the behaviour will be as expected with them (and remember that keys and values in multibimap terminology are each treated as both keys and values in es6 Map terminology.
Efficiency and Performance
This has not been benchmarked, so I make no efficiency or performance claims. I am open to suggestions for optimisations.
License
MIT © Alastair Brayne All rights reserved.