https://github.com/leawind/bimap-ts
A TypeScript library provides a bi-directional map (BiMap) implementation
https://github.com/leawind/bimap-ts
bimap deno jsr lib typescript
Last synced: about 2 months ago
JSON representation
A TypeScript library provides a bi-directional map (BiMap) implementation
- Host: GitHub
- URL: https://github.com/leawind/bimap-ts
- Owner: Leawind
- License: gpl-3.0
- Created: 2025-03-26T04:03:24.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2025-03-26T04:19:00.000Z (about 2 months ago)
- Last Synced: 2025-03-26T05:19:58.304Z (about 2 months ago)
- Topics: bimap, deno, jsr, lib, typescript
- Language: TypeScript
- Homepage: https://jsr.io/@leawind/bimap
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# BiMap
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.
## Features
- 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.## Usage
### Import
```typescript
import { BiMap } from '@leawind/bimap';
```### Create a BiMap
You can create a BiMap using various input types:
```typescript
const bimap1: BiMap = BiMap.from(
new Map([['one', 1], ['two', 2]]),
);
const bimap2: BiMap = BiMap.from([['one', 1], ['two', 2]]);
const bimap3: BiMap = BiMap.from({ one: 1, two: 2 });
```### Add Key-Value Pairs
```typescript
const bimap = new BiMap();
bimap.set('one', 1);
bimap.set('two', 2);
```### Retrieve Values and Keys
```typescript
const value: number = bimap.getValue('one'); // 1
const key: string = bimap.getKey(2); // 'two'
```### Delete Key-Value Pairs
```typescript
bimap.deleteKey('one');
bimap.deleteValue(2);
```### Iterate Over Keys, Values, and Entries
```typescript
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);
}
```### Clone a BiMap
```typescript
const clone = bimap.clone();
```