Skip to content

Commit

Permalink
Merge pull request #22 from razroo/fix-replace-curly-if-null-value
Browse files Browse the repository at this point in the history
fix Replace curly brace so value works if null
  • Loading branch information
CharlieGreenman authored Mar 30, 2023
2 parents 45717c5 + 0012a80 commit 991bdb8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/replace.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
8 changes: 5 additions & 3 deletions src/replace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>, 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;
}
}

0 comments on commit 991bdb8

Please sign in to comment.