Fix for head tail regex match logic, when both regexp produce matching groups

- unit tests
This commit is contained in:
SebastianMC 2023-08-25 14:43:43 +02:00
parent 3cc58f69b9
commit 2670cdb613
2 changed files with 49 additions and 2 deletions

View File

@ -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', () => {

View File

@ -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)
}
}
}
}