-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
131 lines (113 loc) · 3.39 KB
/
app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// advanced types - Type casting
const userInputElement = document.getElementById('user-input');
if (userInputElement) {
(userInputElement as HTMLInputElement).value = 'Hi There';
}
// decorators - building useful decorators
function withTemplate(template: string, hookId: string) {
return function (target: any) {
const hookElement = document.getElementById(hookId);
const personName = new target();
console.log(hookElement);
if (hookElement) {
hookElement.innerHTML = template;
hookElement.querySelector('p')!.textContent = personName.name;
}
};
}
@withTemplate('<p>My person object<p/>', 'person-name')
class Person {
name = 'cesar';
constructor() {
console.log('creating a person object');
}
}
const personInstance = new Person();
console.log(personInstance);
// decorators - autobind example
const button = document.querySelector('button')!;
function Autobind(_: any, _2: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
const adjustDescriptor: PropertyDescriptor = {
configurable: true,
enumerable: false,
get() {
const boundFn = originalMethod.bind(this);
return boundFn;
},
};
return adjustDescriptor;
}
class Printer {
message = 'this works';
@Autobind
showMessage() {
console.log(this.message);
}
}
const prt = new Printer();
button.addEventListener('click', prt.showMessage);
// decorators - validation with decorators
interface ValidatorConfig {
[property: string]: {
[validatableProperty: string]: string[]; //['required', 'positive']
};
}
const registeredValidators: ValidatorConfig = {};
function Required(target: any, propertyName: string) {
registeredValidators[target.constructor.name] = {
...registeredValidators[target.constructor.name],
[propertyName]: [...(registeredValidators[target.constructor.name]?.[propertyName] ?? []), 'required'],
};
}
function PositiveNumber(target: any, propertyName: string) {
registeredValidators[target.constructor.name] = {
...registeredValidators[target.constructor.name],
[propertyName]: [...(registeredValidators[target.constructor.name]?.[propertyName] ?? []), 'positive'],
};
}
function validate(obj: any) {
const objValidatorConfig = registeredValidators[obj.constructor.name];
if (!objValidatorConfig) {
return true;
}
let isValid = true;
for (const prop in objValidatorConfig) {
for (const validator of objValidatorConfig[prop]) {
switch (validator) {
case 'required':
isValid = isValid && !!obj[prop];
break;
case 'positive':
isValid = isValid && obj[prop] > 0;
break;
}
}
}
return isValid;
}
class Course {
@Required
title: string;
@PositiveNumber
price: number;
constructor(title: string, price: number) {
this.title = title;
this.price = price;
}
}
const courseForm = document.querySelector('form')!;
courseForm.addEventListener('submit', (event) => {
event.preventDefault();
const titleElement = document.getElementById('formTitle') as HTMLInputElement;
const priceElement = document.getElementById('formPrice') as HTMLInputElement;
const title = titleElement.value;
const price = +priceElement.value;
const createdCourse = new Course(title, price);
if (!validate(createdCourse)) {
alert('Invalid input, please try again');
return;
}
console.log(createdCourse);
courseForm.reset();
});