RegisTrie
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.
Installation
To install Registrie
using npm:
npm install registrie
Or with yarn:
yarn add registrie
Usage
Registrie
can be used as either a simple key-value store or a more complex registry for nested entries.
Basic Example
If no entryKey
is provided, Registrie
behaves as a simple key-value registry.
import { Registrie } from 'registrie';
const basicRegistry = Registrie<string>();
basicRegistry.register('apple', 'A tasty fruit');
basicRegistry.register('banana', 'A yellow fruit');
console.log(basicRegistry.query('apple'));
console.log(basicRegistry.candidate('b'));
basicRegistry.erase('apple');
console.log(basicRegistry.query('apple'));
Nested Example
If entryKey
is provided, Registrie
supports nested structures. This is useful for managing hierarchical data where each entry has a key and potentially children.
import { Registrie } from 'registrie';
interface Category {
name: string;
subCategories?: Category[];
}
const nestedRegistry = Registrie<Category>('name', 'subCategories');
const fruits: Category = {
name: 'fruits',
subCategories: [{ name: 'apple' }, { name: 'banana' }]
};
const vegetables: Category = {
name: 'vegetables',
subCategories: [{ name: 'carrot' }, { name: 'broccoli' }]
};
nestedRegistry.register(fruits);
nestedRegistry.register(vegetables);
console.log(nestedRegistry.query('fruits apple'));
console.log(nestedRegistry.candidate('fruits'));
API
BasicRegistrie<T = any>
register(key: string, value: T, frozen?: boolean): void
Registers an entry in the registry.
key
: The key to use for the entry.value
: The entry to register.frozen
: If true
(default), registers the entry as frozen (immutable).
query(key: string): T | undefined
Queries the registry for an entry by its key.
key
: The key to search for in the registry.
candidate(key: string): string[]
Provides suggestions for a partial key input.
key
: The partial key to use for suggestions.
erase(key: string): void
Erases an entry from the registry.
key
: The key of the entry to remove.
NestedRegistrie<T extends object>
register(value: T, frozen?: boolean): void
Registers a nested entry in the registry.
value
: The entry to register.frozen
: If true
(default), registers the entry as frozen (immutable).
query(key: string): T | undefined
Queries the registry for an entry by its key.
key
: The key to search for in the registry.
candidate(key: string): Array<T[keyof T]>
Provides suggestions for a partial key input.
key
: The partial key to use for suggestions.
erase(key: string): void
Erases an entry and its children from the registry.
key
: The key of the entry to remove.
License
MIT License © Higuma Soft