@psenger/bidirectional-map
v1.0.8
Published
Bi Directional Map ( BIDI Map ) for NodeJS
Maintainers
Readme
@psenger/bidirectional-map
Bidirectional key-value lookup — set once, look up from either side in O(1).
Features • Installation • Quick Start • API • Contributing
A standard JavaScript Map only goes one direction. BiDirectionalMap registers both
A→B and B→A in a single set() call — so get(A) returns B and get(B) returns
A without managing a second structure yourself. It extends the native Map, so every
method you already use (get, has, delete, forEach, iteration) works without
modification. Zero runtime dependencies.
Features
- One call, both directions.
map.set(key, value)registers the forward and reverse mapping automatically — no secondset()needed. - Consistent pairs. Assigning a key that already participates in a relationship removes the stale pair first, keeping the map internally consistent.
- Full
Mapcompatibility. Extends the built-inMap— spread, destructuring,entries(),keys(),values(), andsizeall work as-is.
Installation
npm install @psenger/bidirectional-map--saveor
yarn add @psenger/bidirectional-mapExample Usage
const bidiMap = new BiDirectionalMap();
bidiMap.set(1, 2).set(3, 4);
console.log (`Key(1) = Value(${bidiMap.get(1)})` );
console.log (`Key(2) = Value(${bidiMap.get(2)})` );
console.log (`Key(3) = Value(${bidiMap.get(3)})` );
console.log (`Key(4) = Value(${bidiMap.get(4)})` );
// Key(1) = Value(2)
// Key(2) = Value(1)
// Key(3) = Value(4)
// Key(4) = Value(3)
for( let [Key,Value] of bidiMap.entries()){
console.log(`Key(${Key}) = Value(${Value})`);
}
// Key(1) = Value(2)
// Key(2) = Value(1)
// Key(3) = Value(4)
// Key(4) = Value(3)API
BiDirectionalMap
{BiDirectionalMap}
Kind: global class
See: wikipedia for further information
Version: 1.0.6
- BiDirectionalMap
- new BiDirectionalMap([props])
- instance
- static
- .Symbol.species ⇒ MapConstructor
new BiDirectionalMap([props])
| Param | Type | Description | | --- | --- | --- | | [props] | Iterable | An Iterable object whose elements are key-value pairs (arrays with two elements, e.g. [[ 1, ['one','two'] ],[ 2, ['three'] ]]). Each key-value pair is added to the new Map; null values are treated as undefined. |
biDirectionalMap.set(key, value) ⇒ BiDirectionalMap
Sets the value for the key in the Map object. Overriding the parent set and returns the Map
object. Only works with String or `Numbers.
Kind: instance method of BiDirectionalMap
| Param | Type | Description |
| --- | --- | --- |
| key | string | number | The key of the element to add to the BiDirectionalMap object. |
| value | string | number | The value of the element to add to the BiDirectionalMap object. |
BiDirectionalMap.Symbol.species ⇒ MapConstructor
Overwrite BiDirectionalMap species to the parent Map constructor
Kind: static property of BiDirectionalMap
License
MIT License
Copyright (c) 2021 Philip A Senger
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
A minimal, zero-dependency bidirectional Map for JavaScript and Node.js.
