Advertisement
boky8

TypeScript class generator

Oct 29th, 2019
691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. {
  3.     "Person": {
  4.         "firstName": "string",
  5.         "lastName": "string",
  6.         "location": "Location"
  7.     },
  8.     "Location": {
  9.         "country": "string",
  10.         "region": "string",
  11.         "city": "string",
  12.         "address": "string"
  13.     }
  14. }
  15. */
  16.  
  17. generateClass('Person', {
  18.     firstName: 'string',
  19.     lastName: 'string',
  20.     location: 'Location'
  21. });
  22.  
  23. function generateClass(className, classProps) {
  24.     const props = Object.entries(classProps);
  25.  
  26.     const privatePropLines = generatePrivatePropsLines(props);
  27.     const constructorLines = generateConstructorLines(className);
  28.     const getSetLines = generateGetSetLines(props);
  29.     const classLines = generateClassLines(className, privatePropLines, constructorLines, getSetLines);
  30.     const classText = generateTextFromLines(classLines);
  31.  
  32.     return classText;
  33. }
  34.  
  35. function generatePrivatePropsLines(props) {
  36.     return [
  37.         ...props.map(([name, type]) => generatePrivatePropLine(name, type)),
  38.         ''
  39.     ];
  40. }
  41.  
  42. function generatePrivatePropLine(name, type) {
  43.     type = type || 'unknown';
  44.     return `private _${name}: ${type};`;
  45. }
  46.  
  47. function generateConstructorLines(className) {
  48.     return [
  49.         `constructor(init: Partial<${className}> = {}) {`,
  50.             '\tObject.assign(this, init});',
  51.         '}',
  52.         ''
  53.     ];
  54. }
  55.  
  56. function generateGetSetLines(props) {
  57.     const lines = [];
  58.  
  59.     for (const [name, type] of props) {
  60.         lines.push(
  61.             ...generateGetLines(name, type),
  62.             ...generateSetLines(name, type),
  63.             ''
  64.         );
  65.     }
  66.  
  67.     return lines;
  68. }
  69.  
  70. function generateGetLines(name, type) {
  71.     return [
  72.         `get ${name}(): ${type} {`,
  73.             `\treturn this._${name};`,
  74.         '}'
  75.     ];
  76. }
  77.  
  78. function generateSetLines(name, type) {
  79.     return [
  80.         `set ${name}(value: ${type}) {\n`,
  81.             `\tthis._${name} = value;`,
  82.         '}'
  83.     ];
  84. }
  85.  
  86. function generateClassLines(className, privatePropLines, constructorLines, getSetLines) {
  87.     return [
  88.         `export class ${className} {`,
  89.             '',
  90.             ...privatePropLines.map(x => `\t${x}`),
  91.             ...constructorLines.map(x => `\t${x}`),
  92.             ...getSetLines.map(x => `\t${x}`),
  93.         '}'
  94.     ];
  95. }
  96.  
  97. function replaceTabs(line) {
  98.     return line.replace(/\t/, '    ');
  99. }
  100.  
  101. function generateTextFromLines(lines) {
  102.     return lines.map(x => replaceTabs(x)).join('\n');
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement