From 189775c96eada4955a908bc4a3e2a2addc91b332 Mon Sep 17 00:00:00 2001 From: Mykhailo Hutsalenko Date: Tue, 18 Aug 2020 00:57:27 +0300 Subject: [PATCH] Mykhailo Hutsalenko Homework #1 --- src/mukhailo.hutsalenko/IClassName.ts | 3 + src/mukhailo.hutsalenko/ISalary.ts | 3 + src/mukhailo.hutsalenko/homework.spec.ts | 78 ++++++++++++++++++++++++ src/mukhailo.hutsalenko/homework.ts | 55 +++++++++++++++++ 4 files changed, 139 insertions(+) create mode 100644 src/mukhailo.hutsalenko/IClassName.ts create mode 100644 src/mukhailo.hutsalenko/ISalary.ts create mode 100644 src/mukhailo.hutsalenko/homework.spec.ts create mode 100644 src/mukhailo.hutsalenko/homework.ts diff --git a/src/mukhailo.hutsalenko/IClassName.ts b/src/mukhailo.hutsalenko/IClassName.ts new file mode 100644 index 0000000..f13b200 --- /dev/null +++ b/src/mukhailo.hutsalenko/IClassName.ts @@ -0,0 +1,3 @@ +export interface IClassName { + className: string +} \ No newline at end of file diff --git a/src/mukhailo.hutsalenko/ISalary.ts b/src/mukhailo.hutsalenko/ISalary.ts new file mode 100644 index 0000000..3e7d2a9 --- /dev/null +++ b/src/mukhailo.hutsalenko/ISalary.ts @@ -0,0 +1,3 @@ +export interface ISalary { + [key: string]: number; +} \ No newline at end of file diff --git a/src/mukhailo.hutsalenko/homework.spec.ts b/src/mukhailo.hutsalenko/homework.spec.ts new file mode 100644 index 0000000..96e1fad --- /dev/null +++ b/src/mukhailo.hutsalenko/homework.spec.ts @@ -0,0 +1,78 @@ +import { calculateSalaries, getToptPaidEmployee, multiplyNumeric, countBySequence, addClass } from './homework'; +import { ISalary } from './ISalary'; + +describe('Test calculateSalaries method', () => { + const salaries: ISalary = { + 'Вася': 100, + 'Петя': 300, + 'Даша': 250 + }; + + test('calculate slaries of an object with data', () => { + expect(calculateSalaries(salaries)).toBe(650); + }); + + test('calculate slaries of an empty object', () => { + expect(calculateSalaries({})).toBe(0); + }); +}); + +describe('Test getToptPaidEmployee method', () => { + const salaries: ISalary = { + 'Вася': 100, + 'Петя': 300, + 'Даша': 250 + }; + + test('get top paid employee from of an object with data', () => { + expect(getToptPaidEmployee(salaries)).toBe('Петя'); + }); + + test('get top paid employee from of an empty object', () => { + expect(getToptPaidEmployee({})).toBe('нет сотрудников'); + }); +}); + +describe('Test multiplyNumeric method', () => { + const menu = { + width: 200, + height: 300, + title: "My menu" + }; + + test('multiply all numeric properties of an object', () => { + multiplyNumeric(menu); + expect(menu.width).toBe(400); + expect(menu.height).toBe(600); + expect(menu.title).toBe('My menu'); + }); +}); + +describe('Test countBySequence method', () => { + const sequence1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + const sequense2 = [2, 4, 6, 8, 10]; + + test('count by for sequence1', () => { + expect(countBySequence(1, 10)).toEqual(sequence1); + }); + + test('count by for sequence2', () => { + expect(countBySequence(2, 5)).toEqual(sequense2); + }); +}); + +describe('Test addClass method', () => { + test('add several class names', () => { + var obj = { + className: 'open menu' + } + + var expectedClassName: string = 'open menu new me'; + + addClass(obj, 'new'); // obj.className='open menu new' + addClass(obj, 'open'); // без изменений (класс уже существует) + addClass(obj, 'me'); // obj.className='open menu new me' + + expect(obj.className).toEqual(expectedClassName); + }); +}); \ No newline at end of file diff --git a/src/mukhailo.hutsalenko/homework.ts b/src/mukhailo.hutsalenko/homework.ts new file mode 100644 index 0000000..5729338 --- /dev/null +++ b/src/mukhailo.hutsalenko/homework.ts @@ -0,0 +1,55 @@ +import { ISalary } from "./ISalary"; +import { IClassName } from "./IClassName"; + +export function calculateSalaries(salaries: ISalary): number { + let totalSalary: number = 0; + Object.values(salaries).forEach(key => totalSalary += key); + return totalSalary; +} + +export function getToptPaidEmployee(salaries: ISalary): string { + let topPaidEmployee = Object.keys(salaries).reduce((function(prevEmp, nextEmp){ + let prevEmpSalary = salaries[prevEmp]; + let nextEmpSalary = salaries[nextEmp]; + + return prevEmpSalary > nextEmpSalary ? prevEmp : nextEmp + }), 'нет сотрудников'); + + return topPaidEmployee; +} + +export function multiplyNumeric(object : any) { + for (const property in object) { + if (isNumeric(object[property])) { + object[property] *= 2; + } + } +} + +function isNumeric(n: any) { + return !isNaN(parseFloat(n)) && isFinite(n) +} + +export function countBySequence(initialNumber: number, length: number) { + let array: number[] = []; + for(let counter = 1; counter < length + 1; counter++) { + array.push(counter * initialNumber); + } + return array; +} + +export function addClass(obj: IClassName, cls: string) { + if( obj.className.split(' ').indexOf(cls) < 0) { + obj.className += ' ' + cls.trim(); + } +} + +var obj = { + className: 'open menu' +} + +addClass(obj, 'new'); // obj.className='open menu new' +addClass(obj, 'open'); // без изменений (класс уже существует) +addClass(obj, 'me'); // obj.className='open menu new me' + console.log(obj.className); +