Skip to content

Commit 0012a80

Browse files
Replace curly brace so value works.
1 parent 45717c5 commit 0012a80

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/replace.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ describe('replace', () => {
4141
expect(result).toEqual(expected);
4242
});
4343

44+
it('should replace curly brace placeholders with values from mockParameters object even if one is null', () => {
45+
const mockParameters = {
46+
param1: 'value1',
47+
param2: 123, // non-string value
48+
param3: null, // non-string value
49+
param4: 'value4/with/slashes' // should be kebab-cased
50+
} as any;
51+
const mockFileStringWithCurlyBrace = 'This is a {param1} and {param2} and {param3} and {param4}';
52+
const expectedResult = 'This is a value-1 and 123 and null and value4/with/slashes';
53+
const result = replaceCurlyBrace(mockParameters, mockFileStringWithCurlyBrace, true);
54+
expect(result).toEqual(expectedResult);
55+
});
56+
4457
describe('parseEJSCode', () => {
4558
it('should replace tag parameters', () => {
4659
const mockParameters = {

src/replace.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ export const parseEJSCode = (
2121
return ejs.render(toReplace, parameters)
2222
}
2323

24-
export const replaceCurlyBrace = (mockParameters: object, mockFileStringWithCurlyBrace: string, useKebabCase?: boolean): string => {
24+
export const replaceCurlyBrace = (mockParameters: Record<string, string>, mockFileStringWithCurlyBrace: string, useKebabCase?: boolean): string => {
2525
let result = mockFileStringWithCurlyBrace;
2626
for(const key in mockParameters) {
2727
// only use kebab case if non file path
28-
const valueToReplaceWith = useKebabCase && mockParameters[key].split('/').length < 2 ? kebabCase(mockParameters[key]) : mockParameters[key];
28+
const value = mockParameters[key];
29+
const isString = typeof value === 'string';
30+
const valueToReplaceWith = useKebabCase && isString && value.split('/').length < 2 ? kebabCase(value) : value;
2931
result = result.replaceAll('{' + key + '}', valueToReplaceWith);
3032
}
3133
return result;
32-
}
34+
}

0 commit comments

Comments
 (0)