diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 53a04d7..c4c4d03 100644 --- a/src/task_1/index.ts +++ b/src/task_1/index.ts @@ -9,7 +9,11 @@ * result of the addition operation ${a} + ${b} = ${рассчитанное значение} */ -class Calculator { +interface ICalculator { + exec(): string; +} + +class Calculator implements ICalculator{ protected a: number = 0; protected b: number = 0; @@ -26,3 +30,42 @@ class Calculator { return (this.a + this.b).toString(); } } + +class BaseDecorator implements ICalculator +{ + protected calculator: ICalculator + protected a: number = 0; + protected b: number = 0; + + constructor(calc: ICalculator) { + this.calculator = calc; + } + + public exec(): string + { + return this.calculator.exec(); + } +} + +class DecorateEn extends BaseDecorator +{ + public exec(): string + { + return `Result of the addition operation ${super.exec()}`; + } +} + +class DecorateRu extends BaseDecorator +{ + public exec(): string + { + return `Результат сложения ${super.exec()}`; + } +} + +//Test region + +let q = new Calculator(15, 78); +let ruCalc = new DecorateRu(q); +let enCalc = new DecorateEn(q); +console.log(`${q.exec()} \n ${enCalc.exec()} \n ${ruCalc.exec()}`) diff --git a/src/task_2/index.ts b/src/task_2/index.ts index 06535aa..470877a 100644 --- a/src/task_2/index.ts +++ b/src/task_2/index.ts @@ -4,11 +4,31 @@ * Когда присваивается корректный e-mail в консоль выводится сообщение email valid. * Когда присваивается некорректный e-mail возбуждается ошибка. */ +function validateBox(target: Object, propertyKey: string | symbol): any { + const regExp = (email: string) =>{ + return /[a-zA-Z0-9]+@[a-zA-Z0-9]+.[a-zA-Z0-9]+/.test(email) + } + let email: string; + let descriptor: PropertyDescriptor = { + set(newEmail: string) { + debugger + if (!regExp(newEmail)) { + console.log("Invalid email") + throw 'Invalid email.' + } + console.log('email valid'); + email = newEmail; + } + } + + return descriptor; +} class Example { + @validateBox public email: string = ""; } let exampleInstance = new Example(); -exampleInstance.email = "fkkldfjg"; // генерирует эксепшен +//exampleInstance.email = "fkkldfjg"; // генерирует эксепшен exampleInstance.email = "misha@mail.ru"; // выводит в консоль e-mail valid diff --git a/src/task_3/index.ts b/src/task_3/index.ts index afa588c..b9bebaf 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -20,6 +20,12 @@ abstract class Control { } /**Класс описывает TextBox контрол */ class TextBox extends Control { + getValue(): string { + return ""; + } + + setValue(val: string): void { + } } /**value контрола selectBox */ class SelectItem { @@ -29,11 +35,23 @@ class SelectItem { /**Класс описывает SelectBox контрол */ class SelectBox extends Control { + getValue(): SelectItem { + return undefined; + } + + setValue(val: SelectItem): void { + } } class Container { public instance: Control; public type: string; + + + constructor(instance: Control, type: string) { + this.instance = instance; + this.type = type; + } } /**Фабрика которая отвечает за создание экземпляров контролов */ @@ -45,10 +63,17 @@ class FactoryControl { this._collection = []; } - public register(type: ?) { + public register>(type: new () => T) { + const instanceType = (type).name; + if(this.existType(instanceType)) { + return; + } + this._collection.push(new Container(new type(), instanceType)); } - public getInstance(type: ?): ? { + public getInstance(type: new () => Control): Control { + const instanceType = type.toString() + return this._collection.find(control => control.type === instanceType).instance; } private existType(type: string) { @@ -61,5 +86,5 @@ factory.register(SelectBox); const selectBoxInstance = factory.getInstance(SelectBox); -selectBoxInstance.setValue("sdfsdf") // компилятор TS не пропускает +//selectBoxInstance.setValue("sdfsdf") // компилятор TS не пропускает selectBoxInstance.setValue(new SelectItem()) // компилятор TS пропускает diff --git a/src/task_4/index.ts b/src/task_4/index.ts index dc0e179..eef14ff 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:0}; -console.log(getProperty(x, "m")); \ No newline at end of file +console.log(getProperty(x, "m")); diff --git a/src/task_5/index.ts b/src/task_5/index.ts index d9fea4b..6020afc 100644 --- a/src/task_5/index.ts +++ b/src/task_5/index.ts @@ -7,6 +7,31 @@ * Если поле не заполнено, то генерируется эксепшен. */ +function validate(type: new () => T, prop: P) { + return (target: Object, propertyKey: string | symbol) : any => { + let property: T; + let descriptor: PropertyDescriptor = { + get() { + return property; + }, + set(value: T) { + if(!(value instanceof type)){ + throw new Error(`Input value has another type: ${type.name}`) + } + if (!(prop in value)) { + throw new Error(`${prop} not exist in value`) + } + if(!value[prop]) + throw new Error("Value must be defined") + console.log("VALID") + property = value; + } + } + + return descriptor; + } +} + class ValueExample1 { public value: string; public id: number; @@ -25,10 +50,16 @@ class ValueExample2 { } } -class Example { +class ExampleT { @validate(ValueExample1, "id") public propValueExample1: any; @validate(ValueExample2, "booleanProp") public propValueExample2: any; } + +let quarta = new ExampleT(); +let valueTest1 = new ValueExample1("Sharpest", 32); +let valueTest2 = new ValueExample2() +quarta.propValueExample1 = valueTest1; +quarta.propValueExample2 = valueTest2;