Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions components/src/utils/variable-interpolation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ describe('replaceVariables() with custom formats', () => {
},
expected: 'hello (perses\\.|prometheus\\$) (world\\.)',
},
{
text: 'hello ${var1:regexliteral} ${var2:regexliteral}',
state: {
var1: { value: ['perses.', 'prometheus$'], loading: false },
var2: { value: 'world.', loading: false },
},
expected: 'hello (perses\\\\.|prometheus\\\\$) (world\\\\.)',
},
// singlequote
{
text: 'hello ${var1:singlequote} ${var2:singlequote}',
Expand Down
6 changes: 5 additions & 1 deletion components/src/utils/variable-interpolation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export enum InterpolationFormat {
SQLSTRING = 'sqlstring',
TEXT = 'text',
QUERYPARAM = 'queryparam',
REGEX_LITERAL = 'regexliteral',
}

function stringToFormat(val: string | undefined): InterpolationFormat | undefined {
Expand Down Expand Up @@ -110,6 +111,10 @@ export function interpolate(values: string[], name: string, format: Interpolatio
const escapedRegex = values.map((v) => v.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&'));
return `(${escapedRegex.join('|')})`;
}
case InterpolationFormat.REGEX_LITERAL: {
const escapedRegex = values.map((v) => v.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\\\$&'));
return `(${escapedRegex.join('|')})`;
}
case InterpolationFormat.SINGLEQUOTE:
return values.map((v) => `'${v}'`).join(',');
case InterpolationFormat.SQLSTRING:
Expand Down Expand Up @@ -143,7 +148,6 @@ export function replaceVariable(
if (typeof variableValue === 'string') {
replaceString = interpolate([variableValue], varName, varFormat || InterpolationFormat.RAW);
}

text = text.replaceAll(variableSyntax, replaceString);
return text.replaceAll(alternativeVariableSyntax, replaceString);
}
Expand Down
Loading