A TypeScript library provides a bi-directional map (BiMap) implementation, allowing efficient lookups by both keys and values. This library ensures that both keys and values are unique, maintaining an inverse mapping from values to keys.
- Efficient lookup by both keys and values.
- Unique keys and values.
- Methods for adding, deleting, and retrieving key-value pairs.
- Iterators for keys, values, and entries.
import { BiMap } from '@leawind/bimap';You can create a BiMap using various input types:
const bimap1: BiMap<string, number> = BiMap.from(
new Map([['one', 1], ['two', 2]]),
);
const bimap2: BiMap<string, number> = BiMap.from([['one', 1], ['two', 2]]);
const bimap3: BiMap<string, number> = BiMap.from({ one: 1, two: 2 });const bimap = new BiMap<string, number>();
bimap.set('one', 1);
bimap.set('two', 2);const value: number = bimap.getValue('one'); // 1
const key: string = bimap.getKey(2); // 'two'bimap.deleteKey('one');
bimap.deleteValue(2);for (const key of bimap.keys()) {
console.log(key);
}
for (const value of bimap.values()) {
console.log(value);
}
for (const [key, value] of bimap.entries()) {
console.log(key, value);
}const clone = bimap.clone();