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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@types/enzyme": "^3.10.5",
"@types/enzyme-adapter-react-16": "^1.0.6",
"@types/jest": "^26.0.5",
"@types/ramda": "^0.27.17",
"@types/react": "^16.9.42",
"@types/react-dom": "^16.9.8",
"@types/react-redux": "^7.1.9",
Expand All @@ -86,6 +87,7 @@
"loki": "^0.24.0",
"postcss-loader": "^3.0.0",
"prettier": "^2.0.5",
"ramda": "^0.27.1",
"react-test-renderer": "^16.13.1",
"redux-mock-store": "^1.5.4",
"style-loader": "^1.2.1",
Expand Down
18 changes: 10 additions & 8 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>

<body>
<div id="app"></div>
</body>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Otus project Game of Life</title>
</head>

<body>
<div id="app"></div>
</body>

Comment on lines +4 to +13
Copy link

Choose a reason for hiding this comment

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

давай в следующий раз без лишних изменений (тех, которые к домашке не относятся)

Copy link
Owner Author

Choose a reason for hiding this comment

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

Принял, не повторится)

</html>
56 changes: 56 additions & 0 deletions src/lesson14/immutability.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
OriginalTeam,
ExpectedTeam,
originalTeamToExpectedTeam,
originalTeamToExpectedTeamObject,
originalArrayToExpectedArray,
} from './immutability';

// Задание 1
test('team to team', () => {
const originalTeam: OriginalTeam = Object.freeze({
Copy link

Choose a reason for hiding this comment

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

а зачем фриз?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Фриз было в самом репозитории домашки, я не менял ничего.

size: 15,
name: 'Tampa Bay Roosters',
league: 'Minor',
});

const expectedTeam: ExpectedTeam = {
name: 'New York Badgers',
league: 'Minor',
roster: 25,
};

expect(originalTeamToExpectedTeamObject(originalTeam)).toEqual(
expectedTeam,
);
});

// Задание 2
test('array to array', () => {
const originalArray = Object.freeze([1, 2, 3, 4]);

const expectedArray = ['two', 3, 4, 5];

expect(originalArrayToExpectedArray(originalArray)).toEqual(expectedArray);
});

// Задание 3
test('team to team deep', () => {
const originalTeam = Object.freeze({
name: 'Tampa Bay Roosters',
captain: {
name: 'Bryan Downey',
age: 27,
},
});

const expectedTeam = {
name: 'Tampa Bay Roosters',
captain: {
name: 'Bryan Downey',
age: 28,
},
};

expect(originalTeamToExpectedTeam(originalTeam)).toEqual(expectedTeam);
});
59 changes: 59 additions & 0 deletions src/lesson14/immutability.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Задание 1
export type OriginalTeam = {
size: number;
name: string;
league: string;
};

export type ExpectedTeam = {
name: string;
league: string;
roster: number;
};

export const originalTeamToExpectedTeamObject = (
originalTeam: OriginalTeam,
): ExpectedTeam => {
const withoutSize = Object.entries(originalTeam).filter(
(value) => value[0] !== 'size',
);
const newValueKeyOfName = withoutSize.map((value) => {
if (value[0] === 'name' && value[1] !== 'New York Badgers')
value[1] = 'New York Badgers';
return value;
});
const teamNew = Object.fromEntries(
(newValueKeyOfName as unknown) as string[],
);
Comment on lines +17 to +27
Copy link

Choose a reason for hiding this comment

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

сложновато, не находишь?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Согласен, надо пересмотреть будет


return { ...teamNew, roster: 25 };
};

// Задание 2
type SomeArray = Array<number | string>;

export const originalArrayToExpectedArray = (
originalArray: SomeArray,
): SomeArray => {
const cloneArrayWithoutFirstElement = originalArray.slice(2);
Copy link

Choose a reason for hiding this comment

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

а если сделать это рестом?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Так клонировать же надо без первого элемента, не пойму как можно сделать это рестом

return ['two', ...cloneArrayWithoutFirstElement, 5];
};

// Задание 3

export type Team = {
name: string;
captain: {
name: string;
age: number;
};
};

export const originalTeamToExpectedTeam = (originalTeam: Team): Team => {
const cloneOriginalTeam = Object.assign({}, originalTeam);
const newCaptain = {
name: 'Bryan Downey',
age: 28,
};
return { ...cloneOriginalTeam, captain: newCaptain };
};
36 changes: 36 additions & 0 deletions src/lesson14/pureFunctions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { getTopName, Team, QsObj, createQs, parseQs } from './pureFunctions';

test('getTopName', () => {
const teams: Team[] = [
{ name: 'Lions', score: 5 },
{ name: 'Tigers', score: 4 },
{ name: 'Bears', score: 6 },
{ name: 'Monkeys', score: 2 },
];

expect(getTopName(teams)).toBe('Bears');
});

test('createQs', () => {
const qsObj: QsObj = {
page: '2',
pageSize: '10',
total: '205',
somethingElse: 'value',
};

expect(createQs(qsObj)).toBe(
'?page=2&pageSize=10&total=205&somethingElse=value',
);
});

test('parseQs', () => {
const qs = '?page=2&pageSize=10&total=205&somethingElse=value';

expect(parseQs(qs)).toEqual({
page: '2',
pageSize: '10',
total: '205',
somethingElse: 'value',
});
});
31 changes: 31 additions & 0 deletions src/lesson14/pureFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Задание 1
export type Team = { name: string; score: number };

export const getTopName = (teams: Team[]): string => {
const copyTeams = teams.map((value) => Object.assign({}, value));
const sorted = copyTeams.sort((x, y) => (x.score > y.score ? 1 : -1));
return sorted[sorted.length - 1].name;
};

// Задание 2
export type QsObj = Record<string, string | number | boolean | object>;

export const createQs = (qsObj: QsObj): string => {
const objectToArray = Object.entries(qsObj);
const elementJoinEquals = objectToArray.map((val) => val.join('='));
const stringJoinAmp = elementJoinEquals.join('&');
const propsWithSymbol = stringJoinAmp.replace(/^/, '?');
Copy link

Choose a reason for hiding this comment

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

зачем replace если ты просто добавляешь символ?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Согласен, лишнего сделал

return propsWithSymbol;
};

// Задание 3

export const parseQs = (qs: string): QsObj => {
const propsWithoutSymbol = qs.replace(/\?/y, '');
Copy link

Choose a reason for hiding this comment

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

тебе же только первый вопрос убрать нужно, в смысле если он на первом месте

Copy link
Owner Author

Choose a reason for hiding this comment

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

Сделаю, спасибо.

const stringSplitAmp = propsWithoutSymbol.split('&');
const elementSplitEquals = stringSplitAmp.map((v) => v.split('='));
const desiredObject = Object.fromEntries(
(elementSplitEquals as unknown) as string[],
);
return desiredObject;
};
42 changes: 42 additions & 0 deletions src/lesson14/ramdaPureFunctions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
getTopName,
Team,
QsObj,
createQs,
parseQs,
} from './ramdaPureFunctions';

test('getTopName', () => {
const teams: Team[] = [
{ name: 'Lions', score: 5 },
{ name: 'Tigers', score: 4 },
{ name: 'Bears', score: 6 },
{ name: 'Monkeys', score: 2 },
];

expect(getTopName(teams)).toBe('Bears');
});

test('createQs', () => {
const qsObj: QsObj = {
page: '2',
pageSize: '10',
total: '205',
somethingElse: 'value',
};

expect(createQs(qsObj)).toBe(
'?page=2&pageSize=10&total=205&somethingElse=value',
);
});

test('parseQs', () => {
const qs = '?page=2&pageSize=10&total=205&somethingElse=value';

expect(parseQs(qs)).toEqual({
page: '2',
pageSize: '10',
total: '205',
somethingElse: 'value',
});
});
40 changes: 40 additions & 0 deletions src/lesson14/ramdaPureFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { compose, last, sortBy, prop, map, join, replace, split } from 'ramda';

// Задание 1
export type Team = { name: string; score: number };

const copyTeams = (teams: Team[]) =>
teams.map((value) => Object.assign({}, value));
const sortByObj = sortBy((t) => t.score);
const getName = prop<string, Team>('name');

export const getTopName = compose(getName, last, sortByObj, copyTeams);

// Задание 2
export type QsObj = Record<string, string | number | boolean | object>;
export type CreateQs = (x: QsObj) => string;

const objectToArray = (qsObj: QsObj): string[][] =>
Object.entries(qsObj as any);
const elementJoinEquals = map((val: string[]) => val.join('='));
const stringJoinAmp = join('&');
const propsWithSymbol = replace(/^/, '?');
export const createQs: CreateQs = compose(
propsWithSymbol,
stringJoinAmp,
elementJoinEquals,
objectToArray,
);

// Задание 3
const propsWithoutSymbol = replace(/\?/y, '');
const stringSplitAmp = split('&');
const elementSplitEquals = map((v: string) => v.split('='));
const desiredObject = (desiredObject: string[][]) =>
Object.fromEntries(desiredObject as any);
export const parseQs = compose(
desiredObject,
elementSplitEquals,
stringSplitAmp,
propsWithoutSymbol,
);