UNPKG

1.36 kBTypeScriptView Raw
1import { PathParameterValues } from '../types';
2/**
3 * A Node in the trie
4 */
5export interface Node<T> {
6 /**
7 * Key of the node
8 */
9 key: string;
10 /**
11 * Value of the node
12 */
13 value?: T;
14 /**
15 * Children of the node
16 */
17 readonly children: {
18 [key: string]: Node<T>;
19 };
20 /**
21 * Regular expression for the template
22 */
23 regexp?: RegExp;
24 /**
25 * Names of the node if it contains named parameters
26 */
27 names?: string[];
28}
29export type NodeWithValue<T> = Node<T> & {
30 value: T;
31};
32export interface ResolvedNode<T> {
33 node: Node<T>;
34 params?: PathParameterValues;
35}
36/**
37 * An implementation of trie for routes. The key hierarchy is built with parts
38 * of the route path delimited by `/`
39 */
40export declare class Trie<T> {
41 readonly root: Node<T>;
42 /**
43 * Create a node for a given path template
44 * @param pathTemplate - The path template,
45 * @param value - Value of the route
46 */
47 create(routeTemplate: string, value: T): Node<T>;
48 /**
49 * Match a route path against the trie
50 * @param path - The route path, such as `/customers/c01`
51 */
52 match(path: string): (ResolvedNode<T> & {
53 node: NodeWithValue<T>;
54 }) | undefined;
55 /**
56 * List all nodes with value of the trie
57 */
58 list(): NodeWithValue<T>[];
59}