Skip to content

Commit 37bbab7

Browse files
committed
feat: improve variables utils (COR-9708) (#592)
1 parent 39b4fef commit 37bbab7

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

packages/common/src/utils/variables.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ describe('Utils | variables', () => {
128128
const replacedVariable = replaceVariables(
129129
`{${variable}.name}`,
130130
{ [variable]: { name: 'world' } },
131-
JSON.stringify
131+
{ modifier: JSON.stringify }
132132
);
133133
expect(replacedVariable).toBe('"world"');
134134
});

packages/common/src/utils/variables.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@ export const variableReplacer = (
55
match: string,
66
inner: string,
77
variables: Record<string, unknown>,
8-
modifier?: (variable: unknown) => unknown
8+
modifier: (variable: unknown) => unknown = (val) => val
99
): unknown => {
1010
const { id, path } = splitVariableName(inner);
1111
if (!(id in variables)) {
1212
return match;
1313
}
1414

1515
if (!path) {
16-
return typeof modifier === 'function' ? modifier(variables[id]) : variables[id];
16+
return modifier(variables[id]);
1717
}
1818

1919
try {
2020
const variable = typeof variables[id] === 'string' ? JSON.parse(variables[id] as string) : variables[id];
21-
return typeof modifier === 'function' ? modifier(_get(variable, path, 0)) : _get(variable, path, 0);
21+
22+
return modifier(_get(variable, path, 0));
2223
} catch (err: any) {
2324
if (err?.message.includes('is not valid JSON')) {
2425
return 0;
@@ -67,32 +68,36 @@ export const splitVariableName = (
6768
export function replaceVariables(
6869
phrase: string | undefined | null,
6970
variables: Record<string, unknown>,
70-
modifier?: (variable: unknown) => unknown,
71-
options?: { trim?: boolean; keepTypeIfOnlyVariable?: false }
71+
options?: { modifier?: (variable: unknown) => unknown; trim?: boolean; keepTypeIfOnlyVariable?: false }
7272
): string;
7373
export function replaceVariables(
7474
phrase: string | undefined | null,
7575
variables: Record<string, unknown>,
76-
modifier?: (variable: unknown) => unknown,
77-
options?: { trim?: boolean; keepTypeIfOnlyVariable: true }
76+
options: { modifier?: (variable: unknown) => unknown; trim?: boolean; keepTypeIfOnlyVariable: true }
7877
): unknown;
7978
export function replaceVariables(
8079
phrase: string | undefined | null,
8180
variables: Record<string, unknown>,
82-
modifier: ((variable: unknown) => unknown) | undefined = undefined,
83-
{ trim = true, keepTypeIfOnlyVariable = false }: { trim?: boolean; keepTypeIfOnlyVariable?: boolean } = {}
81+
{
82+
trim = true,
83+
modifier,
84+
keepTypeIfOnlyVariable = false,
85+
}: { modifier?: ((variable: unknown) => unknown) | undefined; trim?: boolean; keepTypeIfOnlyVariable?: boolean } = {}
8486
): string | unknown {
85-
if (!phrase || (trim && !phrase.trim())) {
87+
const formattedPhrase = trim ? phrase?.trim() : phrase;
88+
89+
if (!formattedPhrase) {
8690
return '';
8791
}
8892

89-
if (keepTypeIfOnlyVariable && phrase.match(VARIABLE_ONLY_REGEXP)) {
93+
if (keepTypeIfOnlyVariable && formattedPhrase.match(VARIABLE_ONLY_REGEXP)) {
9094
// remove the curly braces {} from phrase to get the inner
91-
const inner = phrase.slice(1, -1);
92-
return variableReplacer(phrase, inner, variables, modifier);
95+
const inner = formattedPhrase.slice(1, -1);
96+
97+
return variableReplacer(formattedPhrase, inner, variables, modifier);
9398
}
9499

95-
return phrase.replace(READABLE_VARIABLE_REGEXP, (match, inner) =>
100+
return formattedPhrase.replace(READABLE_VARIABLE_REGEXP, (match, inner) =>
96101
String(variableReplacer(match, inner, variables, modifier))
97102
);
98103
}
@@ -123,18 +128,22 @@ export const transformStringVariableToNumber = (str: string | number | null): nu
123128
return Number.isNaN(number) ? str : number;
124129
};
125130

126-
export const deepVariableSubstitution = <T>(bodyData: T, variableMap: Record<string, unknown>): T => {
127-
const _recurse = (subCollection: any, modifier?: (variable: unknown) => unknown): any => {
131+
export const deepVariableSubstitution = <T>(
132+
bodyData: T,
133+
variableMap: Record<string, unknown>,
134+
options?: { trim?: boolean; modifier?: (variable: unknown) => unknown; keepTypeIfOnlyVariable?: boolean }
135+
): T => {
136+
const _recurse = (subCollection: any): any => {
128137
if (!subCollection) {
129138
return subCollection;
130139
}
131140

132141
if (typeof subCollection === 'string') {
133-
return replaceVariables(subCollection, variableMap, modifier);
142+
return replaceVariables(subCollection, variableMap, options as any);
134143
}
135144

136145
if (Array.isArray(subCollection)) {
137-
return subCollection.map((v) => _recurse(v, modifier));
146+
return subCollection.map((v) => _recurse(v));
138147
}
139148

140149
if (typeof subCollection === 'object') {

0 commit comments

Comments
 (0)