Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion src/task_1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
* result of the addition operation ${a} + ${b} = ${рассчитанное значение}
*/

class Calculator {
interface ICalculator {
exec(): string;
}

class Calculator implements ICalculator{
protected a: number = 0;
protected b: number = 0;

Expand All @@ -26,3 +30,42 @@ class Calculator {
return (this.a + this.b).toString();
}
}

class BaseDecorator implements ICalculator
{
protected calculator: ICalculator
protected a: number = 0;
protected b: number = 0;

constructor(calc: ICalculator) {
this.calculator = calc;
}

public exec(): string
{
return this.calculator.exec();
}
}

class DecorateEn extends BaseDecorator
{
public exec(): string
{
return `Result of the addition operation ${super.exec()}`;
}
}

class DecorateRu extends BaseDecorator
{
public exec(): string
{
return `Результат сложения ${super.exec()}`;
}
}

//Test region

let q = new Calculator(15, 78);
let ruCalc = new DecorateRu(q);
let enCalc = new DecorateEn(q);
console.log(`${q.exec()} \n ${enCalc.exec()} \n ${ruCalc.exec()}`)
Copy link

@m-abrosimov m-abrosimov Apr 18, 2021

Choose a reason for hiding this comment

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

засчитано 1 бал

22 changes: 21 additions & 1 deletion src/task_2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,31 @@
* Когда присваивается корректный e-mail в консоль выводится сообщение email valid.
* Когда присваивается некорректный e-mail возбуждается ошибка.
*/
function validateBox(target: Object, propertyKey: string | symbol): any {
const regExp = (email: string) =>{
return /[a-zA-Z0-9]+@[a-zA-Z0-9]+.[a-zA-Z0-9]+/.test(email)
}
let email: string;
let descriptor: PropertyDescriptor = {
set(newEmail: string) {
debugger
if (!regExp(newEmail)) {
console.log("Invalid email")
throw 'Invalid email.'
}
console.log('email valid');
email = newEmail;
}
}

return descriptor;
}

class Example {
@validateBox
public email: string = "";
}

let exampleInstance = new Example();
exampleInstance.email = "fkkldfjg"; // генерирует эксепшен
//exampleInstance.email = "fkkldfjg"; // генерирует эксепшен
exampleInstance.email = "misha@mail.ru"; // выводит в консоль e-mail valid
31 changes: 28 additions & 3 deletions src/task_3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ abstract class Control<T> {
}
/**Класс описывает TextBox контрол */
class TextBox extends Control<string> {
getValue(): string {
return "";
}

setValue(val: string): void {
}
}
/**value контрола selectBox */
class SelectItem {
Expand All @@ -29,11 +35,23 @@ class SelectItem {

/**Класс описывает SelectBox контрол */
class SelectBox extends Control<SelectItem> {
getValue(): SelectItem {
return undefined;
}

setValue(val: SelectItem): void {
}
}

class Container {
public instance: Control<any>;
public type: string;


constructor(instance: Control<any>, type: string) {
this.instance = instance;
this.type = type;
}
}

/**Фабрика которая отвечает за создание экземпляров контролов */
Expand All @@ -45,10 +63,17 @@ class FactoryControl {
this._collection = [];
}

public register<?>(type: ?) {
public register<T extends Control<any>>(type: new () => T) {
const instanceType = (<Function>type).name;
if(this.existType(instanceType)) {
return;
}
this._collection.push(new Container(new type(), instanceType));
}

Choose a reason for hiding this comment

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

тут нет регистрации


public getInstance<?>(type: ?): ? {
public getInstance<T>(type: new () => Control<T>): Control<T> {
const instanceType = type.toString()
return this._collection.find(control => control.type === instanceType).instance;
}

private existType(type: string) {
Expand All @@ -61,5 +86,5 @@ factory.register(SelectBox);

const selectBoxInstance = factory.getInstance(SelectBox);

selectBoxInstance.setValue("sdfsdf") // компилятор TS не пропускает
//selectBoxInstance.setValue("sdfsdf") // компилятор TS не пропускает
selectBoxInstance.setValue(new SelectItem()) // компилятор TS пропускает
4 changes: 2 additions & 2 deletions src/task_4/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}

const x = undefined;
const x = {m:0};

console.log(getProperty(x, "m"));
console.log(getProperty(x, "m"));
33 changes: 32 additions & 1 deletion src/task_5/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,31 @@
* Если поле не заполнено, то генерируется эксепшен.
*/

function validate<T, P extends keyof T>(type: new () => T, prop: P) {
return (target: Object, propertyKey: string | symbol) : any => {
let property: T;
let descriptor: PropertyDescriptor = {
get() {
return property;
},
set(value: T) {
if(!(value instanceof type)){
throw new Error(`Input value has another type: ${type.name}`)
}
if (!(prop in value)) {

Choose a reason for hiding this comment

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

Нет проверки на соответствие типа.

throw new Error(`${prop} not exist in value`)
}
if(!value[prop])
throw new Error("Value must be defined")
console.log("VALID")
property = value;
}
}

return descriptor;
}
}

class ValueExample1 {
public value: string;
public id: number;
Expand All @@ -25,10 +50,16 @@ class ValueExample2 {
}
}

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

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

let quarta = new ExampleT();
let valueTest1 = new ValueExample1("Sharpest", 32);
let valueTest2 = new ValueExample2()
quarta.propValueExample1 = valueTest1;
quarta.propValueExample2 = valueTest2;