-
Notifications
You must be signed in to change notification settings - Fork 11
Микрюков Максим #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,10 +5,38 @@ | |
| * Когда присваивается некорректный e-mail возбуждается ошибка. | ||
| */ | ||
|
|
||
| class Example { | ||
| public email: string = ""; | ||
| function validator(target: Object, propertyKey: string | symbol) { | ||
| let _val = this[propertyKey]; | ||
|
|
||
| let descriptor: PropertyDescriptor = { | ||
| get() { | ||
| return _val; | ||
| }, | ||
| set(newValue: string) { | ||
| _val = newValue; | ||
| if (!new RegExp(/^([a-z0-9_\.-])+@[a-z0-9-]+\.([a-z]{2,4}\.)?[a-z]{2,4}$/i).test(newValue)) { | ||
| console.log('invalid email'); | ||
| } else { | ||
| console.log('valid email'); | ||
| } | ||
| return _val; | ||
| } | ||
| } | ||
|
|
||
| Object.defineProperty(target, propertyKey, { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это не обязательно делать. |
||
| get: descriptor.get, | ||
| set: descriptor.set | ||
| }); | ||
|
|
||
| } | ||
|
|
||
| class Example1 { | ||
|
|
||
| @validator | ||
| public email: string; | ||
|
|
||
| } | ||
|
|
||
| let exampleInstance = new Example(); | ||
| let exampleInstance = new Example1(); | ||
| exampleInstance.email = "fkkldfjg"; // генерирует эксепшен | ||
| exampleInstance.email = "misha@mail.ru"; // выводит в консоль e-mail valid | ||
| exampleInstance.email = "misha@mail.ru"; // выводит в консоль e-mail valid | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3 бала |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,15 @@ abstract class Control<T> { | |
| } | ||
| /**Класс описывает TextBox контрол */ | ||
| class TextBox extends Control<string> { | ||
| protected value: string; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Зачем переопределил? это поле имеет шаблонный тип в базовом классе. |
||
|
|
||
| public getValue(): string { | ||
| return this.value; | ||
| } | ||
|
|
||
| public setValue(val: string): void { | ||
| this.value = val; | ||
| } | ||
| } | ||
| /**value контрола selectBox */ | ||
| class SelectItem { | ||
|
|
@@ -29,6 +38,15 @@ class SelectItem { | |
|
|
||
| /**Класс описывает SelectBox контрол */ | ||
| class SelectBox extends Control<SelectItem> { | ||
| protected value: SelectItem; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Зачем переопределил? это поле имеет шаблонный тип в базовом классе. |
||
|
|
||
| public getValue(): SelectItem { | ||
| return this.value; | ||
| } | ||
|
|
||
| public setValue(val: SelectItem): void { | ||
| this.value = val; | ||
| } | ||
| } | ||
|
|
||
| class Container { | ||
|
|
@@ -45,10 +63,21 @@ class FactoryControl { | |
| this._collection = []; | ||
| } | ||
|
|
||
| public register<?>(type: ?) { | ||
| public register<T extends Control<any>>(type: new () => T): void { | ||
| const instanceType: string = type.name; | ||
| if (this.existType(instanceType)) { | ||
| console.log('error'); | ||
| } | ||
|
|
||
| let instance = new type(); | ||
|
|
||
| this._collection.push({ type: instanceType, instance: instance}); | ||
| } | ||
|
|
||
| public getInstance<?>(type: ?): ? { | ||
| public getInstance<R>(type: new () => Control<R>): Control<R> { | ||
| const typeInst: Control<R> = new type(); | ||
| const instance = this._collection.filter(element => element.type === typeInst.name); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно использовать find |
||
| return instance[0].instance; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Метод можно было сделать проще вот так |
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. работает всё верно, 3 бала |
||
| private existType(type: string) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,6 @@ function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] { | |
| return obj[key]; | ||
| } | ||
|
|
||
| const x = undefined; | ||
| const x = {m: 'some value'}; | ||
|
|
||
| console.log(getProperty(x, "m")); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3 бала |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,36 @@ | |
| * Если поле не заполнено, то генерируется эксепшен. | ||
| */ | ||
|
|
||
| function validate<T>(type: T, value: string) { | ||
| return function validate(target: Object, propertyName: string) { | ||
| let propertyValue: T; | ||
|
|
||
| let descriptor: PropertyDescriptor = { | ||
| get() { | ||
| return propertyValue; | ||
| }, | ||
| set(newVal: T) { | ||
| if (newVal === type) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. тут надо проверять тип передаваемого объекта вот так |
||
| console.log('valid type'); | ||
| propertyValue = newVal; | ||
| } else { | ||
| throw 'exception'; | ||
| } | ||
| if (!propertyName[value]) { | ||
| throw 'field is empty'; | ||
| } | ||
| return propertyValue; | ||
| } | ||
| } | ||
|
|
||
| Object.defineProperty(target, propertyName, { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это не обязательно |
||
| get: descriptor.get, | ||
| set: descriptor.set | ||
| }); | ||
|
|
||
| } | ||
| } | ||
|
|
||
| class ValueExample1 { | ||
| public value: string; | ||
| public id: number; | ||
|
|
@@ -31,4 +61,13 @@ class Example { | |
|
|
||
| @validate(ValueExample2, "booleanProp") | ||
| public propValueExample2: any; | ||
|
|
||
| constructor (propValueExample1: any, propValueExample2: any) { | ||
| this.propValueExample1 = propValueExample1; | ||
| this.propValueExample2 = propValueExample2; | ||
| } | ||
| } | ||
|
|
||
| const example = new Example(ValueExample1, ValueExample2); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не правильно. В поле надо устанавливать объект, т.е. проверять это надо было вот так: const example = new Example(); |
||
| const e1 = new ValueExample1("21", 21); | ||
| const e2 = new ValueExample2(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ES6", | ||
| "experimentalDecorators": true | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Это не паттерн декоратор.
https://refactoring.guru/ru/design-patterns/decorator/typescript/example