Skip to content

Fix (formatVideo): guard against undefined title and improve artist/title extraction#33

Open
1ly4s0 wants to merge 1 commit intovalerebron:masterfrom
1ly4s0:master
Open

Fix (formatVideo): guard against undefined title and improve artist/title extraction#33
1ly4s0 wants to merge 1 commit intovalerebron:masterfrom
1ly4s0:master

Conversation

@1ly4s0
Copy link

@1ly4s0 1ly4s0 commented May 14, 2025

This PR makes the formatVideo helper more robust by ensuring video.title is always treated as a string and by using a safer splitting and regex strategy to extract artist and title. Previously, calling video.title.split('-') would throw when video.title was undefined, and the simple match(/([^,]*)-(.*)/) could fail or produce unintended results.

Changes include:

  1. Ensure titleStr is always a string.

    • Coerce video.title to '' if it isn’t a string, then trim().
    • Assign this to video.original_title.
  2. Safer split logic.

    • Use titleStr.split('-') only to detect presence of a hyphen without risking a TypeError.
  3. Improved regex extraction.

    • Apply /^([^–-]+)[–-](.+)$/ to handle both ASCII - and en-dash , capturing exactly two groups: artist and title.
    • Trim both groups.
  4. Fallback behavior.

    • If regex doesn’t match, default to artist = '' and leave title = titleStr.
  5. Minor cleanup:

    • Log errors via console.error('formatVideo failed:', err) for better debugging.
    • Preserve existing date formatting logic untouched.

Why:

  • Prevents TypeError when video.title is undefined or not a string.
  • Handles different dash characters and whitespace more cleanly.
  • Provides a safe fallback for unexpected title formats.
  • Improves maintainability and reduces test failures in edge cases.

Testing:

  • Added unit tests covering:
    • video.title as undefined or non-string.
    • Titles without any dash.
    • Titles with ASCII dash and en-dash.
    • Malformed titles that shouldn’t match the regex.
  • Verified existing integration scenarios still produce the correct artist, title, and publishedAt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant