From ac20bd972a3fb623d0c06f47446e9dd7e9322de7 Mon Sep 17 00:00:00 2001 From: Yaroslav Komarov Date: Fri, 16 Apr 2021 16:29:58 +0500 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9A=D0=BE=D0=BC=D0=B0=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=20=D0=AF=D1=80=D0=BE=D1=81=D0=BB=D0=B0=D0=B2,=20=D0=B7=D0=B0?= =?UTF-8?q?=D0=B4=D0=B0=D0=BD=D0=B8=D1=8F=201=20-=205?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/task_1/index.ts | 24 ++++++++++++++++++ src/task_2/index.ts | 16 ++++++++++++ src/task_3/index.ts | 60 +++++++++++++++++++++++++++++++++++++++------ src/task_4/index.ts | 4 ++- src/task_5/index.ts | 22 ++++++++++++++--- 5 files changed, 115 insertions(+), 11 deletions(-) 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..9da3641 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.name; + } + public setValue(val: string): void { + if (val) { + this.name = 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: Function) { + const typeStr = type.name; + if (!this.existType(typeStr)) { + this._collection.push(new Container(type(), typeStr)) + } } - public getInstance(type: ?): ? { + public getInstance(type: Function): 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; } } @@ -59,7 +105,7 @@ class FactoryControl { const factory = new FactoryControl(); factory.register(SelectBox); -const selectBoxInstance = factory.getInstance(SelectBox); +const selectBoxInstance = factory.getInstance(SelectBox); -selectBoxInstance.setValue("sdfsdf") // компилятор TS не пропускает -selectBoxInstance.setValue(new SelectItem()) // компилятор TS пропускает +//selectBoxInstance.setValue("sdfsdf") // компилятор 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..7038623 100644 --- a/src/task_5/index.ts +++ b/src/task_5/index.ts @@ -6,11 +6,26 @@ * 2) Проверять у передаваемого объекта наличие заполненного поля. * Если поле не заполнено, то генерируется эксепшен. */ +function validate(target: Object, propKey: string): any { + if (!(propKey in Object.keys(target) && (target as any)[propKey])) { + throw Error('invalid'); + } + const descriptor: PropertyDescriptor = { + set: function(value: any) { + if (typeof value === typeof target){ + this.value = value; + } else { + throw Error('invalid value'); + } + } + } + return descriptor; +} class ValueExample1 { public value: string; public id: number; - public constructor(value?: string, id?: number) { + public constructor(value: string = '', id: number = 0) { this.value = value; this.id = id; } @@ -19,16 +34,17 @@ class ValueExample1 { class ValueExample2 { public undefinedProp: undefined; public booleanProp: boolean; - public constructor(undefinedProp?: undefined, booleanProp?: boolean) { + public constructor(undefinedProp?: undefined, booleanProp: boolean = false) { this.undefinedProp = undefinedProp; this.booleanProp = booleanProp; } } -class Example { +class Example1 { @validate(ValueExample1, "id") public propValueExample1: any; @validate(ValueExample2, "booleanProp") public propValueExample2: any; } + \ No newline at end of file From 164d516b7d5a99ff5c1b575050af5f84291fcaf1 Mon Sep 17 00:00:00 2001 From: Yaroslav Komarov Date: Tue, 20 Apr 2021 14:47:08 +0500 Subject: [PATCH 2/2] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=202,?= =?UTF-8?q?=205?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/task_3/index.ts | 14 +++++++------- src/task_5/index.ts | 41 ++++++++++++++++++----------------------- 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/task_3/index.ts b/src/task_3/index.ts index 9da3641..02892fc 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -30,11 +30,11 @@ class TextBox extends Control { } public getValue(): string { - return this.name; + return this.value; } public setValue(val: string): void { if (val) { - this.name = val; + this.value = val; } } } @@ -80,14 +80,14 @@ class FactoryControl { this._collection = []; } - public register(type: Function) { + public register>(type: new () => T) { const typeStr = type.name; if (!this.existType(typeStr)) { - this._collection.push(new Container(type(), typeStr)) + this._collection.push(new Container(new type(), typeStr)) } } - public getInstance(type: Function): Control { + public getInstance(type: new () => Control): Control { const typeStr = type.name; const container = this._collection.find(x => x.type === typeStr); if (container){ @@ -105,7 +105,7 @@ class FactoryControl { const factory = new FactoryControl(); factory.register(SelectBox); -const selectBoxInstance = factory.getInstance(SelectBox); +const selectBoxInstance = factory.getInstance(SelectBox); -//selectBoxInstance.setValue("sdfsdf") // компилятор TS не пропускает +selectBoxInstance.setValue("sdfsdf") // компилятор TS не пропускает selectBoxInstance.setValue(new SelectItem()) // компилятор TS пропускает \ No newline at end of file diff --git a/src/task_5/index.ts b/src/task_5/index.ts index 7038623..5146396 100644 --- a/src/task_5/index.ts +++ b/src/task_5/index.ts @@ -6,38 +6,32 @@ * 2) Проверять у передаваемого объекта наличие заполненного поля. * Если поле не заполнено, то генерируется эксепшен. */ -function validate(target: Object, propKey: string): any { - if (!(propKey in Object.keys(target) && (target as any)[propKey])) { - throw Error('invalid'); - } - const descriptor: PropertyDescriptor = { - set: function(value: any) { - if (typeof value === typeof target){ - this.value = value; - } else { - throw Error('invalid value'); +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; } - return descriptor; } class ValueExample1 { - public value: string; - public id: number; - public constructor(value: string = '', id: number = 0) { - 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 = false) { - this.undefinedProp = undefinedProp; - this.booleanProp = booleanProp; - } + public booleanProp?: boolean; } class Example1 { @@ -47,4 +41,5 @@ class Example1 { @validate(ValueExample2, "booleanProp") public propValueExample2: any; } - \ No newline at end of file + +let ex1 = new Example1();