-
Notifications
You must be signed in to change notification settings - Fork 11
Комаров Ярослав #7
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,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 = ""; | ||
| } | ||
|
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 |
|---|---|---|
|
|
@@ -10,30 +10,65 @@ | |
|
|
||
| /**Базовый класс для контролов */ | ||
| abstract class Control<T> { | ||
| public name: string = ""; | ||
| public name: string; | ||
|
|
||
| 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.value; | ||
| } | ||
| public setValue(val: string): void { | ||
| if (val) { | ||
| this.value = 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> { | ||
| public getValue(): SelectItem { | ||
| return this.value; | ||
| } | ||
| public setValue(val: SelectItem): void { | ||
| if (val) { | ||
| this.value = val; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class Container { | ||
| public instance: Control<any>; | ||
| public type: string; | ||
|
|
||
| constructor(instance: Control<any>, type: string) { | ||
| this.instance = instance; | ||
| this.type = type; | ||
| } | ||
| } | ||
|
|
||
| /**Фабрика которая отвечает за создание экземпляров контролов */ | ||
|
|
@@ -45,13 +80,24 @@ class FactoryControl { | |
| this._collection = []; | ||
| } | ||
|
|
||
| public register<?>(type: ?) { | ||
| public register<T extends Control<any>>(type: new () => T) { | ||
| const typeStr = type.name; | ||
| if (!this.existType(typeStr)) { | ||
| this._collection.push(new Container(new type(), typeStr)) | ||
| } | ||
| } | ||
|
|
||
| public getInstance<?>(type: ?): ? { | ||
| public getInstance<T>(type: new () => Control<T>): Control<T> { | ||
| 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; | ||
| } | ||
| } | ||
|
|
@@ -62,4 +108,4 @@ factory.register(SelectBox); | |
| const selectBoxInstance = factory.getInstance(SelectBox); | ||
|
|
||
| selectBoxInstance.setValue("sdfsdf") // компилятор TS не пропускает | ||
| selectBoxInstance.setValue(new SelectItem()) // компилятор TS пропускает | ||
| selectBoxInstance.setValue(new SelectItem()) // компилятор TS пропускает | ||
|
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. не верная реализация. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] { | |
| return obj[key]; | ||
| } | ||
|
|
||
| const x = undefined; | ||
| const x = { | ||
| m: 12, | ||
| } | ||
|
|
||
|
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")); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,29 +6,40 @@ | |
| * 2) Проверять у передаваемого объекта наличие заполненного поля. | ||
| * Если поле не заполнено, то генерируется эксепшен. | ||
| */ | ||
| function validate<T>(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; | ||
| } | ||
| } | ||
|
|
||
| class ValueExample1 { | ||
| public value: string; | ||
| public id: number; | ||
| public constructor(value?: string, id?: number) { | ||
| 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) { | ||
| this.undefinedProp = undefinedProp; | ||
| this.booleanProp = booleanProp; | ||
| } | ||
| public booleanProp?: boolean; | ||
| } | ||
|
|
||
| class Example { | ||
| class Example1 { | ||
| @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. Работает не верно let example = new Example1(); попробуй проверить
Author
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. Оооооочень долго не мог понять, почему у меня проходит без проблем тест, который Вы предложили опробовать. Оказывается я определил конструкторы для ValueExample1 и 2, с присвоением значений по умолчанию и поля всегда были проинициализированны. Крч, не туда воевал |
||
|
|
||
| let ex1 = new Example1(); | ||
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.
3 бала, верная реализация