@@ -93,7 +93,7 @@ export interface IMarkdownDocumenterOptions {
93
93
outputFolder : string ;
94
94
addFileNameSuffix : boolean ;
95
95
projectName : string ;
96
- shouldSortFunctions : boolean ;
96
+ sortFunctions : string ;
97
97
}
98
98
99
99
/**
@@ -109,15 +109,15 @@ export class MarkdownDocumenter {
109
109
private readonly _pluginLoader : PluginLoader ;
110
110
private readonly _addFileNameSuffix : boolean ;
111
111
private readonly _projectName : string ;
112
- private readonly _shouldSortFunctions : boolean ;
112
+ private readonly _sortFunctions : string ;
113
113
114
114
public constructor ( options : IMarkdownDocumenterOptions ) {
115
115
this . _apiModel = options . apiModel ;
116
116
this . _documenterConfig = options . documenterConfig ;
117
117
this . _outputFolder = options . outputFolder ;
118
118
this . _addFileNameSuffix = options . addFileNameSuffix ;
119
119
this . _projectName = options . projectName ;
120
- this . _shouldSortFunctions = options . shouldSortFunctions ;
120
+ this . _sortFunctions = options . sortFunctions ;
121
121
this . _tsdocConfiguration = CustomDocNodes . configuration ;
122
122
this . _markdownEmitter = new CustomMarkdownEmitter ( this . _apiModel ) ;
123
123
@@ -905,8 +905,13 @@ page_type: reference
905
905
break ;
906
906
907
907
case ApiItemKind . Function :
908
- if ( this . _shouldSortFunctions ) {
909
- const firstParam = ( apiMember as ApiParameterListMixin ) . parameters [ 0 ] || { name : '' } ;
908
+ /**
909
+ * If this option is set, group functions by first param.
910
+ * Organize using a map where the key is the first param.
911
+ */
912
+ if ( this . _sortFunctions ) {
913
+ const firstParam = ( apiMember as ApiParameterListMixin )
914
+ . parameters [ 0 ] || { name : '' } ;
910
915
if ( ! functionsRowGroup [ firstParam . name ] ) {
911
916
functionsRowGroup [ firstParam . name ] = [ ] ;
912
917
}
@@ -941,17 +946,48 @@ page_type: reference
941
946
}
942
947
}
943
948
944
- if ( this . _shouldSortFunctions ) {
945
- const sortedFunctionsFirstParamKeys = Object . keys ( functionsRowGroup ) . sort ( ( a , b ) => {
946
- if ( a === 'app' ) {
947
- return - 1 ;
949
+ /**
950
+ * Sort the functions groups by first param. If priority params were
951
+ * provided to --sort-functions, will put them first in the order
952
+ * given.
953
+ */
954
+ if ( this . _sortFunctions ) {
955
+ let priorityParams : string [ ] = [ ] ;
956
+ if ( this . _sortFunctions . includes ( ',' ) ) {
957
+ priorityParams = this . _sortFunctions . split ( ',' ) ;
958
+ } else {
959
+ priorityParams = [ this . _sortFunctions ] ;
960
+ }
961
+ const sortedFunctionsFirstParamKeys = Object . keys ( functionsRowGroup ) . sort (
962
+ ( a , b ) => {
963
+ if ( ! a || ! b ) {
964
+ console . log ( `Missing one. a:${ a } , b:${ b } ` )
965
+ }
966
+ if ( priorityParams . includes ( a ) && priorityParams . includes ( b ) ) {
967
+ return priorityParams . indexOf ( a ) - priorityParams . indexOf ( b ) ;
968
+ } else if ( priorityParams . includes ( a ) ) {
969
+ return - 1 ;
970
+ } else if ( priorityParams . includes ( b ) ) {
971
+ return 1 ;
972
+ }
973
+ return a . localeCompare ( b ) ;
948
974
}
949
- return ( a . localeCompare ( b ) ) ;
950
- } ) ;
951
-
975
+ ) ;
976
+
952
977
for ( const paramKey of sortedFunctionsFirstParamKeys ) {
953
- if ( finalFunctionsTable . rows . length > 0 ) {
954
- finalFunctionsTable . createAndAddRow ( ) ;
978
+ // Header for each group of functions grouped by first param.
979
+ // Doesn't make sense if there's only one group.
980
+ const headerText = paramKey ? `function(${ paramKey } ...)` : 'function()' ;
981
+ if ( sortedFunctionsFirstParamKeys . length > 1 ) {
982
+ finalFunctionsTable . addRow (
983
+ new DocTableRow ( { configuration } , [
984
+ new DocTableCell ( { configuration } , [
985
+ new DocParagraph ( { configuration } , [
986
+ new DocPlainText ( { configuration, text : headerText } )
987
+ ] )
988
+ ] )
989
+ ] )
990
+ ) ;
955
991
}
956
992
for ( const functionsRow of functionsRowGroup [ paramKey ] ) {
957
993
finalFunctionsTable . addRow ( functionsRow ) ;
0 commit comments