Skip to content

Commit 1e2f4b7

Browse files
committed
feat: add a method to sort all descendants
1 parent 28cf96a commit 1e2f4b7

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

modules/RouteNode.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,17 @@ export default class RouteNode {
165165
return this.children.filter(child => !child.absolute)
166166
}
167167

168+
public sortChildren() {
169+
if (this.children.length) {
170+
sortChildren(this.children)
171+
}
172+
}
173+
174+
public sortDescendants() {
175+
this.sortChildren()
176+
this.children.forEach(child => child.sortDescendants())
177+
}
178+
168179
public buildPath(
169180
routeName: string,
170181
params: object = {},
@@ -255,9 +266,7 @@ export default class RouteNode {
255266
this.children.push(route)
256267
// Push greedy spats to the bottom of the pile
257268

258-
const originalChildren = this.children.slice(0)
259-
260-
this.children.sort(sortChildren(originalChildren))
269+
this.sortChildren()
261270
} else {
262271
// Locate parent node
263272
const segments = this.getSegmentsByName(

modules/sortChildren.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import RouteNode from './RouteNode'
22

3-
export default (originalChildren: RouteNode[]) => (
3+
export default function sortChildren(children: RouteNode[]) {
4+
const originalChildren = children.slice(0)
5+
6+
return children.sort(sortPredicate(originalChildren))
7+
}
8+
9+
const sortPredicate = (originalChildren: RouteNode[]) => (
410
left: RouteNode,
511
right: RouteNode
612
): number => {

0 commit comments

Comments
 (0)