A roblox-ts `Map` wrapper that automatically creates default values for missing keys.
npm install @rbxts/default-mapA roblox-ts Map wrapper that automatically creates default values for missing keys.
- Automatic default values: When accessing a non-existent key, a default value is created and stored
- Type-safe: Full TypeScript generics support for keys and values
- Standard Map API: Implements familiar Map methods like get, set, delete, clear, forEach
- Always returns a value: The has() method always returns true since any key can be accessed
``bash`
npm install @rbxts/default-map
`typescript
import { DefaultMap } from "@rbxts/default-map";
// Create a DefaultMap with a default value creator
const counterMap = new DefaultMap(() => 0);
// Accessing a non-existent key creates and returns the default value
counterMap.get("apples"); // Returns 0, creates entry for "apples"
// Increment the counter
counterMap.set("apples", counterMap.get("apples") + 1);
counterMap.get("apples"); // Returns 1
// Works with complex default values
const listMap = new DefaultMap(() => new Array
listMap.get("fruits").push("apple");
listMap.get("fruits").push("banana");
`
`typescript`
new DefaultMap
- defaultValueCreator: Function that creates a new default value when neededentries
- : Optional initial key-value pairs
| Method | Description |
|--------|-------------|
| get(key: K): V | Returns the value for key, creating a default if missing |set(key: K, value: V): this
| | Sets the value for key |delete(key: K): boolean
| | Removes the entry for key |clear(): void
| | Removes all entries |has(key: K): boolean
| | Always returns true |size(): number
| | Returns the number of entries |isEmpty(): boolean
| | Returns true if the map has no entries |forEach(callbackfn: (value: V, key: K, map: Map
| | Iterates over all entries |getReadonlyRawMap(): ReadonlyMap
| | Returns the underlying Map |
This package has no external dependencies. It only uses the built-in Map` type from TypeScript/roblox-ts.
MIT