-
Notifications
You must be signed in to change notification settings - Fork 11
Сулейманов Эмиль #12
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?
Сулейманов Эмиль #12
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 |
|---|---|---|
|
|
@@ -4,11 +4,31 @@ | |
| * Когда присваивается корректный e-mail в консоль выводится сообщение email valid. | ||
| * Когда присваивается некорректный e-mail возбуждается ошибка. | ||
| */ | ||
| function emailCheck(target: Object, propertyKey: string): any { | ||
| let email = ""; | ||
| let descriptor: PropertyDescriptor = { | ||
| get: function(){ | ||
| return email; | ||
| }, | ||
| set: function(newEmail: string) { | ||
| let reg = /\w*@[a-z]+\.[a-z]{2,}/; | ||
| if (reg.test(newEmail)){ | ||
| email = newEmail; | ||
| console.log("email valid") | ||
| } | ||
| else{ | ||
| throw new Error(); | ||
| } | ||
| } | ||
| }; | ||
| return descriptor; | ||
| } | ||
|
|
||
| class Example { | ||
| @emailCheck | ||
| public email: string = ""; | ||
| } | ||
|
|
||
| let exampleInstance = new Example(); | ||
| 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 |
|---|---|---|
|
|
@@ -6,6 +6,11 @@ function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] { | |
| return obj[key]; | ||
| } | ||
|
|
||
| const x = undefined; | ||
|
|
||
| class C { | ||
| public m: string; | ||
| constructor(){ | ||
| this.m = "hello"; | ||
| } | ||
| } | ||
|
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 x = new C(); | ||
| console.log(getProperty(x, "m")); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,10 +6,24 @@ | |
| * 2) Проверять у передаваемого объекта наличие заполненного поля. | ||
| * Если поле не заполнено, то генерируется эксепшен. | ||
| */ | ||
| function validate<T>(type: T, prop: string){ | ||
| return function(target: Object, propertyKey: string | symbol) : any{ | ||
| let descriptor: PropertyDescriptor = { | ||
| set: function(val: any){ | ||
| if (typeof val === typeof 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. val = объект |
||
| this.value = val; | ||
| else | ||
| throw new Error(); | ||
|
|
||
| if (!(prop in val)) | ||
| throw new Error(); | ||
| } | ||
| } | ||
|
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 descriptor , иначе он не будет работать. Метод set не будет вызываться |
||
| } | ||
| } | ||
| class ValueExample1 { | ||
| public value: string; | ||
| public id: number; | ||
| public value: string | undefined; | ||
| public id: number | undefined; | ||
| public constructor(value?: string, id?: number) { | ||
| this.value = value; | ||
| this.id = id; | ||
|
|
@@ -18,14 +32,14 @@ class ValueExample1 { | |
|
|
||
| class ValueExample2 { | ||
| public undefinedProp: undefined; | ||
| public booleanProp: boolean; | ||
| public booleanProp: boolean | undefined; | ||
| public constructor(undefinedProp?: undefined, booleanProp?: boolean) { | ||
| this.undefinedProp = undefinedProp; | ||
| this.booleanProp = booleanProp; | ||
| } | ||
| } | ||
|
|
||
| class Example { | ||
| class Example1 { | ||
| @validate(ValueExample1, "id") | ||
| public propValueExample1: 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