-
-
Notifications
You must be signed in to change notification settings - Fork 14
⚡ Bolt: [performance improvement] array allocations and regex #173
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,15 +1,15 @@ | ||||||||||||||||||||||||
| 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; | ||||||||||||||||||||||||
| return rating as CSFDStars; | ||||||||||||||||||||||||
|
Comment on lines
+12
to
15
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. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Verify the unsafe dereference in this file.
rg -n -C2 "querySelector\\('td\\.star-rating-only \\.stars'\\)\\.classNames" src/helpers/user-ratings.helper.tsRepository: bartholomej/node-csfd-api Length of output: 324 Guard This will throw when the rating node is missing, making the parser brittle against CSFD layout changes. The helper must use optional chaining or try/catch per the robustness requirements for this directory. Suggested fix export const getUserRating = (el: HTMLElement): CSFDStars => {
- const ratingText = getLastWord(el.querySelector('td.star-rating-only .stars').classNames);
+ const starsClass = el.querySelector('td.star-rating-only .stars')?.classNames ?? '';
+ const ratingText = getLastWord(starsClass);
const rating = ratingText.includes('stars-') ? +ratingText.split('-').pop() : 0;
return rating as CSFDStars;
};📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,4 +96,3 @@ export const csfd = new Csfd( | |
| ); | ||
|
|
||
| export type * from './dto'; | ||
|
|
||
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.
🧩 Analysis chain
🏁 Script executed:
Repository: bartholomej/node-csfd-api
Length of output: 273
🏁 Script executed:
Repository: bartholomej/node-csfd-api
Length of output: 503
🏁 Script executed:
Repository: bartholomej/node-csfd-api
Length of output: 1748
Add null-safe handling for icon class extraction.
querySelector('.article-header i.icon')can return null; direct.classNamesaccess will throw at runtime.Suggested fix
export const getSearchColorRating = (el: HTMLElement): CSFDColorRating => { - return parseColor( - getLastWord(el.querySelector('.article-header i.icon').classNames) as CSFDColors - ); + const iconClasses = el.querySelector('.article-header i.icon')?.classNames ?? ''; + return parseColor(getLastWord(iconClasses) as CSFDColors); };Per guideline: "Never assume an element exists. CSFD changes layouts. Use optional chaining
?.ortry/catchinside helpers for robust scraping."🤖 Prompt for AI Agents