@@ -14,6 +14,10 @@ const createMockMatcherRichVersion = (): FolderWildcardMatching<SortingSpec> =>
1414 return matcher
1515}
1616
17+ const PRIO1 = 1
18+ const PRIO2 = 2
19+ const PRIO3 = 3
20+
1721const createMockMatcherSimplestVersion = ( ) : FolderWildcardMatching < SortingSpec > => {
1822 const matcher : FolderWildcardMatching < SortingSpec > = new FolderWildcardMatching ( )
1923 matcher . addWildcardDefinition ( '/Reviews/daily/*' , '/Reviews/daily/*' )
@@ -117,4 +121,137 @@ describe('folderMatch', () => {
117121
118122 expect ( result ) . toEqual ( { errorMsg : "Duplicate wildcard '*' specification for Archive/2019/*" } )
119123 } )
124+ it ( 'regexp-match by name works (order of regexp doesn\'t matter) case A' , ( ) => {
125+ const matcher : FolderWildcardMatching < SortingSpec > = new FolderWildcardMatching ( )
126+ matcher . addRegexpDefinition ( / ^ d a i l y $ / , false , undefined , false , `r1` )
127+ matcher . addRegexpDefinition ( / ^ d a i l y $ / , true , undefined , false , `r2` )
128+ matcher . addWildcardDefinition ( '/Reviews/*' , `w1` )
129+ // Path with leading /
130+ const match1 : SortingSpec | null = matcher . folderMatch ( '/Reviews/daily' , 'daily' )
131+ // Path w/o leading / - this is how Obsidian supplies the path
132+ const match2 : SortingSpec | null = matcher . folderMatch ( 'Reviews/daily' , 'daily' )
133+ expect ( match1 ) . toBe ( 'r2' )
134+ expect ( match2 ) . toBe ( 'r2' )
135+ } )
136+ it ( 'regexp-match by name works (order of regexp doesn\'t matter) reversed case A' , ( ) => {
137+ const matcher : FolderWildcardMatching < SortingSpec > = new FolderWildcardMatching ( )
138+ matcher . addRegexpDefinition ( / ^ d a i l y $ / , true , undefined , false , `r2` )
139+ matcher . addRegexpDefinition ( / ^ d a i l y $ / , false , undefined , false , `r1` )
140+ matcher . addWildcardDefinition ( '/Reviews/*' , `w1` )
141+ // Path with leading /
142+ const match1 : SortingSpec | null = matcher . folderMatch ( '/Reviews/daily' , 'daily' )
143+ // Path w/o leading / - this is how Obsidian supplies the path
144+ const match2 : SortingSpec | null = matcher . folderMatch ( 'Reviews/daily' , 'daily' )
145+ expect ( match1 ) . toBe ( 'r2' )
146+ expect ( match2 ) . toBe ( 'r2' )
147+ } )
148+ it ( 'regexp-match by path works (order of regexp doesn\'t matter) case A' , ( ) => {
149+ const matcher : FolderWildcardMatching < SortingSpec > = new FolderWildcardMatching ( )
150+ matcher . addRegexpDefinition ( / ^ R e v i e w s \/ d a i l y $ / , false , undefined , false , `r1` )
151+ matcher . addRegexpDefinition ( / ^ R e v i e w s \/ d a i l y $ / , true , undefined , false , `r2` )
152+ matcher . addWildcardDefinition ( '/Reviews/*' , `w1` )
153+ // Path with leading /
154+ const match1 : SortingSpec | null = matcher . folderMatch ( '/Reviews/daily' , 'daily' )
155+ // Path w/o leading / - this is how Obsidian supplies the path
156+ const match2 : SortingSpec | null = matcher . folderMatch ( 'Reviews/daily' , 'daily' )
157+ expect ( match1 ) . toBe ( 'w1' ) // The path-based regexp doesn't match the leading /
158+ expect ( match2 ) . toBe ( 'r1' )
159+ } )
160+ it ( 'regexp-match by path works (order of regexp doesn\'t matter) reversed case A' , ( ) => {
161+ const matcher : FolderWildcardMatching < SortingSpec > = new FolderWildcardMatching ( )
162+ matcher . addRegexpDefinition ( / ^ R e v i e w s \/ d a i l y $ / , true , undefined , false , `r2` )
163+ matcher . addRegexpDefinition ( / ^ R e v i e w s \/ d a i l y $ / , false , undefined , false , `r1` )
164+ matcher . addWildcardDefinition ( '/Reviews/*' , `w1` )
165+ // Path with leading /
166+ const match1 : SortingSpec | null = matcher . folderMatch ( '/Reviews/daily' , 'daily' )
167+ // Path w/o leading / - this is how Obsidian supplies the path
168+ const match2 : SortingSpec | null = matcher . folderMatch ( 'Reviews/daily' , 'daily' )
169+ expect ( match1 ) . toBe ( 'w1' ) // The path-based regexp doesn't match the leading /
170+ expect ( match2 ) . toBe ( 'r1' )
171+ } )
172+ it ( 'regexp-match by path and name for root level - order of regexp decides - case A' , ( ) => {
173+ const matcher : FolderWildcardMatching < SortingSpec > = new FolderWildcardMatching ( )
174+ matcher . addRegexpDefinition ( / ^ d a i l y $ / , false , undefined , false , `r1` )
175+ matcher . addRegexpDefinition ( / ^ d a i l y $ / , true , undefined , false , `r2` )
176+ matcher . addWildcardDefinition ( '/Reviews/*' , `w1` )
177+ // Path w/o leading / - this is how Obsidian supplies the path
178+ const match : SortingSpec | null = matcher . folderMatch ( 'daily' , 'daily' )
179+ expect ( match ) . toBe ( 'r2' )
180+ } )
181+ it ( 'regexp-match by path and name for root level - order of regexp decides - reversed case A' , ( ) => {
182+ const matcher : FolderWildcardMatching < SortingSpec > = new FolderWildcardMatching ( )
183+ matcher . addRegexpDefinition ( / ^ d a i l y $ / , true , undefined , false , `r2` )
184+ matcher . addRegexpDefinition ( / ^ d a i l y $ / , false , undefined , false , `r1` )
185+ matcher . addWildcardDefinition ( '/Reviews/*' , `w1` )
186+ // Path w/o leading / - this is how Obsidian supplies the path
187+ const match : SortingSpec | null = matcher . folderMatch ( 'daily' , 'daily' )
188+ expect ( match ) . toBe ( 'r1' )
189+ } )
190+ it ( 'regexp-match priorities - order of definitions irrelevant - unique priorities - case A' , ( ) => {
191+ const matcher : FolderWildcardMatching < SortingSpec > = new FolderWildcardMatching ( )
192+ matcher . addRegexpDefinition ( / ^ f r e q \/ d a i l y $ / , false , 3 , false , `r1p3` )
193+ matcher . addRegexpDefinition ( / ^ f r e q \/ d a i l y $ / , false , 2 , false , `r2p2` )
194+ matcher . addRegexpDefinition ( / ^ f r e q \/ d a i l y $ / , false , 1 , false , `r3p1` )
195+ matcher . addRegexpDefinition ( / ^ f r e q \/ d a i l y $ / , false , undefined , false , `r4pNone` )
196+ // Path w/o leading / - this is how Obsidian supplies the path
197+ const match : SortingSpec | null = matcher . folderMatch ( 'freq/daily' , 'daily' )
198+ expect ( match ) . toBe ( 'r1p3' )
199+ } )
200+ it ( 'regexp-match priorities - order of definitions irrelevant - unique priorities - reversed case A' , ( ) => {
201+ const matcher : FolderWildcardMatching < SortingSpec > = new FolderWildcardMatching ( )
202+ matcher . addRegexpDefinition ( / ^ f r e q \/ d a i l y $ / , false , undefined , false , `r4pNone` )
203+ matcher . addRegexpDefinition ( / ^ f r e q \/ d a i l y $ / , false , 1 , false , `r3p1` )
204+ matcher . addRegexpDefinition ( / ^ f r e q \/ d a i l y $ / , false , 2 , false , `r2p2` )
205+ matcher . addRegexpDefinition ( / ^ f r e q \/ d a i l y $ / , false , 3 , false , `r1p3` )
206+ // Path w/o leading / - this is how Obsidian supplies the path
207+ const match : SortingSpec | null = matcher . folderMatch ( 'freq/daily' , 'daily' )
208+ expect ( match ) . toBe ( 'r1p3' )
209+ } )
210+ it ( 'regexp-match priorities - order of definitions irrelevant - duplicate priorities - case A' , ( ) => {
211+ const matcher : FolderWildcardMatching < SortingSpec > = new FolderWildcardMatching ( )
212+ matcher . addRegexpDefinition ( / ^ d a i l y $ / , true , 3 , false , `r1p3a` )
213+ matcher . addRegexpDefinition ( / ^ d a i l y $ / , true , 3 , false , `r1p3b` )
214+ matcher . addRegexpDefinition ( / ^ d a i l y $ / , true , 2 , false , `r2p2a` )
215+ matcher . addRegexpDefinition ( / ^ d a i l y $ / , true , 2 , false , `r2p2b` )
216+ matcher . addRegexpDefinition ( / ^ d a i l y $ / , true , 1 , false , `r3p1a` )
217+ matcher . addRegexpDefinition ( / ^ d a i l y $ / , true , 1 , false , `r3p1b` )
218+ matcher . addRegexpDefinition ( / ^ d a i l y $ / , true , undefined , false , `r4pNone` )
219+ // Path w/o leading / - this is how Obsidian supplies the path
220+ const match : SortingSpec | null = matcher . folderMatch ( 'daily' , 'daily' )
221+ expect ( match ) . toBe ( 'r1p3b' )
222+ } )
223+ it ( 'regexp-match priorities - order of definitions irrelevant - unique priorities - reversed case A' , ( ) => {
224+ const matcher : FolderWildcardMatching < SortingSpec > = new FolderWildcardMatching ( )
225+ matcher . addRegexpDefinition ( / ^ f r e q \/ d a i l y $ / , false , undefined , false , `r4pNone` )
226+ matcher . addRegexpDefinition ( / ^ f r e q \/ d a i l y $ / , false , 1 , false , `r3p1` )
227+ matcher . addRegexpDefinition ( / ^ f r e q \/ d a i l y $ / , false , 2 , false , `r2p2` )
228+ matcher . addRegexpDefinition ( / ^ f r e q \/ d a i l y $ / , false , 3 , false , `r1p3` )
229+ // Path w/o leading / - this is how Obsidian supplies the path
230+ const match : SortingSpec | null = matcher . folderMatch ( 'freq/daily' , 'daily' )
231+ expect ( match ) . toBe ( 'r1p3' )
232+ } )
233+ it ( 'regexp-match - edge case of matching the root folder - match by path' , ( ) => {
234+ const matcher : FolderWildcardMatching < SortingSpec > = new FolderWildcardMatching ( )
235+ matcher . addRegexpDefinition ( / ^ \/ $ / , false , undefined , false , `r1` )
236+ // Path w/o leading / - this is how Obsidian supplies the path
237+ const match : SortingSpec | null = matcher . folderMatch ( '/' , '' )
238+ expect ( match ) . toBe ( 'r1' )
239+ } )
240+ it ( 'regexp-match - edge case of matching the root folder - match by name not possible' , ( ) => {
241+ const matcher : FolderWildcardMatching < SortingSpec > = new FolderWildcardMatching ( )
242+ // Tricky regexp which can return zero length matches
243+ matcher . addRegexpDefinition ( / .* / , true , undefined , false , `r1` )
244+ matcher . addWildcardDefinition ( '/*' , `w1` )
245+ // Path w/o leading / - this is how Obsidian supplies the path
246+ const match : SortingSpec | null = matcher . folderMatch ( '/' , '' )
247+ expect ( match ) . toBe ( 'w1' )
248+ } )
249+ it ( 'regexp-match - edge case of no match when only regexp rules present' , ( ) => {
250+ const matcher : FolderWildcardMatching < SortingSpec > = new FolderWildcardMatching ( )
251+ // Tricky regexp which can return zero length matches
252+ matcher . addRegexpDefinition ( / a b c / , true , undefined , false , `r1` )
253+ // Path w/o leading / - this is how Obsidian supplies the path
254+ const match : SortingSpec | null = matcher . folderMatch ( '/' , '' )
255+ expect ( match ) . toBeNull ( )
256+ } )
120257} )
0 commit comments