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
4 changes: 2 additions & 2 deletions src/helpers/creator.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { HTMLElement } from 'node-html-parser';
import { CSFDCreatorScreening } from '../dto/creator';
import { CSFDColorRating } from '../dto/global';
import { CSFDColors } from '../dto/user-ratings';
import { addProtocol, parseColor, parseDate, parseIdFromUrl } from './global.helper';
import { addProtocol, getFirstLine, parseColor, parseDate, parseIdFromUrl } from './global.helper';

const getCreatorColorRating = (el: HTMLElement | null): CSFDColorRating => {
const classes: string[] = el?.classNames.split(' ') ?? [];
Expand Down Expand Up @@ -42,7 +42,7 @@ export const getCreatorBirthdayInfo = (

export const getCreatorBio = (el: HTMLElement | null): string | null => {
const p = el?.querySelector('.article-content p');
const first = p?.text?.trim().split('\n')[0]?.trim();
const first = getFirstLine(p?.text?.trim())?.trim();
return first || null;
};

Expand Down
20 changes: 20 additions & 0 deletions src/helpers/global.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,23 @@ export const parseDate = (date: string): string | null => {

// Sleep in loop
export const sleep = (ms: number) => new Promise((res) => setTimeout(res, ms));

/**
* Performance optimization: Extract the last word from a string efficiently
* without intermediate array allocations (avoids .split(' ').pop()).
*/
export const getLastWord = (text: string | null | undefined, delimiter: string = ' '): string => {
if (!text) return '';
const lastIndex = text.lastIndexOf(delimiter);
return lastIndex !== -1 ? text.substring(lastIndex + delimiter.length) : text;
};

/**
* Performance optimization: Extract the first line from a string efficiently
* without intermediate array allocations (avoids .split('\n')[0]).
*/
export const getFirstLine = (text: string | null | undefined): string => {
if (!text) return '';
const index = text.indexOf('\n');
return index !== -1 ? text.substring(0, index) : text;
};
5 changes: 3 additions & 2 deletions src/helpers/movie.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { CSFDOptions } from '../types';
import {
addProtocol,
getColor,
getFirstLine,
parseDate,
parseFilmType,
parseISO8601Duration,
Expand Down Expand Up @@ -219,7 +220,7 @@ export const getMovieTitlesOther = (el: HTMLElement): CSFDTitlesOther[] => {

const titlesOther = namesNode.map((el) => {
const country = el.querySelector('img.flag').attributes.alt;
const title = el.textContent.trim().split('\n')[0];
const title = getFirstLine(el.textContent.trim());

if (country && title) {
return {
Expand Down Expand Up @@ -434,7 +435,7 @@ export const getMovieGroup = (

export const getMovieType = (el: HTMLElement): CSFDFilmTypes => {
const type = el.querySelector('.film-header-name .type');
const text = type?.innerText?.replace(/[{()}]/g, '').split('\n')[0].trim() || 'film';
const text = getFirstLine(type?.innerText?.replace(/[{()}]/g, ''))?.trim() || 'film';
return parseFilmType(text);
};

Expand Down
4 changes: 2 additions & 2 deletions src/helpers/search.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { HTMLElement } from 'node-html-parser';
import { CSFDColorRating, CSFDFilmTypes } from '../dto/global';
import { CSFDMovieCreator } from '../dto/movie';
import { CSFDColors } from '../dto/user-ratings';
import { addProtocol, parseColor, parseFilmType, parseIdFromUrl } from './global.helper';
import { addProtocol, getLastWord, parseColor, parseFilmType, parseIdFromUrl } from './global.helper';

type Creator = 'Režie:' | 'Hrají:';

Expand All @@ -26,7 +26,7 @@ export const getSearchUrl = (el: HTMLElement): string => {

export const getSearchColorRating = (el: HTMLElement): CSFDColorRating => {
return parseColor(
el.querySelector('.article-header i.icon').classNames.split(' ').pop() as CSFDColors
getLastWord(el.querySelector('.article-header i.icon').classNames) as CSFDColors
);
};

Expand Down
8 changes: 4 additions & 4 deletions src/helpers/user-ratings.helper.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { HTMLElement } from 'node-html-parser';
import { CSFDColorRating, CSFDFilmTypes, CSFDStars } from '../dto/global';
import { CSFDColors } from '../dto/user-ratings';
import { parseColor, parseDate, parseFilmType, parseIdFromUrl } from './global.helper';
import { getLastWord, parseColor, parseDate, parseFilmType, parseIdFromUrl } from './global.helper';

export const getUserRatingId = (el: HTMLElement): number => {
const url = el.querySelector('td.name .film-title-name').attributes.href;
return parseIdFromUrl(url);
};

export const getUserRating = (el: HTMLElement): CSFDStars => {
const ratingText = el.querySelector('td.star-rating-only .stars').classNames.split(' ').pop();
const ratingText = getLastWord(el.querySelector('td.star-rating-only .stars').classNames);

const rating = ratingText.includes('stars-') ? +ratingText.split('-').pop() : 0;
const rating = ratingText.includes('stars-') ? +getLastWord(ratingText, '-') : 0;
return rating as CSFDStars;
};

Expand All @@ -31,7 +31,7 @@ export const getUserRatingYear = (el: HTMLElement): number | null => {

export const getUserRatingColorRating = (el: HTMLElement): CSFDColorRating => {
const color = parseColor(
el.querySelector('td.name .icon').classNames.split(' ').pop() as CSFDColors
getLastWord(el.querySelector('td.name .icon').classNames) as CSFDColors
);
return color;
};
Expand Down
8 changes: 4 additions & 4 deletions src/helpers/user-reviews.helper.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { HTMLElement } from 'node-html-parser';
import { CSFDColorRating, CSFDFilmTypes, CSFDStars } from '../dto/global';
import { CSFDColors } from '../dto/user-ratings';
import { parseColor, parseDate, parseFilmType, parseIdFromUrl } from './global.helper';
import { getLastWord, parseColor, parseDate, parseFilmType, parseIdFromUrl } from './global.helper';

export const getUserReviewId = (el: HTMLElement): number => {
const url = el.querySelector('.film-title-name').attributes.href;
return parseIdFromUrl(url);
};

export const getUserReviewRating = (el: HTMLElement): CSFDStars => {
const ratingText = el.querySelector('.star-rating .stars').classNames.split(' ').pop();
const ratingText = getLastWord(el.querySelector('.star-rating .stars').classNames);

const rating = ratingText.includes('stars-') ? +ratingText.split('-').pop() : 0;
const rating = ratingText.includes('stars-') ? +getLastWord(ratingText, '-') : 0;
return rating as CSFDStars;
};

Expand All @@ -32,7 +32,7 @@ export const getUserReviewYear = (el: HTMLElement): number | null => {

export const getUserReviewColorRating = (el: HTMLElement): CSFDColorRating => {
const icon = el.querySelector('.film-title-inline i.icon');
const color = parseColor(icon?.classNames.split(' ').pop() as CSFDColors);
const color = parseColor(getLastWord(icon?.classNames) as CSFDColors);
return color;
};

Expand Down
Loading