@@ -130,23 +130,36 @@ export class TreeNode {
130
130
this . value . setOverride ( filePath , routeBlock )
131
131
}
132
132
133
- getSortedChildren ( ) : TreeNode [ ] {
134
- return Array . from ( this . children . values ( ) ) . sort ( ( a , b ) =>
135
- a . path . localeCompare ( b . path )
136
- )
133
+ /**
134
+ * Generator that yields all descendants without sorting.
135
+ * Use with Array.from() for now, native .map() support in Node 22+.
136
+ */
137
+ * getChildrenDeep ( ) : Generator < TreeNode > {
138
+ for ( const child of this . children . values ( ) ) {
139
+ yield child
140
+ yield * child . getChildrenDeep ( )
141
+ }
142
+ }
143
+
144
+ /**
145
+ * Comparator function for sorting TreeNodes by path.
146
+ */
147
+ static compareByPath ( a : TreeNode , b : TreeNode ) : number {
148
+ return a . path . localeCompare ( b . path )
137
149
}
138
150
139
- getSortedChildrenDeep ( ) : TreeNode [ ] {
140
- return Array . from ( this . children . values ( ) )
141
- . flatMap ( ( child ) => [ child , ...child . getSortedChildrenDeep ( ) ] )
142
- . sort ( ( a , b ) => a . path . localeCompare ( b . path ) )
151
+ /**
152
+ * Get the children of this node sorted by their path.
153
+ */
154
+ getSortedChildren ( ) : TreeNode [ ] {
155
+ return Array . from ( this . children . values ( ) ) . sort ( TreeNode . compareByPath )
143
156
}
144
157
145
- getChildrenDeep ( ) : TreeNode [ ] {
146
- return Array . from ( this . children . values ( ) ) . flatMap ( ( child ) => [
147
- child ,
148
- ... child . getChildrenDeep ( ) ,
149
- ] )
158
+ /**
159
+ * Calls { @link getChildrenDeep} and sorts the result by path in the end.
160
+ */
161
+ getChildrenDeepSorted ( ) : TreeNode [ ] {
162
+ return Array . from ( this . getChildrenDeep ( ) ) . sort ( TreeNode . compareByPath )
150
163
}
151
164
152
165
/**
0 commit comments