From 2854ef1d6e824ace2725d86362dde0b142d88441 Mon Sep 17 00:00:00 2001 From: Nikita Zerniukov Date: Fri, 16 Apr 2021 18:07:40 +0500 Subject: [PATCH 1/3] Save us Lord --- src/task_1/index.ts | 23 +++++++++++++++++++++++ src/task_2/index.ts | 22 +++++++++++++++++++++- src/task_3/index.ts | 21 ++++++++++++++++++--- src/task_4/index.ts | 4 ++-- src/task_5/index.ts | 16 ++++++++++++++++ 5 files changed, 80 insertions(+), 6 deletions(-) diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 53a04d7..07233b2 100644 --- a/src/task_1/index.ts +++ b/src/task_1/index.ts @@ -26,3 +26,26 @@ class Calculator { return (this.a + this.b).toString(); } } + +class DecorateRu extends Calculator +{ + public exec(): string + { + return `Результат сложения ${this.a} + ${this.b} = ${super.exec()}`; + } +} + +class DecorateEn extends Calculator +{ + public exec(): string + { + return `Result of the addition operation ${this.a} + ${this.b} = ${super.exec()}`; + } +} + +//Test region + +let q = new Calculator(15, 78); +let ruCalc = new DecorateRu(15,78); +let enCalc = new DecorateEn(15,78); +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..79324a7 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,6 +35,12 @@ class SelectItem { /**Класс описывает SelectBox контрол */ class SelectBox extends Control { + getValue(): SelectItem { + return undefined; + } + + setValue(val: SelectItem): void { + } } class Container { @@ -45,10 +57,13 @@ class FactoryControl { this._collection = []; } - public register(type: ?) { + public register>(type: new () => T) { + } - public getInstance(type: ?): ? { + public getInstance(type: new () => Control): Control { + const instanceType = type.toString() + return this._collection.find(control => control.type === instanceType); } private existType(type: string) { @@ -61,5 +76,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..aeb2960 100644 --- a/src/task_5/index.ts +++ b/src/task_5/index.ts @@ -7,6 +7,22 @@ * Если поле не заполнено, то генерируется эксепшен. */ +function validate(type: T, prop: string) { + return (target: Object, propertyKey: string | symbol) : any => { + let property: T; + let descriptor: PropertyDescriptor = { + set(value: T) { + if (!(prop in value)) { + throw `${prop} not exist in value` + } + property = value; + } + } + + return descriptor; + } +} + class ValueExample1 { public value: string; public id: number; From f69f905214adb8df08f4eddee1946bb8b442c479 Mon Sep 17 00:00:00 2001 From: Nikita Zerniukov Date: Sun, 25 Apr 2021 16:49:28 +0500 Subject: [PATCH 2/3] Some fixes --- src/task_1/index.ts | 13 +++++++++++-- src/task_3/index.ts | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 07233b2..35a0c2d 100644 --- a/src/task_1/index.ts +++ b/src/task_1/index.ts @@ -27,7 +27,16 @@ class Calculator { } } -class DecorateRu extends Calculator +class BaseDecorator extends Calculator{ + protected _mth:Calculator; + + constructor(a:number, b:number) { + super(a,b); + this._mth = new Calculator(a,b); + } +} + +class DecorateRu extends BaseDecorator { public exec(): string { @@ -35,7 +44,7 @@ class DecorateRu extends Calculator } } -class DecorateEn extends Calculator +class DecorateEn extends BaseDecorator { public exec(): string { diff --git a/src/task_3/index.ts b/src/task_3/index.ts index 79324a7..b743880 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -63,7 +63,7 @@ class FactoryControl { public getInstance(type: new () => Control): Control { const instanceType = type.toString() - return this._collection.find(control => control.type === instanceType); + return this._collection.find(control => control.type === instanceType).instance; } private existType(type: string) { From d73869cfdd936e7e9f8f3c72ff8078159b4c190d Mon Sep 17 00:00:00 2001 From: Nikita Zerniukov Date: Tue, 18 May 2021 18:10:39 +0500 Subject: [PATCH 3/3] Save us Lord --- src/task_1/index.ts | 35 +++++++++++++++++++++++------------ src/task_3/index.ts | 12 +++++++++++- src/task_5/index.ts | 21 ++++++++++++++++++--- 3 files changed, 52 insertions(+), 16 deletions(-) diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 35a0c2d..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; @@ -27,34 +31,41 @@ class Calculator { } } -class BaseDecorator extends Calculator{ - protected _mth:Calculator; +class BaseDecorator implements ICalculator +{ + protected calculator: ICalculator + protected a: number = 0; + protected b: number = 0; - constructor(a:number, b:number) { - super(a,b); - this._mth = new Calculator(a,b); + constructor(calc: ICalculator) { + this.calculator = calc; + } + + public exec(): string + { + return this.calculator.exec(); } } -class DecorateRu extends BaseDecorator +class DecorateEn extends BaseDecorator { public exec(): string { - return `Результат сложения ${this.a} + ${this.b} = ${super.exec()}`; + return `Result of the addition operation ${super.exec()}`; } } -class DecorateEn extends BaseDecorator +class DecorateRu extends BaseDecorator { public exec(): string { - return `Result of the addition operation ${this.a} + ${this.b} = ${super.exec()}`; + return `Результат сложения ${super.exec()}`; } } //Test region let q = new Calculator(15, 78); -let ruCalc = new DecorateRu(15,78); -let enCalc = new DecorateEn(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_3/index.ts b/src/task_3/index.ts index b743880..b9bebaf 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -46,6 +46,12 @@ class SelectBox extends Control { class Container { public instance: Control; public type: string; + + + constructor(instance: Control, type: string) { + this.instance = instance; + this.type = type; + } } /**Фабрика которая отвечает за создание экземпляров контролов */ @@ -58,7 +64,11 @@ class FactoryControl { } 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: new () => Control): Control { diff --git a/src/task_5/index.ts b/src/task_5/index.ts index aeb2960..6020afc 100644 --- a/src/task_5/index.ts +++ b/src/task_5/index.ts @@ -7,14 +7,23 @@ * Если поле не заполнено, то генерируется эксепшен. */ -function validate(type: T, prop: string) { +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 `${prop} not exist 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; } } @@ -41,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;