diff --git a/package-lock.json b/package-lock.json index c6a18e7..8e55e93 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,8 +1,28 @@ { "name": "ts-task-1", "version": "1.0.0", - "lockfileVersion": 1, + "lockfileVersion": 2, "requires": true, + "packages": { + "": { + "name": "ts-task-1", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "tsc": "^1.20150623.0" + }, + "devDependencies": {} + }, + "node_modules/tsc": { + "version": "1.20150623.0", + "resolved": "https://registry.npmjs.org/tsc/-/tsc-1.20150623.0.tgz", + "integrity": "sha1-Trw8d04WkUjLx2inNCUz8ILHpuU=", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + } + } + }, "dependencies": { "tsc": { "version": "1.20150623.0", diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 53a04d7..426f13d 100644 --- a/src/task_1/index.ts +++ b/src/task_1/index.ts @@ -8,6 +8,17 @@ * результат в виде строки на английском языке: * result of the addition operation ${a} + ${b} = ${рассчитанное значение} */ +function DecorateRu(target:any,method:string,descriptor:PropertyDescriptor) { + descriptor.value = function() { + return `результат сложения ${this.a} + ${this.b} = ${this.a + this.b}`; + } +} + +function DecorateEn(target:any,method:string,descriptor:PropertyDescriptor) { + descriptor.value = function() { + return `result of the addition operation ${this.a} + ${this.b} = ${this.a + this.b}`; + } +} class Calculator { protected a: number = 0; @@ -21,7 +32,8 @@ class Calculator { this.b = b; } } - + @DecorateRu + @DecorateEn public exec(): string { return (this.a + this.b).toString(); } diff --git a/src/task_2/index.ts b/src/task_2/index.ts index 06535aa..55d482e 100644 --- a/src/task_2/index.ts +++ b/src/task_2/index.ts @@ -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"; } -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 \ No newline at end of file diff --git a/src/task_3/index.ts b/src/task_3/index.ts index afa588c..26313b9 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -14,26 +14,56 @@ abstract class Control { 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 { + 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 { + private item: SelectItem; + public getValue(): SelectItem { + return this.item; + } + public setValue(val: SelectItem): void { + this.item = val; + } } class Container { public instance: Control; public type: string; + constructor(instance: Control, type: string) { + this.instance = instance; + this.type = type; + } } /**Фабрика которая отвечает за создание экземпляров контролов */ @@ -45,14 +75,25 @@ class FactoryControl { this._collection = []; } - public register(type: ?) { + public register(type: Function) { + const typeStr = type.name; + if (!this.existType(typeStr)) { + this._collection.push(new Container(type(), typeStr)) + } } - public getInstance(type: ?): ? { + public getInstance(type: Function): Control { + 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); -selectBoxInstance.setValue("sdfsdf") // компилятор TS не пропускает +//selectBoxInstance.setValue("sdfsdf") // компилятор TS не пропускает selectBoxInstance.setValue(new SelectItem()) // компилятор TS пропускает diff --git a/src/task_4/index.ts b/src/task_4/index.ts index dc0e179..0524de6 100644 --- a/src/task_4/index.ts +++ b/src/task_4/index.ts @@ -6,6 +6,6 @@ function getProperty(obj: T, key: K): T[K] { return obj[key]; } -const x = undefined; +const x = { "year": 2016 }; -console.log(getProperty(x, "m")); \ No newline at end of file +console.log(getProperty(x, "year")); \ No newline at end of file diff --git a/src/task_5/index.ts b/src/task_5/index.ts index d9fea4b..7c90b3e 100644 --- a/src/task_5/index.ts +++ b/src/task_5/index.ts @@ -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) { + return function validate(target: Object, propertyName: string){ + let descriptor: PropertyDescriptor = { + get() { + return type; + }, + set(newType) { + if (newType === type) { + 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, { + 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; + } \ No newline at end of file