Skip to content

Commit

Permalink
Merge pull request #27 from barnuri/fix-enum
Browse files Browse the repository at this point in the history
typescript-fix-enum
  • Loading branch information
barnuri authored May 16, 2024
2 parents b9ec4da + c70afef commit 25ba328
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/generators/TypescriptGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,14 @@ ${objectInput.properties
const modelFile = join(this.modelsFolder, this.getFileName(enumInput) + this.getFileExtension(true));
const specialChars = ['-', ' ', '!'];
const specialKeywords = ['in', 'public', 'private', 'readonly'];
const fixName = (name: string) => (specialChars.filter(x => name.includes(x)).length > 0 || specialKeywords.filter(x => name === x).length > 0 ? `"${name}"` : name);
const modelFileContent = `
const shouldWrapName = (name: string) => {
if (specialChars.filter(x => name.includes(x)).length > 0) { return true; }
if (specialKeywords.filter(x => name === x).length > 0) { return true; }
if (!isNaN(parseFloat(name))) { return true; }
return false;
};
const fixName = (name: string) => shouldWrapName(name) ? `"${name}"` : name;
const modelFileContent = `
export enum ${this.getFileName(enumInput)} {
${Object.keys(enumVals)
.map(x => `\t${fixName(x)} = ${typeof enumVals[x] === 'number' ? enumVals[x] : `'${enumVals[x]}'`}`)
Expand Down

0 comments on commit 25ba328

Please sign in to comment.