Skip to content

Commit 5884992

Browse files
committed
refactor(user-ratings): specific method names
1 parent e7a7ba2 commit 5884992

File tree

3 files changed

+52
-52
lines changed

3 files changed

+52
-52
lines changed

src/helpers/user-ratings.helper.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { CSFDColorRating, CSFDFilmTypes, CSFDStars } from '../interfaces/global'
33
import { Colors } from '../interfaces/user-ratings.interface';
44
import { parseIdFromUrl } from './global.helper';
55

6-
export const getId = (el: HTMLElement): number => {
6+
export const getUserRatingId = (el: HTMLElement): number => {
77
const url = el.querySelector('td.name .film-title-name').attributes.href;
88
return parseIdFromUrl(url);
99
};
@@ -15,35 +15,35 @@ export const getUserRating = (el: HTMLElement): CSFDStars => {
1515
return rating as CSFDStars;
1616
};
1717

18-
export const getType = (el: HTMLElement): CSFDFilmTypes => {
18+
export const getUserRatingType = (el: HTMLElement): CSFDFilmTypes => {
1919
const typeText = el.querySelectorAll('td.name .film-title-info .info');
2020

2121
return (typeText.length > 1 ? typeText[1].text.slice(1, -1) : 'film') as CSFDFilmTypes;
2222
};
2323

24-
export const getTitle = (el: HTMLElement): string => {
24+
export const getUserRatingTitle = (el: HTMLElement): string => {
2525
return el.querySelector('td.name .film-title-name').text;
2626
};
2727

28-
export const getYear = (el: HTMLElement): number => {
28+
export const getUserRatingYear = (el: HTMLElement): number => {
2929
return +el.querySelectorAll('td.name .film-title-info .info')[0]?.text.slice(1, -1) || null;
3030
};
3131

32-
export const getColorRating = (el: HTMLElement): CSFDColorRating => {
32+
export const getUserRatingColorRating = (el: HTMLElement): CSFDColorRating => {
3333
const color = parseColor(el.querySelector('td.name .icon').classNames.split(' ').pop() as Colors);
3434
return color;
3535
};
3636

37-
export const getDate = (el: HTMLElement): string => {
37+
export const getUserRatingDate = (el: HTMLElement): string => {
3838
return el.querySelector('td.date-only').text.trim();
3939
};
4040

41-
export const getUrl = (el: HTMLElement): string => {
41+
export const getUserRatingUrl = (el: HTMLElement): string => {
4242
const url = el.querySelector('td.name .film-title-name').attributes.href;
4343
return `https://www.csfd.cz${url}`;
4444
};
4545

46-
export const parseColor = (quality: Colors): CSFDColorRating => {
46+
const parseColor = (quality: Colors): CSFDColorRating => {
4747
switch (quality) {
4848
case 'lightgrey':
4949
return 'unknown';

src/services/user-ratings.service.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { HTMLElement, parse } from 'node-html-parser';
22
import { fetchPage } from '../fetchers';
33
import {
4-
getColorRating,
5-
getDate,
6-
getId,
7-
getTitle,
8-
getType,
9-
getUrl,
104
getUserRating,
11-
getYear,
5+
getUserRatingColorRating,
6+
getUserRatingDate,
7+
getUserRatingId,
8+
getUserRatingTitle,
9+
getUserRatingType,
10+
getUserRatingUrl,
11+
getUserRatingYear,
1212
sleep
1313
} from '../helpers/user-ratings.helper';
1414
import { CSFDColorRating, CSFDStars } from '../interfaces/global';
@@ -71,7 +71,7 @@ export class UserRatingsScraper {
7171
}
7272

7373
for (const el of movies) {
74-
const type = getType(el);
74+
const type = getUserRatingType(el);
7575

7676
// Filtering includesOnly
7777
if (config?.includesOnly?.length) {
@@ -93,13 +93,13 @@ export class UserRatingsScraper {
9393

9494
private buildUserRatings(el: HTMLElement) {
9595
this.films.push({
96-
id: getId(el),
97-
title: getTitle(el),
98-
year: getYear(el),
99-
type: getType(el),
100-
url: getUrl(el),
101-
colorRating: getColorRating(el) as CSFDColorRating,
102-
userDate: getDate(el),
96+
id: getUserRatingId(el),
97+
title: getUserRatingTitle(el),
98+
year: getUserRatingYear(el),
99+
type: getUserRatingType(el),
100+
url: getUserRatingUrl(el),
101+
colorRating: getUserRatingColorRating(el) as CSFDColorRating,
102+
userDate: getUserRatingDate(el),
103103
userRating: getUserRating(el) as CSFDStars
104104
});
105105
}

tests/user-ratings.test.ts

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { HTMLElement, parse } from 'node-html-parser';
22
import { describe, expect, test } from 'vitest';
33
import {
4-
getColorRating,
5-
getDate,
6-
getId,
7-
getTitle,
8-
getType,
9-
getUrl,
104
getUserRating,
11-
getYear
5+
getUserRatingColorRating,
6+
getUserRatingDate,
7+
getUserRatingId,
8+
getUserRatingTitle,
9+
getUserRatingType,
10+
getUserRatingUrl,
11+
getUserRatingYear
1212
} from '../src/helpers/user-ratings.helper';
1313
import { CSFDColorRating, CSFDFilmTypes, CSFDStars } from '../src/interfaces/global';
1414
import { userRatingsMock } from './mocks/userRatings.html';
@@ -34,109 +34,109 @@ describe('Get Ratings', () => {
3434

3535
describe('Get ID', () => {
3636
test('First ID', () => {
37-
const movie = getId(movies[0]);
37+
const movie = getUserRatingId(movies[0]);
3838
expect(movie).toEqual<number>(1254361);
3939
});
4040
test('Last ID', () => {
41-
const movie = getId(movies[movies.length - 1]);
41+
const movie = getUserRatingId(movies[movies.length - 1]);
4242
expect(movie).toEqual<number>(1169425);
4343
});
4444
});
4545

4646
describe('Get type', () => {
4747
test('Film', () => {
48-
const movie = getType(movies[0]);
48+
const movie = getUserRatingType(movies[0]);
4949
expect(movie).toEqual<CSFDFilmTypes>('film');
5050
});
5151
test('TV series', () => {
52-
const movie = getType(movies[23]);
52+
const movie = getUserRatingType(movies[23]);
5353
expect(movie).toEqual<CSFDFilmTypes>('seriál');
5454
});
5555
test('Episode', () => {
56-
const movie = getType(movies[4]);
56+
const movie = getUserRatingType(movies[4]);
5757
expect(movie).toEqual<CSFDFilmTypes>('epizoda');
5858
});
5959
// test('TV film', () => {
60-
// const movie = getType(movies[18]);
60+
// const movie = getUserRatingType(movies[18]);
6161
// expect(movie).toEqual<CSFDFilmTypes>('TV film');
6262
// });
6363
// test('Pořad', () => {
64-
// const movie = getType(movies[6]);
64+
// const movie = getUserRatingType(movies[6]);
6565
// expect(movie).toEqual<CSFDFilmTypes>('pořad');
6666
// });
6767
test('Amateur film', () => {
68-
const movie = getType(movies[31]);
68+
const movie = getUserRatingType(movies[31]);
6969
expect(movie).toEqual<CSFDFilmTypes>('amatérský film');
7070
});
7171
test('Season', () => {
72-
const movie = getType(movies[11]);
72+
const movie = getUserRatingType(movies[11]);
7373
expect(movie).toEqual<CSFDFilmTypes>('série');
7474
});
7575
});
7676

7777
describe('Get title', () => {
7878
test('First title', () => {
79-
const movie = getTitle(movies[0]);
79+
const movie = getUserRatingTitle(movies[0]);
8080
expect(movie).toEqual<string>('Stutz');
8181
});
8282
test('Last title', () => {
83-
const movie = getTitle(movies[movies.length - 1]);
83+
const movie = getUserRatingTitle(movies[movies.length - 1]);
8484
expect(movie).toEqual<string>('Kouření způsobuje kašel');
8585
});
8686
});
8787

8888
describe('Get year', () => {
8989
test('First year', () => {
90-
const movie = getYear(movies[0]);
90+
const movie = getUserRatingYear(movies[0]);
9191
expect(movie).toEqual<number>(2022);
9292
});
9393
test('Some year', () => {
94-
const movie = getYear(movies[7]);
94+
const movie = getUserRatingYear(movies[7]);
9595
expect(movie).toEqual<number>(2016);
9696
});
9797
test('Almost last year', () => {
98-
const movie = getYear(movies[movies.length - 7]);
98+
const movie = getUserRatingYear(movies[movies.length - 7]);
9999
expect(movie).toEqual<number>(2000);
100100
});
101101
});
102102

103103
describe('Get color rating', () => {
104104
// test('Black', () => {
105-
// const movie = getColorRating(movies[7]);
105+
// const movie = getUserRatingColorRating(movies[7]);
106106
// expect(movie).toEqual<CSFDColorRating>('bad');
107107
// });
108108
// test('Gray', () => {
109-
// const movie = getColorRating(movies[29]);
109+
// const movie = getUserRatingColorRating(movies[29]);
110110
// expect(movie).toEqual<CSFDColorRating>('unknown');
111111
// });
112112
test('Blue', () => {
113-
const movie = getColorRating(movies[3]);
113+
const movie = getUserRatingColorRating(movies[3]);
114114
expect(movie).toEqual<CSFDColorRating>('average');
115115
});
116116
test('Red', () => {
117-
const movie = getColorRating(movies[1]);
117+
const movie = getUserRatingColorRating(movies[1]);
118118
expect(movie).toEqual<CSFDColorRating>('good');
119119
});
120120
});
121121

122122
describe('Get date', () => {
123123
test('First date', () => {
124-
const movie = getDate(movies[0]);
124+
const movie = getUserRatingDate(movies[0]);
125125
expect(movie).toEqual<string>('16.12.2022');
126126
});
127127
test('Last date', () => {
128-
const movie = getDate(movies[movies.length - 1]);
128+
const movie = getUserRatingDate(movies[movies.length - 1]);
129129
expect(movie).toEqual<string>('05.07.2022');
130130
});
131131
});
132132

133133
describe('Get Url', () => {
134134
test('First url', () => {
135-
const movie = getUrl(movies[0]);
135+
const movie = getUserRatingUrl(movies[0]);
136136
expect(movie).toEqual<string>('https://www.csfd.cz/film/1254361-stutz/');
137137
});
138138
test('Last url', () => {
139-
const movie = getUrl(movies[movies.length - 1]);
139+
const movie = getUserRatingUrl(movies[movies.length - 1]);
140140
expect(movie).toEqual<string>('https://www.csfd.cz/film/1169425-koureni-zpusobuje-kasel/');
141141
});
142142
});

0 commit comments

Comments
 (0)