A HashMap implementation in TypeScript, inspired by Rust's standard library.
This library supports custom hashing and equality, entry APIs, resizing,
efficient key-value operations, and full iterator support.
- Generic key-value store
- Customizable hashing via
Hasher<K> - Entry API (
Entry<K, T>) for fine-grained updates - Auto-resizing based on load factor
- Support for weak and hybrid hashing of object keys
- Full iterator support (
keys(),values(),entries()) - Map-level transformations (
map,retain,drain) - Optional chaining with
Optionsupport (via@sck/optres)
deno add @sck/hashmapimport { HashMap } from "@sck/hashmap";
const map = new HashMap<string, number>();
map.set("one", 1);
map.set("two", 2);
console.log(map.get("one")); // Some(1)
console.log(map.get("three")); // None
map.remove("two");-
set(key: K, value: T): voidInserts or updates the value for a given key. -
get(key: K): Option<T>Retrieves the value associated with the key. -
getKeyValue(key: K): Option<[K, T]>Retrieves both key and value, if present. -
has(key: K): booleanChecks if the map contains the key. -
remove(key: K): Option<T>Removes a key from the map and returns its value. -
clear(): thisClears all entries.
entry(key: K): Entry<K, T>Access an entry for mutation or conditional insert. Entries are "live" windows into the HashMap.
-
get(): Option<T>Returns the current value. -
insert(value: T): Option<T>Inserts a value and returns the old one if present. -
orInsert(value: T): Entry<K, T>Inserts the value if key is not present. -
orInsertWith(fn: (key: K) => T): Entry<K, T>Inserts using a function if key is not present. -
andModify(fn: (value: T) => T): Entry<K, T>Applies a function to the value if present. -
remove(): Option<T>Removes the entry and returns the value. -
isOccupied(): booleanReturns true if the entry is filled.
-
keys(): IterableIterator<K>Yields all keys. -
values(): IterableIterator<T>Yields all values. -
entries(): IterableIterator<Entry<K, T>>Yields all entries. -
[Symbol.iterator](): IterableIterator<[K, T]>Allowsfor...ofiteration.
-
reserve(count: number): voidEnsures enough capacity forcountelements. -
shrinkTo(capacity: number): voidShrinks capacity to the given value. -
shrinkToFit(): voidShrinks capacity to fit the current number of entries. -
capacity(): numberReturns the total bucket capacity. -
size(): numberReturns the number of stored entries. -
isEmpty(): booleanReturnstrueif map has no entries. -
buckets(): numberReturns number of internal buckets (may differ from size).
-
map(fn: (value: T, key: K) => T): voidTransforms all values in place. -
retain(fn: (key: K, value: T) => boolean): voidKeeps only entries that satisfy the predicate. -
drain(): IterableIterator<[K, T]>Empties the map and returns all entries.
Uses typeof key + ':' + key.toString() for string-based keys. Suitable for
primitives.
Creates a Hasher<object> that tracks object identity using WeakMap. Suitable
for use when keys are only objects.
Augments extensible objects with a hidden symbol property for performance. Falls back to a WeakMap if object is non-extensible.