Skip to content

Commit

Permalink
Merge pull request #1182 from mikepenz/feature/optimize_transformers
Browse files Browse the repository at this point in the history
Improve transformer performance
  • Loading branch information
mikepenz authored Sep 13, 2024
2 parents 85cfd64 + 77c5dd8 commit 6818a87
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 11 deletions.
6 changes: 4 additions & 2 deletions __tests__/testParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1074,11 +1074,13 @@ action.surefire.report.email.InvalidEmailAddressException: Invalid email address
const transformer: Transformer[] = [
{
searchValue: '\\.',
replaceValue: '/'
replaceValue: '/',
regex: new RegExp('\\.'.replace('\\\\', '\\'), 'gu')
},
{
searchValue: '(.+?)_t',
replaceValue: '$1.t'
replaceValue: '$1.t',
regex: new RegExp('(.+?)_t'.replace('\\\\', '\\'), 'gu')
}
]
const {totalCount, skippedCount, annotations} = await parseFile(
Expand Down
2 changes: 2 additions & 0 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('readTransformers', () => {
const transformer = readTransformers('[{"searchValue":"::","replaceValue":"/"}]')
expect(transformer).toStrictEqual([
{
regex: /::/gu,
searchValue: '::',
replaceValue: '/'
}
Expand All @@ -23,6 +24,7 @@ describe('readTransformers', () => {
)
expect(transformer).toStrictEqual([
{
regex: /\./gu,
searchValue: '\\.',
replaceValue: '/'
},
Expand Down
15 changes: 11 additions & 4 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/testParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface Position {
export interface Transformer {
searchValue: string
replaceValue: string
regex?: RegExp
}

/**
Expand Down
14 changes: 10 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ export function readTransformers(raw: string | undefined): Transformer[] {
}
try {
const transformers: Transformer[] = JSON.parse(raw)
for (const transformer of transformers) {
try {
transformer.regex = new RegExp(transformer.searchValue.replace('\\\\', '\\'), 'gu')
} catch (error) {
core.warning(`⚠️ Bad replacer regex: ${transformer.searchValue}`)
}
}
return transformers
} catch (error) {
core.info(`⚠️ Transformers provided, but they couldn't be parsed. Fallback to Defaults.`)
Expand All @@ -43,11 +50,10 @@ export function readTransformers(raw: string | undefined): Transformer[] {
}

export function applyTransformer(transformer: Transformer, string: string): string {
try {
const regExp = new RegExp(transformer.searchValue.replace('\\\\', '\\'), 'gu')
const regExp = transformer.regex
if (regExp) {
return string.replace(regExp, transformer.replaceValue)
} catch (e) {
core.warning(`⚠️ Bad replacer regex: ${transformer.searchValue}`)
} else {
return string.replace(transformer.searchValue, transformer.replaceValue)
}
}
Expand Down

0 comments on commit 6818a87

Please sign in to comment.