Skip to content

Commit

Permalink
Merge pull request #72 from razroo/71-remove-beginning
Browse files Browse the repository at this point in the history
remove beginning of forward slash
  • Loading branch information
CharlieGreenman authored Sep 8, 2024
2 parents 7b9979c + b1c516d commit 9b682bf
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 130 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 53 additions & 42 deletions src/replace.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,50 +73,61 @@ describe('replace', () => {
expect(result).toEqual(expectedResult);
});

describe('parseEJSCode', () => {
it('should replace tag parameters', () => {
const mockParameters = {
name: 'test',
selector: 'test-two',
className: ''
};
const mockString = `hello <%= name %>`;
const result = parseEJSCode(mockParameters, mockString);
expect(result).toEqual('hello test');
});
it('parseEJSCode should parse for loop correctly', () => {
const mockParameters = {
colors: ['blue', 'green', 'red', 'yellow'],
};
const mockFileString = "import x from 'y';\nclass DynamicClass = {\n<% for(let i=0; i<colors.length; i++) { %>\n<%= colors[i] %>: string;\n<% } %>\n}";

const result = parseEJSCode(mockParameters as any, mockFileString);
const expected = "import x from 'y';\nclass DynamicClass = {\n\nblue: string;\n\ngreen: string;\n\nred: string;\n\nyellow: string;\n\n}";
expect(result).toEqual(expected);
});
it('should remove beginning forward slash if null value', () => {
const mockParameters = {
name: 'blue',
nameFilePath: '',
}
const mockFileStringWithCurlyBrace = '{nameFilePath}/{name}.mock.ts';
const result = replaceCurlyBrace(mockParameters, mockFileStringWithCurlyBrace, true);
const expected = 'blue.mock.ts';
expect(result).toEqual(expected);
});

it('parseEJSCode should parse object loop correctly', () => {
const mockParameters = {
"nameSchema":{"Stepper":{"title":"String","id":"ID","count":"Int","recipeId":"String"}}
}
const mockFileString = `<% const schema = Object.values(nameSchema)[0] %>
export interface <%= Object.keys(nameSchema)[0] %> {<% for (const prop in schema) { %>
<%= prop %>: <%= schema[prop] %>;<% } %>
}
`;

const result = parseEJSCode(mockParameters as any, mockFileString);
const expected = `
export interface Stepper {
title: String;
id: ID;
count: Int;
recipeId: String;
}
`;
expect(result).toEqual(expected);
});
});

describe('parseEJSCode', () => {
it('should replace tag parameters', () => {
const mockParameters = {
name: 'test',
selector: 'test-two',
className: ''
};
const mockString = `hello <%= name %>`;
const result = parseEJSCode(mockParameters, mockString);
expect(result).toEqual('hello test');
});
it('parseEJSCode should parse for loop correctly', () => {
const mockParameters = {
colors: ['blue', 'green', 'red', 'yellow'],
};
const mockFileString = "import x from 'y';\nclass DynamicClass = {\n<% for(let i=0; i<colors.length; i++) { %>\n<%= colors[i] %>: string;\n<% } %>\n}";

const result = parseEJSCode(mockParameters as any, mockFileString);
const expected = "import x from 'y';\nclass DynamicClass = {\n\nblue: string;\n\ngreen: string;\n\nred: string;\n\nyellow: string;\n\n}";
expect(result).toEqual(expected);
});

it('parseEJSCode should parse object loop correctly', () => {
const mockParameters = {
"nameSchema":{"Stepper":{"title":"String","id":"ID","count":"Int","recipeId":"String"}}
}
const mockFileString = `<% const schema = Object.values(nameSchema)[0] %>
export interface <%= Object.keys(nameSchema)[0] %> {<% for (const prop in schema) { %>
<%= prop %>: <%= schema[prop] %>;<% } %>
}
`;

const result = parseEJSCode(mockParameters as any, mockFileString);
const expected = `
export interface Stepper {
title: String;
id: ID;
count: Int;
recipeId: String;
}
`;
expect(result).toEqual(expected);
});
});
});
42 changes: 27 additions & 15 deletions src/replace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,39 @@ export const parseEJSCode = (
return ejs.render(toReplace, parameters)
}

export const replaceCurlyBrace = (mockParameters: Record<string, string>, mockFileStringWithCurlyBrace: string, useKebabCase?: boolean): string => {
export const replaceCurlyBrace = (mockParameters: Record<string, any>, mockFileStringWithCurlyBrace: string, useKebabCase?: boolean): string => {
let result = mockFileStringWithCurlyBrace;
for(const key in mockParameters) {
// only use kebab case if non file path
const processValue = (value: any, useKebabCase: boolean): string => {
if (value === null || value === undefined) return 'null';
if (typeof value === 'string') {
if (useKebabCase && !value.includes('/')) {
return kebabCase(value);
}
return value;
}
return String(value);
};

for (const key in mockParameters) {
const value = mockParameters[key];
const isString = typeof value === 'string';
const valueToReplaceWith = useKebabCase && isString && value.split('/').length < 2 ? kebabCase(value) : value;
result = result.replace(new RegExp(`{${key}}`, 'g'), valueToReplaceWith);
const processedValue = processValue(value, useKebabCase || false);
result = result.replace(new RegExp(`{${key}}`, 'g'), processedValue);
}
const hasCurlyBrace = /{.*}/;
if (hasCurlyBrace.test(result)) {
for(const key in mockParameters) {
// only use kebab case if non file path

// Handle nested curly braces
while (/{.*}/.test(result)) {
for (const key in mockParameters) {
const value = mockParameters[key];
const isString = typeof value === 'string';
const valueToReplaceWith = useKebabCase && isString && value.split('/').length < 2 ? kebabCase(value) : value;
result = result.replace(new RegExp(`{${key}}`, 'g'), valueToReplaceWith);
const processedValue = processValue(value, useKebabCase || false);
result = result.replace(new RegExp(`{${key}}`, 'g'), processedValue);
}
}
return result;
}

// Remove leading forward slash if present
result = result.replace(/^\//, '');

// Remove any remaining empty path segments
result = result.replace(/\/+/g, '/').replace(/^\/|\/$/g, '');

return result;
}
71 changes: 0 additions & 71 deletions tsconfig.json

This file was deleted.

0 comments on commit 9b682bf

Please sign in to comment.