Skip to content

Commit

Permalink
feat: assign optional arguments in the constructor for JS generator (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mahakporwal02 authored Jan 15, 2022
1 parent 8ff4bb9 commit 5622923
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Array [
email;
constructor(input) {
if (input.hasOwnProperty('email')) {
this.email = input.email;
}
}
get email() { return this.email; }
Expand Down
8 changes: 6 additions & 2 deletions examples/javascript-use-cjs/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class Person {
email;
constructor(input) {
if (input.hasOwnProperty('email')) {
this.email = input.email;
}
}
get email() { return this.email; }
Expand All @@ -26,7 +28,9 @@ class Root {
person;
constructor(input) {
if (input.hasOwnProperty('person')) {
this.person = input.person;
}
}
get person() { return this.person; }
Expand Down
8 changes: 6 additions & 2 deletions examples/javascript-use-esm/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class Person {
email;
constructor(input) {
if (input.hasOwnProperty('email')) {
this.email = input.email;
}
}
get email() { return this.email; }
Expand All @@ -27,7 +29,9 @@ class Root {
person;
constructor(input) {
if (input.hasOwnProperty('person')) {
this.person = input.person;
}
}
get person() { return this.person; }
Expand Down
8 changes: 7 additions & 1 deletion src/generators/javascript/renderers/ClassRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ export const JS_DEFAULT_CLASS_PRESET: ClassPreset<ClassRenderer> = {
},
ctor({ renderer, model }) {
const properties = model.properties || {};
const assigments = Object.entries(properties).filter(([propertyName]) => model.isRequired(propertyName)).map(([propertyName, property]) => {
const assigments = Object.entries(properties).map(([propertyName, property]) => {
if (!model.isRequired(propertyName)) {
propertyName = renderer.nameProperty(propertyName, property);
return `if (input.hasOwnProperty('${propertyName}')) {
this.${propertyName} = input.${propertyName};
}`;
}
propertyName = renderer.nameProperty(propertyName, property);
return `this.${propertyName} = input.${propertyName};`;
});
Expand Down
6 changes: 6 additions & 0 deletions test/generators/javascript/JavaScriptGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ describe('JavaScriptGenerator', () => {
this.city = input.city;
this.state = input.state;
this.houseNumber = input.houseNumber;
if (input.hasOwnProperty('marriage')) {
this.marriage = input.marriage;
}
if (input.hasOwnProperty('members')) {
this.members = input.members;
}
this.arrayType = input.arrayType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ class Address {
this.city = input.city;
this.state = input.state;
this.houseNumber = input.houseNumber;
if (input.hasOwnProperty('marriage')) {
this.marriage = input.marriage;
}
if (input.hasOwnProperty('members')) {
this.members = input.members;
}
this.arrayType = input.arrayType;
if (input.hasOwnProperty('otherModel')) {
this.otherModel = input.otherModel;
}
}
get streetName() { return this.streetName; }
Expand Down Expand Up @@ -96,7 +105,16 @@ class Address {
this.city = input.city;
this.state = input.state;
this.houseNumber = input.houseNumber;
if (input.hasOwnProperty('marriage')) {
this.marriage = input.marriage;
}
if (input.hasOwnProperty('members')) {
this.members = input.members;
}
this.arrayType = input.arrayType;
if (input.hasOwnProperty('otherModel')) {
this.otherModel = input.otherModel;
}
}
get streetName() { return this.streetName; }
Expand Down

0 comments on commit 5622923

Please sign in to comment.