@@ -4,19 +4,19 @@ import { CSFDColorRating } from '../dto/global';
4
4
import { Colors } from '../dto/user-ratings' ;
5
5
import { addProtocol , parseColor , parseIdFromUrl } from './global.helper' ;
6
6
7
- const getCreatorColorRating = ( el : HTMLElement ) : CSFDColorRating => {
8
- return parseColor ( el ?. classNames . split ( ' ' ) . pop ( ) as Colors ) ;
7
+ const getCreatorColorRating = ( el : HTMLElement | null ) : CSFDColorRating => {
8
+ const classes : string [ ] = el ?. classNames . split ( ' ' ) ?? [ ] ;
9
+ const last = classes [ classes . length - 1 ] as Colors | undefined ;
10
+ return parseColor ( last ) ;
9
11
} ;
10
12
11
- export const getCreatorId = ( url : string ) : number | null => {
12
- if ( url ) {
13
- return parseIdFromUrl ( url ) ;
14
- }
15
- return null ;
16
- }
13
+ export const getCreatorId = ( url : string | null | undefined ) : number | null => {
14
+ return url ? parseIdFromUrl ( url ) : null ;
15
+ } ;
17
16
18
- export const getCreatorName = ( el : HTMLElement | null ) : string => {
19
- return el . querySelector ( 'h1' ) . innerText . trim ( ) ;
17
+ export const getCreatorName = ( el : HTMLElement | null ) : string | null => {
18
+ const h1 = el ?. querySelector ( 'h1' ) ;
19
+ return h1 ?. innerText ?. trim ( ) ?? null ;
20
20
} ;
21
21
22
22
export const getCreatorBirthdayInfo = (
@@ -42,8 +42,10 @@ export const getCreatorBirthdayInfo = (
42
42
return { birthday, age, birthPlace } ;
43
43
} ;
44
44
45
- export const getCreatorBio = ( el : HTMLElement | null ) : string => {
46
- return el . querySelector ( '.article-content p' ) ?. text . trim ( ) . split ( '\n' ) [ 0 ] . trim ( ) || null ;
45
+ export const getCreatorBio = ( el : HTMLElement | null ) : string | null => {
46
+ const p = el ?. querySelector ( '.article-content p' ) ;
47
+ const first = p ?. text ?. trim ( ) . split ( '\n' ) [ 0 ] ?. trim ( ) ;
48
+ return first || null ;
47
49
} ;
48
50
49
51
export const getCreatorPhoto = ( el : HTMLElement | null ) : string | null => {
@@ -63,32 +65,27 @@ const parseBirthPlace = (text: string): string =>
63
65
64
66
65
67
export const getCreatorFilms = ( el : HTMLElement | null ) : CSFDCreatorScreening [ ] => {
66
- const filmNodes = el . querySelectorAll ( '.box' ) [ 0 ] ?. querySelectorAll ( 'table tr' ) ;
67
- let yearCache : number ;
68
+ const filmNodes = el ? .querySelectorAll ( '.box' ) ?. [ 0 ] ?. querySelectorAll ( 'table tr' ) ?? [ ] ;
69
+ let yearCache : number | null = null ;
68
70
const films = filmNodes . map ( ( filmNode ) => {
69
- const id = getCreatorId ( filmNode . querySelector ( 'td.name .film-title-name' ) ?. attributes . href ) ;
70
- const title = filmNode . querySelector ( '.name' ) ?. text . trim ( ) ;
71
- const year = + filmNode . querySelector ( '.year' ) ?. text . trim ( ) ;
71
+ const id = getCreatorId ( filmNode . querySelector ( 'td.name .film-title-name' ) ?. attributes ?. href ) ;
72
+ const title = filmNode . querySelector ( '.name' ) ?. text ?. trim ( ) ;
73
+ const yearText = filmNode . querySelector ( '.year' ) ?. text ?. trim ( ) ;
74
+ const year = yearText ? + yearText : null ;
72
75
const colorRating = getCreatorColorRating ( filmNode . querySelector ( '.name .icon' ) ) ;
73
76
74
77
// Cache year from previous film because there is a gap between movies with same year
75
- if ( year ) {
78
+ if ( typeof year === 'number' && ! isNaN ( year ) ) {
76
79
yearCache = + year ;
77
80
}
78
81
79
- if ( id && title ) {
80
- return {
81
- id,
82
- title,
83
- year : year || yearCache ,
84
- colorRating
85
- } ;
82
+ const finalYear = year ?? yearCache ;
83
+ if ( id != null && title && finalYear != null ) {
84
+ return { id, title, year : finalYear , colorRating } ;
86
85
}
87
- return { } ;
86
+ return null ;
88
87
} ) ;
89
88
// Remove empty objects
90
- const filmsUnique = films . filter (
91
- ( value ) => Object . keys ( value ) . length !== 0
92
- ) as CSFDCreatorScreening [ ] ;
89
+ const filmsUnique = films . filter ( Boolean ) as CSFDCreatorScreening [ ] ;
93
90
return filmsUnique ;
94
91
} ;
0 commit comments