From 2543344a4b32a710530ea558775f5d2276d596e1 Mon Sep 17 00:00:00 2001 From: M1dnighter <57962832+M1dnighter@users.noreply.github.com> Date: Sat, 17 Apr 2021 10:51:25 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B8=D0=B6=D0=B5=D0=B3=D0=BE=D1=80?= =?UTF-8?q?=D0=BE=D0=B4=D0=BE=D0=B2=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/task_1/index.ts | 9 +++++++++ src/task_2/index.ts | 14 +++++++++++++- src/task_3/index.ts | 42 +++++++++++++++++++++++++++++++++++------- src/task_4/index.ts | 2 +- 4 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 53a04d7..66039a6 100644 --- a/src/task_1/index.ts +++ b/src/task_1/index.ts @@ -26,3 +26,12 @@ class Calculator { return (this.a + this.b).toString(); } } +class DecorateEn extends Calculator { + public exec(): string{ + return `result of the addition operation ${this.a} + ${this.b} = ${super.exec()}`} + } +class DecorateRu extends Calculator { + public exec(): string{ + return `результат сложения ${this.a} + ${this.b} = ${super.exec()}`} + } + diff --git a/src/task_2/index.ts b/src/task_2/index.ts index 06535aa..be36ede 100644 --- a/src/task_2/index.ts +++ b/src/task_2/index.ts @@ -4,8 +4,20 @@ * Когда присваивается корректный e-mail в консоль выводится сообщение email valid. * Когда присваивается некорректный e-mail возбуждается ошибка. */ +function validDecorator(target: Object, propertyKey: string):any{ + let descriptor:PropertyDescriptor = { + set(email : string) { + if (email.match(/^([\w.*-]+@([\w-]+\.)+[\w-]{2,4})?$/g)){ + console.log("email valid"); + } + else throw "Invalid" + } + } + return descriptor; +} -class Example { +class Example{ + @validDecorator public email: string = ""; } diff --git a/src/task_3/index.ts b/src/task_3/index.ts index afa588c..aa0887f 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -12,28 +12,42 @@ abstract class Control { public name: string = ""; - protected value: T; + protected value: T | undefined; /**взять значение из контрола */ - public abstract getValue(): T; + public abstract getValue(): T | undefined; /**установить значение в контрол */ public abstract setValue(val: T): void; } /**Класс описывает TextBox контрол */ class TextBox extends Control { + protected val: string | undefined + public getValue(): string | undefined{ + return this.val; + } + public setValue(val: string): void{ + this.val = val; + } } /**value контрола selectBox */ class SelectItem { - public value: string; - public id: number; + public value: string | undefined; + public id: number | undefined; } /**Класс описывает SelectBox контрол */ class SelectBox extends Control { + protected val: SelectItem | undefined; + public getValue(): SelectItem | undefined{ + return this.val; + } + public setValue(val: SelectItem): void{ + this.val = val; + } } class Container { public instance: Control; - public type: string; + public type: string | undefined; } /**Фабрика которая отвечает за создание экземпляров контролов */ @@ -45,10 +59,24 @@ class FactoryControl { this._collection = []; } - public register(type: ?) { + public register>(type: new () => T){ + const typeInstance: T = new type(); + if (this.existType(typeInstance.name)) { + throw new Error("Already exists") + } + if (!(typeInstance instanceof Control)){ + throw new Error("Invalid type"); + + } } - public getInstance(type: ?): ? { + public getInstance(type: new () => Control): Control{ + const typeInstance: Control = new type(); + const instances = this._collection.filter(g => g.type === typeInstance.name); + if (instances.length > 0) { + return instances[0].instance; + } + throw Error("Instance not found"); } private existType(type: string) { diff --git a/src/task_4/index.ts b/src/task_4/index.ts index dc0e179..d0e0a4a 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": "Ы" }; console.log(getProperty(x, "m")); \ No newline at end of file