Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. export interface IUser {
  2. firstName?: string;
  3. lastName?: string;
  4. age?: number;
  5. weight?: number
  6. }
  7.  
  8. const userDto: IUser = { // DTO (data transfer object)
  9. firstName: 'Cory',
  10. lastName: 'Rylan',
  11. age: 100,
  12. weight: 150
  13. };
  14. // mock API data instance for User
  15. export class User {
  16. private _firstName: string;
  17. private _lastName: string;
  18. private _age: number;
  19.  
  20. get firstName() {
  21. return this._firstName;
  22. }
  23.  
  24. get lastName() {
  25. return this._lastName;
  26. }
  27.  
  28. get fullName() {
  29. return `${this.firstName} ${this.lastName}`;
  30. }
  31.  
  32. get age() {
  33. return this._age;
  34. }
  35.  
  36. constructor(firstName: string, lastName: string, age: number) {
  37. this.setName(firstName, lastName);
  38. this.setAge(age);
  39. }
  40.  
  41. setName(firstName: string, lastName: string) {
  42. if (this.validName(firstName) && this.validName(lastName)) {
  43. this._firstName = firstName;
  44. this._lastName = lastName;
  45. }
  46. }
  47.  
  48. setAge(age: number) {
  49. if (age >= 18) {
  50. this._age = age;
  51. } else {
  52. throw new Error('User age must be greater than 18');
  53. }
  54. }
  55.  
  56. private validName(name: string) {
  57. if (name.length > 0 && /^[a-zA-Z]+$/.test(name)) {
  58. return true
  59. } else {
  60. throw new Error('Invalid name format');
  61. }
  62. }
  63. }
  64.  
  65. const user = new User('John', 'Doe', 18);
  66. user.setName('Jack', 'Doe');
  67. console.log(user.fullName); // Jack Doe
  68.  
  69. const user2 = new User('John', 'Smith', 17);
  70. // Error: User age must be greater than 18
  71. -----------
  72. export interface IUser {
  73. firstName?: string;
  74. lastName?: string;
  75. age?: number;
  76. weight?: number
  77. }
  78.  
  79. export class User {
  80. private state: IUser = { };
  81.  
  82. get firstName() {
  83. return this.state.firstName;
  84. }
  85.  
  86. get lastName() {
  87. return this.state.lastName;
  88. }
  89.  
  90. get fullName() {
  91. return `${this.firstName} ${this.lastName}`;
  92. }
  93.  
  94. get age() {
  95. return this.state.age;
  96. }
  97.  
  98. constructor(firstName: string, lastName: string, age: number) {
  99. this.setName(firstName, lastName);
  100. this.setAge(age);
  101. }
  102.  
  103. setName(firstName: string, lastName: string) {
  104. if (this.validName(firstName) && this.validName(lastName)) {
  105. this.state.firstName = firstName;
  106. this.state.lastName = lastName;
  107. }
  108. }
  109.  
  110. setAge(age: number) {
  111. if (age >= 18) {
  112. this.state.age = age;
  113. } else {
  114. throw new Error('User age must be greater than 18');
  115. }
  116. }
  117.  
  118. private validName(name: string) {
  119. if (name.length > 0 && /^[a-zA-Z]+$/.test(name)) {
  120. return true
  121. } else {
  122. throw new Error('Invalid name format');
  123. }
  124. }
  125. }
  126.  
  127.  
  128. // We move the internal private properties to be on a single private internal state property. By moving the
  129. // internal private properties onto a single object, we can use the state property to easily copy or hydrate the // class with the data from our API. Let’s look at the hydration logic that will copy our data over.
  130.  
  131. export interface Type<T> extends Function {
  132. new (...args: any[]): T;
  133. }
  134.  
  135. export class DomainConverter {
  136. static fromDto<T>(domain: Type<T>, dto: any) {
  137. const instance = Object.create(domain.prototype);
  138. instance.state = dto;
  139. return instance as T;
  140. }
  141.  
  142. static toDto<T>(domain: any) {
  143. return domain.state as T;
  144. }
  145. }
  146.  
  147. const userDto: IUser = {
  148. firstName: 'Cory',
  149. lastName: 'Rylan',
  150. age: 27,
  151. weight: 150
  152. };
  153.  
  154. const user: User = DomainConverter.fromDto<User>(User, userDto);
  155.  
  156. const userData: IUser = DomainConverter.toDto<User>(user);
  157.  
  158. console.log(userData);
  159. // { firstName: "Cory", lastName: "Rylan", age: 27, weight: 150 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement