diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 53a04d7..297bc30 100644 --- a/src/task_1/index.ts +++ b/src/task_1/index.ts @@ -26,3 +26,27 @@ class Calculator { return (this.a + this.b).toString(); } } + +class BaseDecorator extends Calculator { + protected wrappee: Calculator; + + constructor(a: number, b: number) { + super(a, b); + this.wrappee = new Calculator(a, b); + } +} + +class DecorateRu extends BaseDecorator{ + public exec(): string { + return `результат сложения ${this.a} + ${this.b} = ${this.wrappee.exec()}`; + } +} + +class DecorateEn extends BaseDecorator{ + public exec(): string { + return `result of the addition operation ${this.a} + ${this.b} = ${this.wrappee.exec()}`; + } +} + +const tmp = new DecorateRu(1, 3); +console.log(tmp.exec()); \ No newline at end of file diff --git a/src/task_2/index.ts b/src/task_2/index.ts index 06535aa..5e3efc6 100644 --- a/src/task_2/index.ts +++ b/src/task_2/index.ts @@ -5,7 +5,23 @@ * Когда присваивается некорректный e-mail возбуждается ошибка. */ +function decoratorProp(target: Object, propertyKey: string | symbol): any { + const descriptor: PropertyDescriptor = { + set: function (email: string) { + const re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; + if (re.test(email)) { + this.value = email; + console.log('email valid'); + } else { + throw Error('email invalid'); + } + } + } + return descriptor; +} + class Example { + @decoratorProp public email: string = ""; } diff --git a/src/task_3/index.ts b/src/task_3/index.ts index afa588c..02892fc 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -10,9 +10,14 @@ /**Базовый класс для контролов */ abstract class Control { - public name: string = ""; + public name: string; protected value: T; + + constructor(value: T, name: string = '') { + this.name = name; + this.value = value; + } /**взять значение из контрола */ public abstract getValue(): T; /**установить значение в контрол */ @@ -20,20 +25,50 @@ abstract class Control { } /**Класс описывает TextBox контрол */ class TextBox extends Control { + constructor(value: string = '', name?: string){ + super(value, name); + } + + public getValue(): string { + return this.value; + } + public setValue(val: string): void { + if (val) { + this.value = val; + } + } } /**value контрола selectBox */ class SelectItem { public value: string; public id: number; + + constructor(value: string = '', id: number = 0) { + this.value = value; + this.id = id; + } } /**Класс описывает SelectBox контрол */ class SelectBox extends Control { + public getValue(): SelectItem { + return this.value; + } + public setValue(val: SelectItem): void { + if (val) { + this.value = val; + } + } } class Container { public instance: Control; public type: string; + + constructor(instance: Control, type: string) { + this.instance = instance; + this.type = type; + } } /**Фабрика которая отвечает за создание экземпляров контролов */ @@ -45,13 +80,24 @@ class FactoryControl { this._collection = []; } - public register(type: ?) { + public register>(type: new () => T) { + const typeStr = type.name; + if (!this.existType(typeStr)) { + this._collection.push(new Container(new type(), typeStr)) + } } - public getInstance(type: ?): ? { + public getInstance(type: new () => Control): Control { + const typeStr = type.name; + const container = this._collection.find(x => x.type === typeStr); + if (container){ + return container.instance; + } + this.register(type); + return this._collection[this._collection.length - 1].instance; } - private existType(type: string) { + private existType(type: string): boolean{ return this._collection.filter(g => g.type === type).length > 0; } } @@ -62,4 +108,4 @@ factory.register(SelectBox); const selectBoxInstance = factory.getInstance(SelectBox); selectBoxInstance.setValue("sdfsdf") // компилятор TS не пропускает -selectBoxInstance.setValue(new SelectItem()) // компилятор TS пропускает +selectBoxInstance.setValue(new SelectItem()) // компилятор TS пропускает \ No newline at end of file diff --git a/src/task_4/index.ts b/src/task_4/index.ts index dc0e179..1b3a027 100644 --- a/src/task_4/index.ts +++ b/src/task_4/index.ts @@ -6,6 +6,8 @@ function getProperty(obj: T, key: K): T[K] { return obj[key]; } -const x = undefined; +const x = { + m: 12, +} 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..5146396 100644 --- a/src/task_5/index.ts +++ b/src/task_5/index.ts @@ -6,29 +6,40 @@ * 2) Проверять у передаваемого объекта наличие заполненного поля. * Если поле не заполнено, то генерируется эксепшен. */ +function validate(type: new () => T, propKey: string) { + return function(object: Object, propName: string): any { + const descriptor: PropertyDescriptor = { + set: function(value: T) { + if(!(value as any)[propKey]){ + throw Error('no set value'); + } + if(!(value instanceof type)){ + throw Error('invalid value'); + } else { + this.value = value; + } + } + } + return descriptor; + } +} class ValueExample1 { - public value: string; - public id: number; - public constructor(value?: string, id?: number) { - this.value = value; - this.id = id; - } + public value?: string; + public id?: number; } class ValueExample2 { public undefinedProp: undefined; - public booleanProp: boolean; - public constructor(undefinedProp?: undefined, booleanProp?: boolean) { - this.undefinedProp = undefinedProp; - this.booleanProp = booleanProp; - } + public booleanProp?: boolean; } -class Example { +class Example1 { @validate(ValueExample1, "id") public propValueExample1: any; @validate(ValueExample2, "booleanProp") public propValueExample2: any; } + +let ex1 = new Example1();