diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 53a04d7..369a8c5 100644 --- a/src/task_1/index.ts +++ b/src/task_1/index.ts @@ -8,6 +8,23 @@ * результат в виде строки на английском языке: * result of the addition operation ${a} + ${b} = ${рассчитанное значение} */ +function DecorateRu(target: Object, method: string, descriptor: PropertyDescriptor){ + let original = descriptor.value; + descriptor.value = function(...args: any){ + let result = original.apply(this, args); + console.log(`результат сложения ${args[0]} + ${args[1]} = ${result}`) + return descriptor; + } +} + +function DecorateEn(target: Object, method: string, descriptor: PropertyDescriptor){ + let original = descriptor.value; + descriptor.value = function(...args: any){ + let result = original.apply(this, args); + console.log(`result of the addition operation ${args[0]} + ${args[1]} = ${result}`) + return descriptor; + } +} class Calculator { protected a: number = 0; @@ -21,8 +38,9 @@ class Calculator { this.b = b; } } - + @DecorateEn + @DecorateRu public exec(): string { return (this.a + this.b).toString(); } -} +} \ No newline at end of file diff --git a/src/task_2/index.ts b/src/task_2/index.ts index 06535aa..532e110 100644 --- a/src/task_2/index.ts +++ b/src/task_2/index.ts @@ -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 \ No newline at end of file diff --git a/src/task_4/index.ts b/src/task_4/index.ts index dc0e179..451dac5 100644 --- a/src/task_4/index.ts +++ b/src/task_4/index.ts @@ -6,6 +6,11 @@ function getProperty(obj: T, key: K): T[K] { return obj[key]; } -const x = undefined; - +class C { + public m: string; + constructor(){ + this.m = "hello"; + } +} +let x = new C(); console.log(getProperty(x, "m")); \ No newline at end of file diff --git a/src/task_5/index.ts b/src/task_5/index.ts index d9fea4b..723eff3 100644 --- a/src/task_5/index.ts +++ b/src/task_5/index.ts @@ -6,10 +6,24 @@ * 2) Проверять у передаваемого объекта наличие заполненного поля. * Если поле не заполнено, то генерируется эксепшен. */ +function validate(type: T, prop: string){ + return function(target: Object, propertyKey: string | symbol) : any{ + let descriptor: PropertyDescriptor = { + set: function(val: any){ + if (typeof val === typeof type) + this.value = val; + else + throw new Error(); + if (!(prop in val)) + throw new Error(); + } + } + } +} 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;