Skip to content

Commit d3e1170

Browse files
committed
Bug fix: when only files outsiders group was present, no catch-all outsiders group was added automatically. Same for folders-only outsiders group.
Cosmetics: wildcard patters for root like '/*' or '/...' produced a non matchable sort spec in the main sorting spec. The sort spec in wildcards tree was ok.
1 parent 4bca973 commit d3e1170

File tree

2 files changed

+87
-4
lines changed

2 files changed

+87
-4
lines changed

src/custom-sort/sorting-spec-processor.spec.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,86 @@ describe('SortingSpecProcessor', () => {
436436
})
437437
})
438438

439+
const txtInputSimplistic1: string = `
440+
target-folder: /*
441+
/:files
442+
//folders
443+
`
444+
445+
const expectedSortSpecForSimplistic1: { [key: string]: CustomSortSpec } = {
446+
"/": {
447+
groups: [{
448+
filesOnly: true,
449+
order: CustomSortOrder.alphabetical,
450+
type: CustomSortGroupType.Outsiders
451+
}, {
452+
order: CustomSortOrder.alphabetical,
453+
type: CustomSortGroupType.Outsiders
454+
}],
455+
outsidersFilesGroupIdx: 0,
456+
outsidersGroupIdx: 1,
457+
targetFoldersPaths: ['/*']
458+
}
459+
}
460+
461+
const expectedWildcardMatchingTreeForSimplistic1 = {
462+
"matchAll": {
463+
groups: [{
464+
filesOnly: true,
465+
order: CustomSortOrder.alphabetical,
466+
type: CustomSortGroupType.Outsiders
467+
}, {
468+
order: CustomSortOrder.alphabetical,
469+
type: CustomSortGroupType.Outsiders
470+
}],
471+
outsidersFilesGroupIdx: 0,
472+
outsidersGroupIdx: 1,
473+
targetFoldersPaths: ['/*']
474+
},
475+
"subtree": {}
476+
}
477+
478+
const txtInputSimplistic2: string = `
479+
target-folder: /
480+
/:files
481+
//folders
482+
`
483+
484+
const expectedSortSpecForSimplistic2: { [key: string]: CustomSortSpec } = {
485+
"/": {
486+
groups: [{
487+
filesOnly: true,
488+
order: CustomSortOrder.alphabetical,
489+
type: CustomSortGroupType.Outsiders
490+
}, {
491+
order: CustomSortOrder.alphabetical,
492+
type: CustomSortGroupType.Outsiders
493+
}],
494+
outsidersFilesGroupIdx: 0,
495+
outsidersGroupIdx: 1,
496+
targetFoldersPaths: ['/']
497+
}
498+
}
499+
500+
describe('SortingSpecProcessor', () => {
501+
let processor: SortingSpecProcessor;
502+
beforeEach(() => {
503+
processor = new SortingSpecProcessor();
504+
});
505+
it('should recognize the simplistic sorting spec to put files first (wildcard /* rule)', () => {
506+
const inputTxtArr: Array<string> = txtInputSimplistic1.split('\n')
507+
const result = processor.parseSortSpecFromText(inputTxtArr, 'mock-folder', 'custom-name-note.md')
508+
expect(result?.sortSpecByPath).toEqual(expectedSortSpecForSimplistic1)
509+
expect(result?.sortSpecByWildcard.tree).toEqual(expectedWildcardMatchingTreeForSimplistic1)
510+
})
511+
it('should recognize the simplistic sorting spec to put files first (direct / rule)', () => {
512+
const inputTxtArr: Array<string> = txtInputSimplistic2.split('\n')
513+
const result = processor.parseSortSpecFromText(inputTxtArr, 'mock-folder', 'custom-name-note.md')
514+
expect(result?.sortSpecByPath).toEqual(expectedSortSpecForSimplistic2)
515+
expect(result?.sortSpecByWildcard).toBeUndefined()
516+
})
517+
})
518+
439519
const txtInputItemsToHideWithDupsSortSpec: string = `
440520
target-folder: AAA
441521
/--hide: SomeFileToHide.md

src/custom-sort/sorting-spec-processor.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,20 +326,23 @@ enum WildcardPriority {
326326

327327
const stripWildcardPatternSuffix = (path: string): [path: string, priority: number] => {
328328
if (path.endsWith(MATCH_ALL_SUFFIX)) {
329+
path = path.slice(0, -MATCH_ALL_SUFFIX.length)
329330
return [
330-
path.slice(0, -MATCH_ALL_SUFFIX.length),
331+
path.length > 0 ? path : '/',
331332
WildcardPriority.MATCH_ALL
332333
]
333334
}
334335
if (path.endsWith(MATCH_CHILDREN_1_SUFFIX)) {
336+
path = path.slice(0, -MATCH_CHILDREN_1_SUFFIX.length)
335337
return [
336-
path.slice(0, -MATCH_CHILDREN_1_SUFFIX.length),
338+
path.length > 0 ? path : '/',
337339
WildcardPriority.MATCH_CHILDREN,
338340
]
339341
}
340342
if (path.endsWith(MATCH_CHILDREN_2_SUFFIX)) {
343+
path = path.slice(0, -MATCH_CHILDREN_2_SUFFIX.length)
341344
return [
342-
path.slice(0, -MATCH_CHILDREN_2_SUFFIX.length),
345+
path.length > 0 ? path : '/',
343346
WildcardPriority.MATCH_CHILDREN
344347
]
345348
}
@@ -732,7 +735,7 @@ export class SortingSpecProcessor {
732735
console.warn(`Inconsistent Outsiders sorting group definition in sort spec for folder '${last(spec.targetFoldersPaths)}'`)
733736
}
734737
// For consistency and to simplify sorting code later on, implicitly append a single catch-all Outsiders group
735-
if (!outsidersGroupForFiles && !outsidersGroupForFolders) {
738+
if (!(outsidersGroupForFiles && outsidersGroupForFolders)) {
736739
spec.outsidersGroupIdx = spec.groups.length
737740
spec.groups.push({
738741
type: CustomSortGroupType.Outsiders

0 commit comments

Comments
 (0)