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