1.9 KiB
1.9 KiB
API: hyper-p2p-trie-prefix
Export: { HyperP2PTriePrefix, PROTOCOL }
Protocol: trie-prefix/v1 (identifier only — no P2P wire messages)
Overview
HyperP2PTriePrefix is a lowercase character trie supporting insert, prefix enumeration, membership test, and delete with path compaction. Ideal for autocomplete, command palettes, and namespace browsing in Bare/Pear UIs.
Constructor
const { HyperP2PTriePrefix } = require('hyper-p2p-trie-prefix')
const trie = new HyperP2PTriePrefix()
Root is internal TrieNode with children: Map, terminal, count.
Methods
insert(word)
- Normalizes to lowercase string
- Returns:
true(always after successful path walk) - Emits:
insertonly when word newly terminal - Increments:
stats.insertedon first insert
prefixSearch(prefix)
- Returns: sorted list of full words sharing prefix (includes prefix itself if terminal)
- DFS from prefix node
- Increments:
stats.queries
has(word)
Exact membership in _words set.
remove(word)
Deletes terminal flag, decrements count along path, prunes empty child nodes. Returns false if missing.
list()
Sorted all words.
getStats()
{ inserted, queries, words, protocol }
ready() / close()
close() resets root trie and _words, emits closed.
TrieNode fields
| Field | Meaning |
|---|---|
children |
Map<char, TrieNode> |
terminal |
Word ends here |
count |
Prefix reference count for compaction |
Events
| Event | Payload |
|---|---|
insert |
{ word } |
closed |
— |
Wire messages
None.
Errors
assertNonEmpty on insert.
Example
const trie = new HyperP2PTriePrefix()
trie.insert('hyper')
trie.insert('hyperbee')
console.log(trie.prefixSearch('hyp'))
Testing
npm install && npm test