1- import { TFile , TFolder , requireApiVersion } from 'obsidian' ;
2- import {
3- CustomSortGroup ,
4- CustomSortGroupType ,
5- CustomSortOrder ,
6- CustomSortSpec
7- } from "./custom-sort-types" ;
1+ import { requireApiVersion , TFile , TFolder } from 'obsidian' ;
2+ import { CustomSortGroup , CustomSortGroupType , CustomSortOrder , CustomSortSpec } from "./custom-sort-types" ;
83import { isDefined } from "../utils/utils" ;
94
105let Collator = new Intl . Collator ( undefined , {
@@ -15,7 +10,7 @@ let Collator = new Intl.Collator(undefined, {
1510
1611interface FolderItemForSorting {
1712 path : string
18- groupIdx : number // the index itself represents order for groups
13+ groupIdx ? : number // the index itself represents order for groups
1914 sortString : string // fragment (or full name) to be used for sorting
2015 matchGroup ?: string // advanced - used for secondary sorting rule, to recognize 'same regex match'
2116 ctime : number
@@ -38,15 +33,22 @@ let Sorters: { [key in CustomSortOrder]: SorterFn } = {
3833} ;
3934
4035function compareTwoItems ( itA : FolderItemForSorting , itB : FolderItemForSorting , sortSpec : CustomSortSpec ) {
41- if ( itA . groupIdx === itB . groupIdx ) {
42- const group : CustomSortGroup = sortSpec . groups [ itA . groupIdx ]
43- if ( group . regexSpec && group . secondaryOrder && itA . matchGroup === itB . matchGroup ) {
44- return Sorters [ group . secondaryOrder ] ( itA , itB )
36+ if ( itA . groupIdx != undefined && itB . groupIdx != undefined ) {
37+ if ( itA . groupIdx === itB . groupIdx ) {
38+ const group : CustomSortGroup | undefined = sortSpec . groups [ itA . groupIdx ]
39+ if ( group ?. regexSpec && group . secondaryOrder && itA . matchGroup === itB . matchGroup ) {
40+ return Sorters [ group . secondaryOrder ?? CustomSortOrder . default ] ( itA , itB )
41+ } else {
42+ return Sorters [ group ?. order ?? CustomSortOrder . default ] ( itA , itB )
43+ }
4544 } else {
46- return Sorters [ group . order ] ( itA , itB )
45+ return itA . groupIdx - itB . groupIdx ;
4746 }
4847 } else {
49- return itA . groupIdx - itB . groupIdx ;
48+ // should never happen - groupIdx is not known for at least one of items to compare.
49+ // The logic of determining the index always sets some idx
50+ // Yet for sanity and to satisfy TS code analyzer a fallback to default behavior below
51+ return Sorters [ CustomSortOrder . default ] ( itA , itB )
5052 }
5153}
5254
@@ -58,7 +60,7 @@ const isFolder = (entry: TFile | TFolder) => {
5860export const determineSortingGroup = function ( entry : TFile | TFolder , spec : CustomSortSpec ) : FolderItemForSorting {
5961 let groupIdx : number
6062 let determined : boolean = false
61- let matchedGroup : string
63+ let matchedGroup : string | null | undefined
6264 const aFolder : boolean = isFolder ( entry )
6365 const aFile : boolean = ! aFolder
6466 const entryAsTFile : TFile = entry as TFile
@@ -77,10 +79,10 @@ export const determineSortingGroup = function (entry: TFile | TFolder, spec: Cus
7779 determined = true ;
7880 }
7981 } else { // regexp is involved
80- const match : RegExpMatchArray = group . regexSpec . regex . exec ( nameForMatching ) ;
82+ const match : RegExpMatchArray | null | undefined = group . regexSpec ? .regex . exec ( nameForMatching ) ;
8183 if ( match ) {
8284 determined = true
83- matchedGroup = group . regexSpec . normalizerFn ( match [ 1 ] ) ;
85+ matchedGroup = group . regexSpec ? .normalizerFn ( match [ 1 ] ) ;
8486 }
8587 }
8688 break ;
@@ -90,10 +92,10 @@ export const determineSortingGroup = function (entry: TFile | TFolder, spec: Cus
9092 determined = true ;
9193 }
9294 } else { // regexp is involved
93- const match : RegExpMatchArray = group . regexSpec . regex . exec ( nameForMatching ) ;
95+ const match : RegExpMatchArray | null | undefined = group . regexSpec ? .regex . exec ( nameForMatching ) ;
9496 if ( match ) {
9597 determined = true
96- matchedGroup = group . regexSpec . normalizerFn ( match [ 1 ] ) ;
98+ matchedGroup = group . regexSpec ? .normalizerFn ( match [ 1 ] ) ;
9799 }
98100 }
99101 break ;
@@ -107,10 +109,10 @@ export const determineSortingGroup = function (entry: TFile | TFolder, spec: Cus
107109 } else { // regexp is involved as the prefix or as the suffix
108110 if ( ( group . exactPrefix && nameForMatching . startsWith ( group . exactPrefix ) ) ||
109111 ( group . exactSuffix && nameForMatching . endsWith ( group . exactSuffix ) ) ) {
110- const match : RegExpMatchArray = group . regexSpec . regex . exec ( nameForMatching ) ;
112+ const match : RegExpMatchArray | null | undefined = group . regexSpec ? .regex . exec ( nameForMatching ) ;
111113 if ( match ) {
112114 const fullMatch : string = match [ 0 ]
113- matchedGroup = group . regexSpec . normalizerFn ( match [ 1 ] ) ;
115+ matchedGroup = group . regexSpec ? .normalizerFn ( match [ 1 ] ) ;
114116 // check for overlapping of prefix and suffix match (not allowed)
115117 if ( ( fullMatch . length + ( group . exactPrefix ?. length ?? 0 ) + ( group . exactSuffix ?. length ?? 0 ) ) <= nameForMatching . length ) {
116118 determined = true
@@ -127,10 +129,10 @@ export const determineSortingGroup = function (entry: TFile | TFolder, spec: Cus
127129 determined = true ;
128130 }
129131 } else { // regexp is involved
130- const match : RegExpMatchArray = group . regexSpec . regex . exec ( nameForMatching ) ;
132+ const match : RegExpMatchArray | null | undefined = group . regexSpec ? .regex . exec ( nameForMatching ) ;
131133 if ( match ) {
132134 determined = true
133- matchedGroup = group . regexSpec . normalizerFn ( match [ 1 ] ) ;
135+ matchedGroup = group . regexSpec ? .normalizerFn ( match [ 1 ] ) ;
134136 }
135137 }
136138 break ;
@@ -144,7 +146,7 @@ export const determineSortingGroup = function (entry: TFile | TFolder, spec: Cus
144146 }
145147
146148 // the final groupIdx for undetermined folder entry is either the last+1 groupIdx or idx of explicitly defined outsiders group
147- let determinedGroupIdx = groupIdx ;
149+ let determinedGroupIdx : number | undefined = groupIdx ;
148150
149151 if ( ! determined ) {
150152 // Automatically assign the index to outsiders group, if relevant was configured
@@ -174,7 +176,7 @@ export const folderSort = function (sortingSpec: CustomSortSpec, order: string[]
174176
175177 const folderItems : Array < FolderItemForSorting > = ( sortingSpec . itemsToHide ?
176178 this . file . children . filter ( ( entry : TFile | TFolder ) => {
177- return ! sortingSpec . itemsToHide . has ( entry . name )
179+ return ! sortingSpec . itemsToHide ! . has ( entry . name )
178180 } )
179181 :
180182 this . file . children )
0 commit comments