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.
To install Registrie
using npm:
npm install registrie
Or with yarn:
yarn add registrie
Registrie
can be used as either a simple key-value store or a more complex registry for nested entries.
If no entryKey
is provided, Registrie
behaves as a simple key-value registry.
import { Registrie } from 'registrie';
// Create a new registry
const basicRegistry = Registrie<string>();
// Register entries
basicRegistry.register('apple', 'A tasty fruit');
basicRegistry.register('banana', 'A yellow fruit');
// Query entries
console.log(basicRegistry.query('apple')); // Output: 'A tasty fruit'
// Get suggestions for a partial key
console.log(basicRegistry.candidate('b')); // Output: ['banana']
// Erase an entry
basicRegistry.erase('apple');
console.log(basicRegistry.query('apple')); // Output: undefined
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[];
}
// Create a nested registry
const nestedRegistry = Registrie<Category>('name', 'subCategories');
// Define categories
const fruits: Category = {
name: 'fruits',
subCategories: [{ name: 'apple' }, { name: 'banana' }]
};
const vegetables: Category = {
name: 'vegetables',
subCategories: [{ name: 'carrot' }, { name: 'broccoli' }]
};
// Register categories
nestedRegistry.register(fruits);
nestedRegistry.register(vegetables);
// Query entries
console.log(nestedRegistry.query('fruits apple')); // Output: { name: 'apple' }
// Get suggestions
console.log(nestedRegistry.candidate('fruits')); // Output: ['apple', 'banana']
Registers an entry in the registry.
-
key
: The key to use for the entry. -
value
: The entry to register. -
frozen
: Iftrue
(default), registers the entry as frozen (immutable).
Queries the registry for an entry by its key.
-
key
: The key to search for in the registry.
Provides suggestions for a partial key input.
-
key
: The partial key to use for suggestions.
Erases an entry from the registry.
-
key
: The key of the entry to remove.
Registers a nested entry in the registry.
-
value
: The entry to register. -
frozen
: Iftrue
(default), registers the entry as frozen (immutable).
Queries the registry for an entry by its key.
-
key
: The key to search for in the registry.
Provides suggestions for a partial key input.
-
key
: The partial key to use for suggestions.
Erases an entry and its children from the registry.
-
key
: The key of the entry to remove.
MIT License © Higuma Soft