diff --git a/src/custom-sort/sorting-spec-processor.spec.ts b/src/custom-sort/sorting-spec-processor.spec.ts index fde99c8..c84bdc8 100644 --- a/src/custom-sort/sorting-spec-processor.spec.ts +++ b/src/custom-sort/sorting-spec-processor.spec.ts @@ -527,6 +527,68 @@ describe('SortingSpecProcessor', () => { }) }) +const txtInputThreeDotsCases: string = ` +target-folder: AAA +... +.... + // Only in the below scenario the / is treated as empty-separator and swallowed +./... + // Below tricky and not obvious cases +../... +.../.. +../...S +S.../.. +S../...S +` + +const expectedSortSpecForThreeDotsCases: { [key: string]: CustomSortSpec } = { + "AAA": { + groups: [{ + type: CustomSortGroupType.MatchAll + },{ + exactSuffix: '.', + type: CustomSortGroupType.ExactSuffix + },{ + exactPrefix: '.', + type: CustomSortGroupType.ExactPrefix + },{ + exactPrefix: '..', + type: CustomSortGroupType.ExactPrefix + },{ + exactSuffix: '/..', + type: CustomSortGroupType.ExactSuffix + },{ + exactPrefix: '..', + exactSuffix: 'S', + type: CustomSortGroupType.ExactHeadAndTail + },{ + exactPrefix: 'S', + exactSuffix: '/..', + type: CustomSortGroupType.ExactHeadAndTail + },{ + exactPrefix: 'S..', + exactSuffix: 'S', + type: CustomSortGroupType.ExactHeadAndTail + },{ + type: CustomSortGroupType.Outsiders + }], + outsidersGroupIdx: 8, + targetFoldersPaths: ['AAA'] + } +} + +describe('SortingSpecProcessor', () => { + let processor: SortingSpecProcessor; + beforeEach(() => { + processor = new SortingSpecProcessor(); + }); + it('should correctly handle some of three-dots scenarios', () => { + const inputTxtArr: Array = txtInputThreeDotsCases.split('\n') + const result = processor.parseSortSpecFromText(inputTxtArr, 'mock-folder', 'custom-name-note.md') + expect(result?.sortSpecByPath).toEqual(expectedSortSpecForThreeDotsCases) + }) +}) + const txtInputTrueAlphabeticalSortAttr: string = ` target-folder: True Alpha < true a-z @@ -2788,6 +2850,78 @@ describe('convertPlainStringSortingGroupSpecToArraySpec', () => { '...', 'tion. !!!' ]) }) + it('should recognize four dots escaper - variant 0', () => { + const s = './...' + expect(processor.convertPlainStringSortingGroupSpecToArraySpec(s)).toEqual([ + '.', '...' + ]) + }) + it('should recognize four dots escaper - variant 1', () => { + const s = '../...' + expect(processor.convertPlainStringSortingGroupSpecToArraySpec(s)).toEqual([ + '..', '...' + ]) + }) + it('should recognize four dots escaper - variant 2', () => { + const s = './...Some' + expect(processor.convertPlainStringSortingGroupSpecToArraySpec(s)).toEqual([ + '.', '...', 'Some' + ]) + }) + it('should recognize four dots escaper - variant 3', () => { + const s = 'Some./...' + expect(processor.convertPlainStringSortingGroupSpecToArraySpec(s)).toEqual([ + 'Some.','...' + ]) + }) + it('should recognize four dots escaper - variant 3a', () => { + const s = 'Some./.....' + expect(processor.convertPlainStringSortingGroupSpecToArraySpec(s)).toEqual([ + 'Some./..','...' + ]) + }) + it('should recognize four dots escaper - variant 3b', () => { + const s = 'Some./.....X' + expect(processor.convertPlainStringSortingGroupSpecToArraySpec(s)).toEqual([ + 'Some.','...', '..X' + ]) + }) + it('should recognize four dots escaper - variant 4', () => { + const s = 'Some./...Some' + expect(processor.convertPlainStringSortingGroupSpecToArraySpec(s)).toEqual([ + 'Some.','...', 'Some' + ]) + }) + it('should recognize four dots escaper - tricky variant 4', () => { + const s = 'Some./... haha ...Some' + expect(processor.convertPlainStringSortingGroupSpecToArraySpec(s)).toEqual([ + 'Some.','...', ' haha ...Some' + ]) + }) + it('should recognize four dots escaper - tricky variant 5', () => { + const s = 'S.../..' + expect(processor.convertPlainStringSortingGroupSpecToArraySpec(s)).toEqual([ + 'S','...', '/..' + ]) + }) + it('should NOT recognize four dots escaper - tricky variant 1', () => { + const s = 'Some... haha ./...Some' + expect(processor.convertPlainStringSortingGroupSpecToArraySpec(s)).toEqual([ + 'Some','...', ' haha ./...Some' + ]) + }) + it('should NOT recognize four dots escaper - tricky variant 2', () => { + const s = 'Some... haha .../...Some' + expect(processor.convertPlainStringSortingGroupSpecToArraySpec(s)).toEqual([ + 'Some', '...', ' haha .../...Some' + ]) + }) + it('should NOT recognize four dots escaper - tricky variant 3', () => { + const s = '.../...' + expect(processor.convertPlainStringSortingGroupSpecToArraySpec(s)).toEqual([ + '...', '/...' + ]) + }) it('should recognize some edge case', () => { const s = 'Edge...... ... ..... ... eee?' expect(processor.convertPlainStringSortingGroupSpecToArraySpec(s)).toEqual([ diff --git a/src/custom-sort/sorting-spec-processor.ts b/src/custom-sort/sorting-spec-processor.ts index 2109e28..693e9bf 100644 --- a/src/custom-sort/sorting-spec-processor.ts +++ b/src/custom-sort/sorting-spec-processor.ts @@ -98,6 +98,10 @@ const ContextFreeProblems = new Set([ const ThreeDots = '...'; const ThreeDotsLength = ThreeDots.length; +const AmbigueFourDotsEscaper = './...' +const AmbigueFourDotsEscaperLength = AmbigueFourDotsEscaper.length +const AmbigueFourDotsEscaperOverlap = 1 // Number of leading chars in the Escaper to retain in original string + interface CustomSortOrderAscDescPair { asc: CustomSortOrder desc: CustomSortOrder @@ -365,7 +369,7 @@ const inlineRegexSymbolsArrEscapedForRegex: Array = [ escapeRegexUnsafeCharacters(InlineRegexSymbol_Digit2), escapeRegexUnsafeCharacters(InlineRegexSymbol_0_to_3), escapeRegexUnsafeCharacters(InlineRegexSymbol_CapitalLetter), - escapeRegexUnsafeCharacters(InlineRegexSymbol_LowercaseLetter) + escapeRegexUnsafeCharacters(InlineRegexSymbol_LowercaseLetter), ] interface RegexExpr { @@ -380,7 +384,7 @@ const inlineRegexSymbolsToRegexExpressionsArr: { [key: string]: RegexExpr} = { [InlineRegexSymbol_Digit2]: {regexExpr: '[0-9]'}, [InlineRegexSymbol_0_to_3]: {regexExpr: '[0-3]'}, [InlineRegexSymbol_CapitalLetter]: {regexExpr: '[\\p{Lu}\\p{Lt}]', isUnicode: true, isCaseSensitive: true}, - [InlineRegexSymbol_LowercaseLetter]: {regexExpr: '\\p{Ll}', isUnicode: true, isCaseSensitive: true} + [InlineRegexSymbol_LowercaseLetter]: {regexExpr: '\\p{Ll}', isUnicode: true, isCaseSensitive: true}, } const inlineRegexSymbolsDetectionRegex = new RegExp(inlineRegexSymbolsArrEscapedForRegex.join('|'), 'gi') @@ -1554,7 +1558,7 @@ export class SortingSpecProcessor { [Attribute.OrderUnspecified]: this.validateOrderAttrValue.bind(this) } - convertPlainStringSortingGroupSpecToArraySpec = (spec: string): Array => { + convertPlainStringSortingGroupSpecToArraySpec = (spec: string): Array => { spec = spec.trim() if (isThreeDots(spec)) { return [ThreeDots] @@ -1563,16 +1567,30 @@ export class SortingSpecProcessor { return [ThreeDots, spec.substring(ThreeDotsLength)]; } if (spec.endsWith(ThreeDots)) { - return [spec.substring(0, spec.length - ThreeDotsLength), ThreeDots]; + if (spec.endsWith(AmbigueFourDotsEscaper)) { + return [spec.substring(0, spec.length - AmbigueFourDotsEscaperLength + AmbigueFourDotsEscaperOverlap), ThreeDots]; + } else { + return [spec.substring(0, spec.length - ThreeDotsLength), ThreeDots]; + } } const idx = spec.indexOf(ThreeDots); + const idxOfAmbigueFourDotsEscaper = spec.indexOf(AmbigueFourDotsEscaper) if (idx > 0) { - return [ - spec.substring(0, idx), - ThreeDots, - spec.substring(idx + ThreeDotsLength) - ]; + if (idxOfAmbigueFourDotsEscaper >= 0 && + idxOfAmbigueFourDotsEscaper === idx - (AmbigueFourDotsEscaperLength - ThreeDotsLength) ) { + return [ + spec.substring(0, idxOfAmbigueFourDotsEscaper + AmbigueFourDotsEscaperOverlap), + ThreeDots, + spec.substring(idx + ThreeDotsLength) + ]; + } else { + return [ + spec.substring(0, idx), + ThreeDots, + spec.substring(idx + ThreeDotsLength) + ]; + } } // Unrecognized, treat as exact match