reversible-map
A
Map
extended call with support of reversing keys.
One ReversibleMap
object consider the use of two values as keys.
You can access to a key data using the other key.
This documentation will use TypeScript typing. Remove any type to restore "pure" JavaScript.
Getting started
// With a standard Map;classic_map.set'classic', 'map'; // <type_key1, type_key2>;reversible_map.set'one', 2; // Key order is not importantreversible_map.set3, 'four'; reversible_map.get3; // => 'four'reversible_map.get'four'; // => 3reversible_map.get2; // => 'one'
Keys can be of any type (like in the Map
object).
The number of keys allowed is unlimited and their order is not relevant.
To install the package, use:
$ npm install reversible-map
Usage
Main methods are get
, set
and has
.
Instanciation
; ;// You can instanciate with an entries array;
Set values
map.set"one", 3;map.set"four", 5;map.set"six", 8;// Setters are chainablemap.set"nine", 11.set"twelve", 14;
Get values
map.get"one"; // => 3map.get"four"; // => 5map.get"six"; // => 8
Iteration
Iteration through .entries()
, Symbol.iterator
, .keysUnique()
ensure that getting a value with those methods will not give you duplicates.
.keys()
give you all the keys, including the reverse of a key previously iterated.
/// Iterate through the map;// Using Symbol.iterator | .entries()for of map
Appartenance
// Test if one key existsmap.has"one"; // => truemap.has"hello"; // => false
Delete values
/// Delete valuesmap.delete"one"; // Deletions are also chainable.