From 2670cdb613e2bb48a797754119b482445bbcfcd5 Mon Sep 17 00:00:00 2001 From: SebastianMC <23032356+SebastianMC@users.noreply.github.com> Date: Fri, 25 Aug 2023 14:43:43 +0200 Subject: [PATCH] Fix for head tail regex match logic, when both regexp produce matching groups - unit tests --- src/custom-sort/custom-sort.spec.ts | 32 +++++++++++++++++++++++++++++ src/custom-sort/custom-sort.ts | 19 +++++++++++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/custom-sort/custom-sort.spec.ts b/src/custom-sort/custom-sort.spec.ts index 285096c..d30bbc7 100644 --- a/src/custom-sort/custom-sort.spec.ts +++ b/src/custom-sort/custom-sort.spec.ts @@ -347,6 +347,38 @@ describe('determineSortingGroup', () => { path: 'Some parent folder/Part:123-icle.md' }); }); + it('should match head and tail, when advanced regexp in both, head and tail', () => { + // given + const file: TFile = mockTFile('Part 555-6 123-icle', 'md', 444, MOCK_TIMESTAMP + 555, MOCK_TIMESTAMP + 666); + const sortSpec: CustomSortSpec = { + targetFoldersPaths: ['Some parent folder'], + groups: [{ + type: CustomSortGroupType.ExactHeadAndTail, + regexPrefix: { + regex: /^Part *(\d+(?:-\d+)*)/i, + normalizerFn: CompoundDashNumberNormalizerFn + }, + regexSuffix: { + regex: / *(\d+(?:-\d+)*)-icle$/i, + normalizerFn: CompoundDashNumberNormalizerFn + } + }] + } + + // when + const result = determineSortingGroup(file, sortSpec) + + // then + expect(result).toEqual({ + groupIdx: 0, // Matched! + isFolder: false, + sortString: "00000555|00000006//00000123////Part 555-6 123-icle.md", + matchGroup: '00000555|00000006//00000123//', + ctime: MOCK_TIMESTAMP + 555, + mtime: MOCK_TIMESTAMP + 666, + path: 'Some parent folder/Part 555-6 123-icle.md' + }); + }); }) describe('CustomSortGroupType.ExactPrefix', () => { it('should correctly recognize exact prefix', () => { diff --git a/src/custom-sort/custom-sort.ts b/src/custom-sort/custom-sort.ts index af8f1b4..382f302 100644 --- a/src/custom-sort/custom-sort.ts +++ b/src/custom-sort/custom-sort.ts @@ -191,6 +191,16 @@ export const getSorterFnFor = (sorting: CustomSortOrder, currentUIselectedSortin } } +// logic of primary and secondary sorting: +// 0th - compare groupIdx +// 1st - apply primary, if 0 then +// 2nd - apply secondary if present, if 0 or not present then +// 3rd - apply folder-level order (which also will probably return 0) +// 4th - apply the UI-selected +// 5th - if not known, use CustomSortOrder.default + + + function getComparator(sortSpec: CustomSortSpec, currentUIselectedSorting?: string): SorterFn { const compareTwoItems = (itA: FolderItemForSorting, itB: FolderItemForSorting) => { if (itA.groupIdx != undefined && itB.groupIdx != undefined) { @@ -208,7 +218,9 @@ function getComparator(sortSpec: CustomSortSpec, currentUIselectedSorting?: stri } else { // should never happen - groupIdx is not known for at least one of items to compare. // The logic of determining the index always sets some idx - // Yet for sanity and to satisfy TS code analyzer a fallback to default behavior below + // Yet for sanity and to satisfy TS code analyzer some valid behavior below + if (itA.groupIdx !== undefined) return -1 + if (itB.groupIdx !== undefined) return 1 return getSorterFnFor(CustomSortOrder.default, currentUIselectedSorting)(itA, itB) } } @@ -313,7 +325,10 @@ export const determineSortingGroup = function (entry: TFile | TFolder, spec: Cus // check for overlapping of prefix and suffix match (not allowed) if ((fullMatchLeft!.length + fullMatchRight!.length) <= nameForMatching.length) { determined = true - matchedGroup = matchedGroupLeft ?? matchedGroupRight + + if (matchedGroupLeft || matchedGroupRight) { + matchedGroup = (matchedGroupLeft && matchedGroupRight) ? (matchedGroupLeft + matchedGroupRight) : (matchedGroupLeft ?? matchedGroupRight) + } } } }