From 89d1f43dfa329f81b3a248cf1037b90baec07904 Mon Sep 17 00:00:00 2001 From: Mike Penz Date: Fri, 1 Mar 2024 15:20:31 +0000 Subject: [PATCH 1/3] - refactor regex handling for the different usecases to allow providing the preferred method - introduce new `exec` and `execAll` variants which support named groups - introduce `replaceAll` in addition to `replace` - update test cases - cleanup code --- __tests__/regexUtils.test.ts | 57 +++++++++++++++ __tests__/tags.test.ts | 19 +++-- src/configuration.ts | 8 +-- src/pr-collector/regexUtils.ts | 122 ++++++++++++++++++++++++++++----- src/pr-collector/tags.ts | 22 +++--- src/pr-collector/types.ts | 13 ++-- src/regexUtils.ts | 4 +- src/transform.ts | 39 ++++------- 8 files changed, 209 insertions(+), 75 deletions(-) create mode 100644 __tests__/regexUtils.test.ts diff --git a/__tests__/regexUtils.test.ts b/__tests__/regexUtils.test.ts new file mode 100644 index 0000000..2cf675c --- /dev/null +++ b/__tests__/regexUtils.test.ts @@ -0,0 +1,57 @@ +import { transformStringToValue, validateRegex } from '../src/pr-collector/regexUtils' +import { Regex } from '../src/pr-collector/types' + +jest.setTimeout(180000) + +it('Replace into target', async () => { + const regex: Regex = { + pattern: '.*(\\[Feature\\]|\\[Issue\\]).*', + target: '$1', + } + const validatedRegex = validateRegex(regex) + expect(validateRegex).not.toBeNull() + expect(transformStringToValue("[Feature] TEST", validatedRegex!!)).toStrictEqual(`[Feature]`) +}) + +it('Replace all into target', async () => { + const regex: Regex = { + pattern: '.*(\\[Feature\\]|\\[Issue\\]).*', + method: 'replaceAll', + target: '$1', + } + const validatedRegex = validateRegex(regex) + expect(validateRegex).not.toBeNull() + expect(transformStringToValue("[Feature] TEST", validatedRegex!!)).toStrictEqual(`[Feature]`) +}) + +it('Match without target', async () => { + const regex: Regex = { + pattern: '\\[Feature\\]|\\[Issue\\]', + method: 'match' + } + const validatedRegex = validateRegex(regex) + expect(validateRegex).not.toBeNull() + expect(transformStringToValue("[Feature] TEST", validatedRegex!!)).toStrictEqual(`[Feature]`) +}) + +it('Match into target', async () => { + const regex: Regex = { + pattern: '(?