(edges: string[])
| 35 | } |
| 36 | |
| 37 | insert(edges: string[]): RadixTrie<T> | Leaf<T> { |
| 38 | // eslint-disable-next-line @typescript-eslint/no-this-alias |
| 39 | let currentNode: RadixTrie<T> | Leaf<T> = this |
| 40 | for (let i = 0; i < edges.length; i += 1) { |
| 41 | const edge = edges[i] |
| 42 | let nextNode: RadixTrie<T> | Leaf<T> | null = currentNode.get(edge) |
| 43 | // If we're at the end of this set of edges: |
| 44 | if (i === edges.length - 1) { |
| 45 | // If this end already exists as a RadixTrie, then hose it and replace with a Leaf: |
| 46 | if (nextNode instanceof RadixTrie) { |
| 47 | currentNode.delete(nextNode) |
| 48 | nextNode = null |
| 49 | } |
| 50 | // If nextNode doesn't exist (or used to be a RadixTrie) then make a Leaf: |
| 51 | if (!nextNode) { |
| 52 | nextNode = new Leaf(currentNode) |
| 53 | currentNode.children[edge] = nextNode |
| 54 | } |
| 55 | return nextNode |
| 56 | // We're not at the end of this set of edges: |
| 57 | } else { |
| 58 | // If we're not at the end, but we've hit a Leaf, replace with a RadixTrie |
| 59 | if (nextNode instanceof Leaf) nextNode = null |
| 60 | if (!nextNode) { |
| 61 | nextNode = new RadixTrie(currentNode) |
| 62 | currentNode.children[edge] = nextNode |
| 63 | } |
| 64 | } |
| 65 | currentNode = nextNode |
| 66 | } |
| 67 | return currentNode |
| 68 | } |
| 69 | |
| 70 | delete(node: RadixTrie<T> | Leaf<T>): boolean { |
| 71 | for (const edge in this.children) { |
no test coverage detected