@@ -8,7 +8,7 @@ import { ReflectionCategory } from "../../models";
8
8
import { Component , ConverterComponent } from "../components" ;
9
9
import { Converter } from "../converter" ;
10
10
import type { Context } from "../context" ;
11
- import { BindOption , removeIf } from "../../utils" ;
11
+ import { BindOption , getSortFunction , removeIf } from "../../utils" ;
12
12
13
13
/**
14
14
* A handler that sorts and categorizes the found reflections in the resolving phase.
@@ -17,6 +17,8 @@ import { BindOption, removeIf } from "../../utils";
17
17
*/
18
18
@Component ( { name : "category" } )
19
19
export class CategoryPlugin extends ConverterComponent {
20
+ sortFunction ! : ( reflections : DeclarationReflection [ ] ) => void ;
21
+
20
22
@BindOption ( "defaultCategory" )
21
23
defaultCategory ! : string ;
22
24
@@ -55,6 +57,8 @@ export class CategoryPlugin extends ConverterComponent {
55
57
* Triggered when the converter begins converting a project.
56
58
*/
57
59
private onBegin ( _context : Context ) {
60
+ this . sortFunction = getSortFunction ( this . application . options ) ;
61
+
58
62
// Set up static properties
59
63
if ( this . defaultCategory ) {
60
64
CategoryPlugin . defaultCategory = this . defaultCategory ;
@@ -156,40 +160,32 @@ export class CategoryPlugin extends ConverterComponent {
156
160
getReflectionCategories (
157
161
reflections : DeclarationReflection [ ]
158
162
) : ReflectionCategory [ ] {
159
- const categories : ReflectionCategory [ ] = [ ] ;
160
- let defaultCat : ReflectionCategory | undefined ;
161
- reflections . forEach ( ( child ) => {
163
+ const categories = new Map < string , ReflectionCategory > ( ) ;
164
+
165
+ for ( const child of reflections ) {
162
166
const childCategories = this . getCategories ( child ) ;
163
167
if ( childCategories . size === 0 ) {
164
- if ( ! defaultCat ) {
165
- defaultCat = categories . find (
166
- ( category ) =>
167
- category . title === CategoryPlugin . defaultCategory
168
- ) ;
169
- if ( ! defaultCat ) {
170
- defaultCat = new ReflectionCategory (
171
- CategoryPlugin . defaultCategory
172
- ) ;
173
- categories . push ( defaultCat ) ;
174
- }
175
- }
176
-
177
- defaultCat . children . push ( child ) ;
178
- return ;
168
+ childCategories . add ( CategoryPlugin . defaultCategory ) ;
179
169
}
170
+
180
171
for ( const childCat of childCategories ) {
181
- let category = categories . find ( ( cat ) => cat . title === childCat ) ;
172
+ const category = categories . get ( childCat ) ;
182
173
183
174
if ( category ) {
184
175
category . children . push ( child ) ;
185
- continue ;
176
+ } else {
177
+ const cat = new ReflectionCategory ( childCat ) ;
178
+ cat . children . push ( child ) ;
179
+ categories . set ( childCat , cat ) ;
186
180
}
187
- category = new ReflectionCategory ( childCat ) ;
188
- category . children . push ( child ) ;
189
- categories . push ( category ) ;
190
181
}
191
- } ) ;
192
- return categories ;
182
+ }
183
+
184
+ for ( const cat of categories . values ( ) ) {
185
+ this . sortFunction ( cat . children ) ;
186
+ }
187
+
188
+ return Array . from ( categories . values ( ) ) ;
193
189
}
194
190
195
191
/**
0 commit comments