-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
186 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { createPrefixTree, TreeNode } from "./tree"; | ||
|
||
export class ExtendableRoutes { | ||
tree: TreeNode | ||
|
||
constructor(tree: TreeNode) { | ||
this.tree = tree | ||
} | ||
|
||
/** | ||
* Traverse the tree in BFS order. | ||
* | ||
* @returns | ||
*/ | ||
*[Symbol.iterator]() { | ||
let currentNode: TreeNode | null = this.tree | ||
let currentChildren = this.tree.children | ||
|
||
while (currentNode) { | ||
yield* currentChildren | ||
|
||
|
||
} | ||
} | ||
} | ||
|
||
export class EditableTreeNode { | ||
node: TreeNode | ||
parentName?: string | ||
|
||
constructor(node: TreeNode, parentName?: string) { | ||
this.node = node | ||
this.parentName = parentName | ||
} | ||
|
||
/** | ||
* Remove the current route and all its children. | ||
*/ | ||
delete() { | ||
if (this.node.parent) { | ||
this.node.delete() | ||
// throw new Error('Cannot delete the root node.') | ||
} | ||
// this.node.parent.remove(this.node.path.slice(1)) | ||
} | ||
|
||
/** | ||
* Append a new route as a children of this route. | ||
*/ | ||
append() { | ||
|
||
} | ||
|
||
get name() { | ||
return this.node.name | ||
} | ||
|
||
get meta() { | ||
return this.node.metaAsObject | ||
} | ||
|
||
get path() { | ||
return this.node.path | ||
} | ||
|
||
get fullPath() { | ||
return this.node.fullPath | ||
} | ||
|
||
/** | ||
* DFS traversal of the tree. | ||
* @example | ||
* ```ts | ||
* for (const node of tree) { | ||
* // ... | ||
* } | ||
* ``` | ||
*/ | ||
*traverseDFS(): Generator<EditableTreeNode, void, unknown> { | ||
// the root node is special | ||
if (!this.node.isRoot()) { | ||
yield this | ||
} | ||
for (const [name, child] of this.node.children) { | ||
// console.debug(`CHILD: ${_name} - ${child.fullPath}`) | ||
yield* new EditableTreeNode(child, name).traverseDFS() | ||
} | ||
} | ||
|
||
*[Symbol.iterator](): Generator<EditableTreeNode, void, unknown> { | ||
yield* this.traverseBFS() | ||
} | ||
|
||
/** | ||
* BFS traversal of the tree as a generator. | ||
* | ||
* @example | ||
* ```ts | ||
* for (const node of tree) { | ||
* // ... | ||
* } | ||
* ``` | ||
*/ | ||
*traverseBFS(): Generator<EditableTreeNode, void, unknown> { | ||
for (const [name, child] of this.node.children) { | ||
yield new EditableTreeNode(child, name) | ||
} | ||
// we need to traverse again in case the user removed a route | ||
for (const [name, child] of this.node.children) { | ||
yield* new EditableTreeNode(child, name).traverseBFS() | ||
} | ||
} | ||
} | ||
|
||
function testy() { | ||
const tree = createPrefixTree({} as any) | ||
const route = new EditableTreeNode(tree) | ||
|
||
for (const r of route) { | ||
console.log(r.name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters