-
Notifications
You must be signed in to change notification settings - Fork 0
Lesson14: Functional programming #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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> | ||
|
|
||
| </html> | ||
| 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({ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. а зачем фриз?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| }); | ||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. сложновато, не находишь?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. а если сделать это рестом?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 }; | ||
| }; | ||
| 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', | ||
| }); | ||
| }); |
| 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(/^/, '?'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. зачем replace если ты просто добавляешь символ?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, ''); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. тебе же только первый вопрос убрать нужно, в смысле если он на первом месте
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| }; | ||
| 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', | ||
| }); | ||
| }); |
| 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, | ||
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
давай в следующий раз без лишних изменений (тех, которые к домашке не относятся)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Принял, не повторится)