@thi.ng/router
Version:
Generic trie-based router with support for wildcards, route param validation/coercion, auth
55 lines • 1.44 kB
TypeScript
import type { Maybe } from "@thi.ng/api";
/**
* Trie data structure for storing & matching route patterns with wildcards.
*
* @remarks
* Wildcard handling:
*
* - `?` - matches any single item
* - `+` - matches 1 or more items following (only to be used in final position)
*
* Match priorities (highest to lowest):
*
* 1. Non-parametric values
* 2. `?` wildcard (params)
* 3. `+` wildcard
*
* If any higher priority route fails and a compatible lower priority route
* exists, it will still be attempted to be matched.
*
* @example
* ```ts tangle:../export/wildcards.ts
* import { Trie } from "@thi.ng/router";
*
* const trie = new Trie();
* trie.set(["a", "?", "c"], "A");
* trie.set(["a", "b"], "B");
* trie.set(["a", "+"], "C");
* trie.set(["+"], "D");
*
* // matches A because B doesn't match
* // and A has higher priority than C
* console.log(trie.get(["a", "b", "c"]));
* // A
*
* // perfect match B
* console.log(trie.get(["a", "b"]));
* // B
*
* // matches C because neither A or B matches
* console.log(trie.get(["a", "b", "d"]));
* // C
*
* // matches D because all others fail
* console.log(trie.get(["a"]));
* // D
* ```
*/
export declare class Trie<T> {
n: Record<string, Trie<T>>;
v?: T;
constructor(key?: string[], v?: Maybe<T>, i?: number);
set(key: string[], v: T, i?: number): void;
get(key: string[], i?: number): Maybe<T>;
}
//# sourceMappingURL=trie.d.ts.map