🏃
Ubimap
Ubimap
is a safe, typed, enumerable bidirectional map that ensures unique values and supports compound keys.
npm install ubimap # or yarn add ubimap
const { UbiMap } = require('ubimap');
import { UbiMap } from 'ubimap';
<script
crossorigin
src="https://cdn.jsdelivr.net/npm/ubimap@latest/dist/index.umd.js"
></script>
const { UbiMap } = window.ubimap;
import { UbiMap } from 'https://cdn.skypack.dev/ubimap@latest';
UbiMap
UbiMap
is a safe, bidirectional map that allows you to store and retrieve values based
on compound keys. It ensures that both keys and values are unique and provides methods to
query the map in both directions. This guide will walk you through how to set up and use
UbiMap
.
UbiMap
First, import the UbiMap
class into your TypeScript file:
import { UbiMap } from 'ubimap';
To create an instance of UbiMap
, simply pass the key structure as a tuple type, and
optionally, the value type and separator:
const ubimap = new UbiMap<[string, string]>();
K
is the tuple representing the compound key.V
is the type of the values (default is string
).S
is the separator used to join the key components (default is ' '
).You can set key-value pairs using the set
method:
ubimap.set('a', 'b', 'value1'); // Key: 'a b', Value: 'value1'
ubimap.set('c', 'd', 'value2'); // Key: 'c d', Value: 'value2'
Both keys and values must be unique. If you try to add a duplicate key or value, an error will be thrown:
ubimap.set('a', 'b', 'value1'); // This will throw an error if 'a b' already exists.
You can retrieve values by using the get
method, providing the components of the key as
separate arguments:
const value = ubimap.get('a', 'b'); // Returns 'value1'
If you want to find the key associated with a value, you can use the getKey
method:
const key = ubimap.getKey('value1'); // Returns 'a b'
You can list keys or values that match a given prefix using keys
and values
:
// List keys with a prefix 'a'
const keysWithPrefix = ubimap.keys('a'); // ['a b']
// List values with a prefix 'value'
const valuesWithPrefix = ubimap.values('value'); // ['value1', 'value2']
You can also iterate over all key-value pairs using a for...of
loop:
for (const [key, value] of ubimap) {
console.log(key, value);
}
This will log each key-value pair in the map.
import { UbiMap } from 'ubimap';
// Create a new UbiMap with compound keys and string values
const ubimap = new UbiMap<[string, string]>();
// Add some key-value pairs
ubimap.set('a', 'b', 'value1');
ubimap.set('c', 'd', 'value2');
// Retrieve a value by key
const value = ubimap.get('a', 'b'); // 'value1'
// Retrieve the key for a value
const key = ubimap.getKey('value2'); // 'c d'
// List all keys with the prefix 'a'
const keysWithPrefix = ubimap.keys('a'); // ['a b']
// Iterate through all entries
for (const [key, value] of ubimap) {
console.log(key, value);
}
If you try to add a duplicate key or value, an error can be thrown. For example:
try {
ubimap.set('a', 'b', 'value1');
ubimap.set('a', 'b', 'value2');
} catch (error) {
if (error instanceof KeyAlreadyExistsError) {
console.error(error.message);
}
if (error instanceof ValueAlreadyExistsError) {
console.error(error.message);
}
}
Licensed under the MIT. See LICENSE
for more informations.