diff --git a/src/custom-sort/sorting-spec-processor.spec.ts b/src/custom-sort/sorting-spec-processor.spec.ts index 7632209..f013118 100644 --- a/src/custom-sort/sorting-spec-processor.spec.ts +++ b/src/custom-sort/sorting-spec-processor.spec.ts @@ -436,6 +436,86 @@ describe('SortingSpecProcessor', () => { }) }) +const txtInputSimplistic1: string = ` +target-folder: /* +/:files +//folders +` + +const expectedSortSpecForSimplistic1: { [key: string]: CustomSortSpec } = { + "/": { + groups: [{ + filesOnly: true, + order: CustomSortOrder.alphabetical, + type: CustomSortGroupType.Outsiders + }, { + order: CustomSortOrder.alphabetical, + type: CustomSortGroupType.Outsiders + }], + outsidersFilesGroupIdx: 0, + outsidersGroupIdx: 1, + targetFoldersPaths: ['/*'] + } +} + +const expectedWildcardMatchingTreeForSimplistic1 = { + "matchAll": { + groups: [{ + filesOnly: true, + order: CustomSortOrder.alphabetical, + type: CustomSortGroupType.Outsiders + }, { + order: CustomSortOrder.alphabetical, + type: CustomSortGroupType.Outsiders + }], + outsidersFilesGroupIdx: 0, + outsidersGroupIdx: 1, + targetFoldersPaths: ['/*'] + }, + "subtree": {} +} + +const txtInputSimplistic2: string = ` +target-folder: / +/:files +//folders +` + +const expectedSortSpecForSimplistic2: { [key: string]: CustomSortSpec } = { + "/": { + groups: [{ + filesOnly: true, + order: CustomSortOrder.alphabetical, + type: CustomSortGroupType.Outsiders + }, { + order: CustomSortOrder.alphabetical, + type: CustomSortGroupType.Outsiders + }], + outsidersFilesGroupIdx: 0, + outsidersGroupIdx: 1, + targetFoldersPaths: ['/'] + } +} + +describe('SortingSpecProcessor', () => { + let processor: SortingSpecProcessor; + beforeEach(() => { + processor = new SortingSpecProcessor(); + }); + it('should recognize the simplistic sorting spec to put files first (wildcard /* rule)', () => { + const inputTxtArr: Array = txtInputSimplistic1.split('\n') + const result = processor.parseSortSpecFromText(inputTxtArr, 'mock-folder', 'custom-name-note.md') + expect(result?.sortSpecByPath).toEqual(expectedSortSpecForSimplistic1) + expect(result?.sortSpecByWildcard.tree).toEqual(expectedWildcardMatchingTreeForSimplistic1) + }) + it('should recognize the simplistic sorting spec to put files first (direct / rule)', () => { + const inputTxtArr: Array = txtInputSimplistic2.split('\n') + const result = processor.parseSortSpecFromText(inputTxtArr, 'mock-folder', 'custom-name-note.md') + expect(result?.sortSpecByPath).toEqual(expectedSortSpecForSimplistic2) + expect(result?.sortSpecByWildcard).toBeUndefined() + }) +}) + const txtInputItemsToHideWithDupsSortSpec: string = ` target-folder: AAA /--hide: SomeFileToHide.md diff --git a/src/custom-sort/sorting-spec-processor.ts b/src/custom-sort/sorting-spec-processor.ts index be2aadf..17e360b 100644 --- a/src/custom-sort/sorting-spec-processor.ts +++ b/src/custom-sort/sorting-spec-processor.ts @@ -326,20 +326,23 @@ enum WildcardPriority { const stripWildcardPatternSuffix = (path: string): [path: string, priority: number] => { if (path.endsWith(MATCH_ALL_SUFFIX)) { + path = path.slice(0, -MATCH_ALL_SUFFIX.length) return [ - path.slice(0, -MATCH_ALL_SUFFIX.length), + path.length > 0 ? path : '/', WildcardPriority.MATCH_ALL ] } if (path.endsWith(MATCH_CHILDREN_1_SUFFIX)) { + path = path.slice(0, -MATCH_CHILDREN_1_SUFFIX.length) return [ - path.slice(0, -MATCH_CHILDREN_1_SUFFIX.length), + path.length > 0 ? path : '/', WildcardPriority.MATCH_CHILDREN, ] } if (path.endsWith(MATCH_CHILDREN_2_SUFFIX)) { + path = path.slice(0, -MATCH_CHILDREN_2_SUFFIX.length) return [ - path.slice(0, -MATCH_CHILDREN_2_SUFFIX.length), + path.length > 0 ? path : '/', WildcardPriority.MATCH_CHILDREN ] } @@ -732,7 +735,7 @@ export class SortingSpecProcessor { console.warn(`Inconsistent Outsiders sorting group definition in sort spec for folder '${last(spec.targetFoldersPaths)}'`) } // For consistency and to simplify sorting code later on, implicitly append a single catch-all Outsiders group - if (!outsidersGroupForFiles && !outsidersGroupForFolders) { + if (!(outsidersGroupForFiles && outsidersGroupForFolders)) { spec.outsidersGroupIdx = spec.groups.length spec.groups.push({ type: CustomSortGroupType.Outsiders