-
Notifications
You must be signed in to change notification settings - Fork 11
Лебедкин Константин #13
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,11 +4,28 @@ | |
| * Когда присваивается корректный e-mail в консоль выводится сообщение email valid. | ||
| * Когда присваивается некорректный e-mail возбуждается ошибка. | ||
| */ | ||
| function confirmMail(target: Object, propertyKey: string | symbol): any { | ||
| let email = "" | ||
| let descriptor: PropertyDescriptor = { | ||
| get() { | ||
| return email | ||
| }, | ||
| set(newEmail: string) { | ||
| if (newEmail !== "" && !/[a-zA-Z0-9]+@[a-zA-Z0-9]+.[a-zA-Z0-9]+/.test(newEmail)) { | ||
| throw 'Invalid email.' | ||
| } | ||
| console.log('email valid'); | ||
| email = newEmail; | ||
| } | ||
| } | ||
| return descriptor; | ||
| } | ||
|
|
||
| class Example { | ||
| public email: string = ""; | ||
| class Example1 { | ||
| @confirmMail | ||
| public email: string = "misha@mail.ru"; | ||
| } | ||
|
|
||
|
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 бала |
||
| 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,26 +14,56 @@ abstract class Control<T> { | |
|
|
||
| protected value: T; | ||
| /**взять значение из контрола */ | ||
| constructor(value: T, name: string) { | ||
| this.name = name; | ||
| this.value = value; | ||
| } | ||
|
|
||
| public abstract getValue(): T; | ||
| /**установить значение в контрол */ | ||
| public abstract setValue(val: T): void; | ||
| } | ||
| /**Класс описывает TextBox контрол */ | ||
| class TextBox extends Control<string> { | ||
| constructor(value: string = '', name: string) { | ||
| super(value, name); | ||
| } | ||
|
|
||
| public getValue(): string { | ||
| return this.name; | ||
| } | ||
| public setValue(val: string): void { | ||
| 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<SelectItem> { | ||
| private item: 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. зачем? у тебя ешь шаблонное поле value, которое выполняет туже самую функцию |
||
| public getValue(): SelectItem { | ||
| return this.item; | ||
| } | ||
| public setValue(val: SelectItem): void { | ||
| this.item = val; | ||
| } | ||
| } | ||
|
|
||
| class Container { | ||
| public instance: Control<any>; | ||
| public type: string; | ||
| constructor(instance: Control<any>, type: string) { | ||
| this.instance = instance; | ||
| this.type = type; | ||
| } | ||
| } | ||
|
|
||
| /**Фабрика которая отвечает за создание экземпляров контролов */ | ||
|
|
@@ -45,14 +75,25 @@ class FactoryControl { | |
| this._collection = []; | ||
| } | ||
|
|
||
| public register<?>(type: ?) { | ||
| public register(type: Function) { | ||
|
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. тут должны были использоваться generic-типы. |
||
| const typeStr = type.name; | ||
| if (!this.existType(typeStr)) { | ||
| this._collection.push(new Container(type(), typeStr)) | ||
| } | ||
| } | ||
|
|
||
| public getInstance<?>(type: ?): ? { | ||
| public getInstance(type: Function): Control<any> { | ||
| const typeStr = type.name; | ||
| const container = this._collection.find(item => item.type === typeStr); | ||
| if (container) { | ||
| return container.instance; | ||
| } | ||
| this.register(type); | ||
| return this._collection[this._collection.length - 1].instance; | ||
| } | ||
|
|
||
| private existType(type: string) { | ||
| return this._collection.filter(g => g.type === type).length > 0; | ||
| private existType(type: string): boolean { | ||
| return this._collection.filter(item => item.type === type).length > 0; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -61,5 +102,5 @@ factory.register(SelectBox); | |
|
|
||
| const selectBoxInstance = factory.getInstance(SelectBox); | ||
|
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. т.к. не используются generic-типы в этот метод можно передать любой тип. |
||
|
|
||
| selectBoxInstance.setValue("sdfsdf") // компилятор TS не пропускает | ||
| //selectBoxInstance.setValue("sdfsdf") // компилятор TS не пропускает | ||
| selectBoxInstance.setValue(new SelectItem()) // компилятор TS пропускает | ||
| 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 = { "year": 2016 }; | ||
|
|
||
|
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 бала |
||
| console.log(getProperty(x, "m")); | ||
| console.log(getProperty(x, "year")); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,28 +7,55 @@ | |
| * Если поле не заполнено, то генерируется эксепшен. | ||
| */ | ||
|
|
||
| class ValueExample1 { | ||
| public value: string; | ||
| public id: number; | ||
| public constructor(value?: string, id?: number) { | ||
| this.value = value; | ||
| this.id = id; | ||
| } | ||
| } | ||
|
|
||
| class ValueExample2 { | ||
| public undefinedProp: undefined; | ||
| public booleanProp: boolean; | ||
| public constructor(undefinedProp?: undefined, booleanProp?: boolean) { | ||
| this.undefinedProp = undefinedProp; | ||
| this.booleanProp = booleanProp; | ||
| } | ||
| } | ||
| function validate(type: object, propertyKey: 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. нет проверки на принадлежность поля к типу. приложил правильное решени |
||
| return function validate(target: Object, propertyName: string){ | ||
| let descriptor: PropertyDescriptor = { | ||
| get() { | ||
| return type; | ||
| }, | ||
| set(newType) { | ||
| if (newType === 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'); | ||
| type = newType; | ||
| } else { | ||
| throw 'exception'; | ||
| } | ||
| if (!propertyName[propertyKey]) { | ||
| throw 'field is empty'; | ||
| } | ||
| return type; | ||
| } | ||
| } | ||
|
|
||
| class Example { | ||
| @validate(ValueExample1, "id") | ||
| public propValueExample1: any; | ||
|
|
||
| @validate(ValueExample2, "booleanProp") | ||
| public propValueExample2: any; | ||
| } | ||
| 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; | ||
| public constructor(value?: string, id?: number) { | ||
| this.value = value; | ||
| this.id = id; | ||
| } | ||
| } | ||
|
|
||
| class ValueExample2 { | ||
| public undefinedProp: undefined; | ||
| public booleanProp: boolean; | ||
| public constructor(undefinedProp?: undefined, booleanProp?: boolean) { | ||
| this.undefinedProp = undefinedProp; | ||
| this.booleanProp = booleanProp; | ||
| } | ||
| } | ||
|
|
||
| class Example { | ||
| @validate(ValueExample1, "id") | ||
| public propValueExample1: any; | ||
|
|
||
| @validate(ValueExample2, "booleanProp") | ||
| public propValueExample2: any; | ||
| } | ||
|
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. function validate<T, P extends keyof T>(type: new () => T, prop: P): (target: Object, propertyKey: string | symbol) => any { } class ValueExample1 { class ValueExample2 { class Example { } let ex1 = new Example(); let objValExample = new ValueExample1(); ex1.propValueExample2 = false; |
||
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