Skip to content

Commit aa19bde

Browse files
committed
feat: add checking startWithDigit for enums
1 parent 7a9ff70 commit aa19bde

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/Case.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export enum Case {
1414

1515
export const pascalCase = flow(camelCase, upperFirst);
1616

17-
const isMultipleWords = (str: string) => Boolean(str.match(/[\s_-]|[a-z][A-Z0-9]/g));
17+
const isMultipleWords = (str: string) => Boolean(str.match(/[\s_]|[a-z][A-Z0-9]/g));
1818

1919
const camelCaseForMultipleWords = (str: string) => (isMultipleWords(str) ? camelCase(str) : str);
2020
const snakeCaseForMultipleWords = (str: string) => (isMultipleWords(str) ? snakeCase(str) : str);
@@ -25,6 +25,12 @@ const transforms = {
2525
[Case.PASCAL]: pascalCase,
2626
};
2727

28+
const startsWithDigit = (inputString: string): boolean => {
29+
// Regular expression to check if the string starts with a digit
30+
const regex = /^\d/;
31+
return regex.test(inputString);
32+
};
33+
2834
// A recursive function that looks at the models and their properties and
2935
// converts each property name using the provided transform function.
3036
export const convertModelNames = <T extends Model | OperationResponse>(model: T, type: Exclude<Case, Case.NONE>): T => {
@@ -40,9 +46,10 @@ export const convertModelNames = <T extends Model | OperationResponse>(model: T,
4046
};
4147

4248
const convertEnumName = (modelEnum: Enum, type: Exclude<Case, Case.NONE>): Enum => {
49+
const transformedName = transforms[type](modelEnum.name);
4350
return {
4451
...modelEnum,
45-
name: transforms[type](modelEnum.name),
52+
name: startsWithDigit(transformedName) ? `_${transformedName}` : transformedName,
4653
};
4754
};
4855

0 commit comments

Comments
 (0)