Skip to content

Сулейманов Эмиль#12

Open
theSweater23 wants to merge 1 commit intoRTF-Angular-2021:masterfrom
theSweater23:suleymanov_emil
Open

Сулейманов Эмиль#12
theSweater23 wants to merge 1 commit intoRTF-Angular-2021:masterfrom
theSweater23:suleymanov_emil

Conversation

@theSweater23
Copy link

No description provided.

public exec(): string {
return (this.a + this.b).toString();
}
}

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

let exampleInstance = new Example();
exampleInstance.email = "fkkldfjg"; // генерирует эксепшен
exampleInstance.email = "misha@mail.ru"; // выводит в консоль e-mail valid
exampleInstance.email = "misha@mail.ru"; // выводит в консоль e-mail valid No newline at end of file

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 бала

constructor(){
this.m = "hello";
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 бала

if (!(prop in val))
throw new Error();
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нужно ещё сделать return descriptor , иначе он не будет работать. Метод set не будет вызываться

return function(target: Object, propertyKey: string | symbol) : any{
let descriptor: PropertyDescriptor = {
set: function(val: any){
if (typeof val === typeof type)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val = объект
type = функция
значит это выражение будет всегда false

class Example1 {
@validate(ValueExample1, "id")
public propValueExample1: any;

Choose a reason for hiding this comment

The 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 {
let temp: T;
return (target: Object, propertyKey: string | symbol): PropertyDescriptor => {
let descriptor: PropertyDescriptor = {
get: function () {
return temp;
},
set: function (val: T) {
if (!(val instanceof type)) {
throw new Error("Устанавливается значение которое не соотвтетсвует типу");
}
if (temp[prop]) {
throw new Error("У требуемого поля не верный примитивный тип");
}
console.log("value valid");
temp = val;
}
};

    return descriptor;
};

}

class ValueExample1 {
public value: string;
public id: number;
}

class ValueExample2 {
public prop3: undefined;
public prop2: boolean;
}

class Example {
@Validate(ValueExample1, "id1")
public propValueExample1: any;

@validate(ValueExample2, "prop3")
public propValueExample2: any;

}

let ex1 = new Example();

let objValExample = new ValueExample1();
objValExample.id = 1;
objValExample.value = "qwe";
ex1.propValueExample1 = objValExample;

ex1.propValueExample2 = false;

@m-abrosimov
Copy link

Итого 6 балов

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants