From 653a83b8111e16cfc23346afe367f6df369cda59 Mon Sep 17 00:00:00 2001 From: Maxim Date: Thu, 15 Apr 2021 23:41:31 +0500 Subject: [PATCH 1/4] tasks --- src/task_1/index.ts | 25 +++++++++++++++++++++++++ src/task_2/index.ts | 36 ++++++++++++++++++++++++++++++++---- src/task_4/index.ts | 2 +- src/task_5/index.ts | 39 +++++++++++++++++++++++++++++++++++++++ src/tsconfig.json | 6 ++++++ 5 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 src/tsconfig.json diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 53a04d7..fd7e339 100644 --- a/src/task_1/index.ts +++ b/src/task_1/index.ts @@ -9,6 +9,25 @@ * result of the addition operation ${a} + ${b} = ${рассчитанное значение} */ +function DecorateRu(target: Object, method: string, descriptor: PropertyDescriptor) { + let exec = descriptor.value; + descriptor.value = function(...args: any) { + let returnValue = exec.apply(this, args); + console.log(`результат сложения ${this.a} + ${this.b} = ${returnValue}`) + return returnValue; + } +} + +function DecorateEn(target: Object, method: string, descriptor: PropertyDescriptor) { + let exec = descriptor.value; + descriptor.value = function(...args: any) { + let returnValue = exec.apply(this, args); + console.log(`result of the addition operation ${this.a} + ${this.b} = ${returnValue}`) + return returnValue; + } + +} + class Calculator { protected a: number = 0; protected b: number = 0; @@ -22,7 +41,13 @@ class Calculator { } } + @DecorateEn + @DecorateRu public exec(): string { return (this.a + this.b).toString(); } } + +const test = new Calculator(10, 20); +test.exec(); + diff --git a/src/task_2/index.ts b/src/task_2/index.ts index 06535aa..8cf140b 100644 --- a/src/task_2/index.ts +++ b/src/task_2/index.ts @@ -5,10 +5,38 @@ * Когда присваивается некорректный e-mail возбуждается ошибка. */ -class Example { - public email: string = ""; +function validator(target: Object, propertyKey: string | symbol) { + let _val = this[propertyKey]; + + let descriptor: PropertyDescriptor = { + get() { + return _val; + }, + set(newValue: string) { + _val = newValue; + if (!new RegExp(/^([a-z0-9_\.-])+@[a-z0-9-]+\.([a-z]{2,4}\.)?[a-z]{2,4}$/i).test(newValue)) { + console.log('invalid email'); + } else { + console.log('valid email'); + } + return _val; + } + } + + Object.defineProperty(target, propertyKey, { + get: descriptor.get, + set: descriptor.set + }); + +} + +class Example1 { + + @validator + public email: string; + } -let exampleInstance = new Example(); +let exampleInstance = new Example1(); exampleInstance.email = "fkkldfjg"; // генерирует эксепшен -exampleInstance.email = "misha@mail.ru"; // выводит в консоль e-mail valid +exampleInstance.email = "misha@mail.ru"; // выводит в консоль e-mail valid \ No newline at end of file diff --git a/src/task_4/index.ts b/src/task_4/index.ts index dc0e179..6581713 100644 --- a/src/task_4/index.ts +++ b/src/task_4/index.ts @@ -6,6 +6,6 @@ function getProperty(obj: T, key: K): T[K] { return obj[key]; } -const x = undefined; +const x = {m: 'some value'}; console.log(getProperty(x, "m")); \ No newline at end of file diff --git a/src/task_5/index.ts b/src/task_5/index.ts index d9fea4b..7bd326c 100644 --- a/src/task_5/index.ts +++ b/src/task_5/index.ts @@ -7,6 +7,36 @@ * Если поле не заполнено, то генерируется эксепшен. */ +function validate(type: T, value: string) { + return function validate(target: Object, propertyName: string) { + let propertyValue: T; + + let descriptor: PropertyDescriptor = { + get() { + return propertyValue; + }, + set(newVal: T) { + if (newVal === type) { + console.log('valid type'); + propertyValue = newVal; + } else { + throw 'exception'; + } + if (!propertyName[value]) { + throw 'field is empty'; + } + return propertyValue; + } + } + + Object.defineProperty(target, propertyName, { + get: descriptor.get, + set: descriptor.set + }); + + } +} + class ValueExample1 { public value: string; public id: number; @@ -31,4 +61,13 @@ class Example { @validate(ValueExample2, "booleanProp") public propValueExample2: any; + + constructor (propValueExample1: any, propValueExample2: any) { + this.propValueExample1 = propValueExample1; + this.propValueExample2 = propValueExample2; + } } + +const example = new Example(ValueExample1, ValueExample2); +const e1 = new ValueExample1("21", 21); +const e2 = new ValueExample2(); diff --git a/src/tsconfig.json b/src/tsconfig.json new file mode 100644 index 0000000..fe8723d --- /dev/null +++ b/src/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "target": "ES6", + "experimentalDecorators": true + } +} \ No newline at end of file From e7bf4972d43b190bf09f7085195b68f024db3c44 Mon Sep 17 00:00:00 2001 From: mmikriukov Date: Sat, 17 Apr 2021 13:18:41 +0500 Subject: [PATCH 2/4] update --- src/task_3/index.ts | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/task_3/index.ts b/src/task_3/index.ts index afa588c..25a8613 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -20,6 +20,15 @@ abstract class Control { } /**Класс описывает TextBox контрол */ class TextBox extends Control { + protected value: string; + + public getValue(): string { + return this.value; + } + + public setValue(val: string): void { + this.value = val; + } } /**value контрола selectBox */ class SelectItem { @@ -29,6 +38,15 @@ class SelectItem { /**Класс описывает SelectBox контрол */ class SelectBox extends Control { + protected value: SelectItem; + + public getValue(): SelectItem { + return this.value; + } + + public setValue(val: SelectItem): void { + this.value = val; + } } class Container { @@ -45,10 +63,21 @@ class FactoryControl { this._collection = []; } - public register(type: ?) { + public register>(type: new () => T): void { + const instanceType: string = type.name; + if (this.existType(instanceType)) { + console.log('error'); + } + + let instance = new type(); + + this._collection.push({ type: instanceType, instance: instance}); } - public getInstance(type: ?): ? { + public getInstance(type: new () => Control): Control { + const typeInst = type.prototype.constructor.name; + const instance = this._collection.filter(element => element.type === typeInst)[0].instance + return instance; } private existType(type: string) { From 5cf8530d9cc9273fb7578dc000ab2090d09899d6 Mon Sep 17 00:00:00 2001 From: mmikriukov Date: Sat, 17 Apr 2021 13:21:27 +0500 Subject: [PATCH 3/4] update --- src/task_3/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/task_3/index.ts b/src/task_3/index.ts index 25a8613..f45d217 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -75,7 +75,7 @@ class FactoryControl { } public getInstance(type: new () => Control): Control { - const typeInst = type.prototype.constructor.name; + const typeInst: Control = new type(); const instance = this._collection.filter(element => element.type === typeInst)[0].instance return instance; } From 8a13f4da5fb9aeb7362030b3318f962e1931e806 Mon Sep 17 00:00:00 2001 From: mmikriukov Date: Sat, 17 Apr 2021 13:22:11 +0500 Subject: [PATCH 4/4] update --- src/task_3/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/task_3/index.ts b/src/task_3/index.ts index f45d217..928499f 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -76,8 +76,8 @@ class FactoryControl { public getInstance(type: new () => Control): Control { const typeInst: Control = new type(); - const instance = this._collection.filter(element => element.type === typeInst)[0].instance - return instance; + const instance = this._collection.filter(element => element.type === typeInst.name); + return instance[0].instance; } private existType(type: string) {