registrie
v1.0.0
Published
A flexible trie-based registry for storing and querying entries with optional hierarchical keys. `Registrie` supports both simple key-value stores and complex nested structures, making it a versatile tool for various applications.
Maintainers
Readme
RegisTrie
A trie-based registry for TypeScript/JavaScript. Supports both simple key-value storage and hierarchical nested structures with prefix-based autocomplete.
Installation
npm install registrieUsage
BasicRegistrie — simple key-value store
import { Registrie } from 'registrie';
const registry = Registrie<string>();
registry.register('apple', 'A tasty fruit');
registry.register('banana', 'A yellow fruit');
registry.register('apricot', 'An orange fruit');
registry.query('apple'); // 'A tasty fruit'
registry.query('cherry'); // undefined
registry.candidate('ap'); // ['apple', 'apricot']
registry.candidate('b'); // ['banana']
registry.candidate(''); // ['apple', 'apricot', 'banana']
registry.erase('apple');
registry.query('apple'); // undefinedNestedRegistrie — hierarchical store
Keys are extracted from the objects themselves. Children are registered recursively.
import { Registrie } from 'registrie';
interface Command {
name: string;
description: string;
subCommands?: Command[];
}
const registry = Registrie<Command>('name', 'subCommands');
registry.register({
name: 'git',
description: 'Version control',
subCommands: [
{ name: 'commit', description: 'Record changes' },
{ name: 'push', description: 'Upload changes' },
]
});
registry.query('git'); // { name: 'git', ... }
registry.query('git commit'); // { name: 'commit', ... }
registry.query('git pull'); // undefined
// Pass a trailing space to get children of a node
registry.candidate(''); // ['git']
registry.candidate('git '); // ['commit', 'push']
registry.erase('git');
registry.query('git'); // undefined
registry.query('git commit'); // undefinedAPI
Registrie<T>(entryKey?, childrenEntryKey?)
Factory function. Returns a BasicRegistrie<T> or NestedRegistrie<T> depending on arguments.
| Arguments | Returns |
|-----------|---------|
| none | BasicRegistrie<T> |
| entryKey | NestedRegistrie<T> |
| entryKey, childrenEntryKey | NestedRegistrie<T> with recursive children |
BasicRegistrie<T>
| Method | Signature | Description |
|--------|-----------|-------------|
| register | (key: string, value: T, frozen?: boolean) => void | Store an entry. Overwrites if key exists. Throws on empty key. |
| query | (key: string) => T \| undefined | Exact key lookup. |
| candidate | (key: string) => string[] | All keys with given prefix, sorted. |
| erase | (key: string) => void | Remove an entry. No-op if not found. |
NestedRegistrie<T extends object>
| Method | Signature | Description |
|--------|-----------|-------------|
| register | (value: T, frozen?: boolean) => void | Store an entry, key extracted from object. Overwrites if key exists. |
| query | (key: string) => T \| undefined | Space-delimited path lookup. |
| candidate | (key: string) => string[] | Immediate child keys at current depth, sorted. Pass trailing space to get children. |
| erase | (key: string) => void | Remove entry and its logical children. No-op if not found. |
Notes
frozen defaults to true
By default, registered objects are frozen with Object.freeze() making them immutable after registration. Pass frozen: false to opt out:
registry.register('key', value, false);Duplicate keys Registering the same key twice silently overwrites the previous entry.
candidate() in NestedRegistrie
To get children of a node, include a trailing space in the key:
registry.candidate('git '); // children of 'git' → ['commit', 'push']
registry.candidate('git'); // entries with prefix 'git' → ['git']License
MIT © Higuma Soft
