diff --git a/src/replace.spec.ts b/src/replace.spec.ts index f8cf811..333ebe4 100644 --- a/src/replace.spec.ts +++ b/src/replace.spec.ts @@ -41,6 +41,19 @@ describe('replace', () => { expect(result).toEqual(expected); }); + it('should replace curly brace placeholders with values from mockParameters object even if one is null', () => { + const mockParameters = { + param1: 'value1', + param2: 123, // non-string value + param3: null, // non-string value + param4: 'value4/with/slashes' // should be kebab-cased + } as any; + const mockFileStringWithCurlyBrace = 'This is a {param1} and {param2} and {param3} and {param4}'; + const expectedResult = 'This is a value-1 and 123 and null and value4/with/slashes'; + const result = replaceCurlyBrace(mockParameters, mockFileStringWithCurlyBrace, true); + expect(result).toEqual(expectedResult); + }); + describe('parseEJSCode', () => { it('should replace tag parameters', () => { const mockParameters = { diff --git a/src/replace.ts b/src/replace.ts index a9d64f1..6ff958f 100644 --- a/src/replace.ts +++ b/src/replace.ts @@ -21,12 +21,14 @@ export const parseEJSCode = ( return ejs.render(toReplace, parameters) } -export const replaceCurlyBrace = (mockParameters: object, mockFileStringWithCurlyBrace: string, useKebabCase?: boolean): string => { +export const replaceCurlyBrace = (mockParameters: Record, mockFileStringWithCurlyBrace: string, useKebabCase?: boolean): string => { let result = mockFileStringWithCurlyBrace; for(const key in mockParameters) { // only use kebab case if non file path - const valueToReplaceWith = useKebabCase && mockParameters[key].split('/').length < 2 ? kebabCase(mockParameters[key]) : mockParameters[key]; + const value = mockParameters[key]; + const isString = typeof value === 'string'; + const valueToReplaceWith = useKebabCase && isString && value.split('/').length < 2 ? kebabCase(value) : value; result = result.replaceAll('{' + key + '}', valueToReplaceWith); } return result; -} \ No newline at end of file +}