Update packages/addon/src/utils.ts
Browse files25 unmodified lines262728293031323 unmodified lines363738394041424344454647484950515253543 unmodified lines5859606162636465666768109 unmodified lines17817918018125 unmodified lines}// Sanitizes the title by removing unnecessary file extensions and metadatafunction sanitizeTitle(title: string, isStrict: boolean = false): string {let sanitized = title.replace(/\.(1080p|HEVC|x265|AAC|mp4|mkv|avi|mov|webm|flv|wmv)/gi, '') // Remove file extensions.replace(/[-_]/g, ' ') // Replace hyphens and underscores with spaces3 unmodified linessanitized = sanitized.replace(/\b(Americas)\b/g, "America's"); // Preserve apostrophe// Handle stricter sanitization for movies (e.g., "Alive" case)if (isStrict) {// Remove overly common words in movie titles like "The", "Alive"sanitized = sanitized.replace(/\b(Alive|The|The\s)/g, '').trim();}log('sanitizeTitle - Sanitized title:', sanitized);return sanitized;}export function matchesTitle(fileTitle: string, query: string, strict: boolean): boolean {const sanitizedQuery = query.toLowerCase().trim();const sanitizedTitle = sanitizeTitle(fileTitle, strict).toLowerCase().trim();log('matchesTitle - Matching title:', { fileTitle, query, sanitizedTitle });3 unmodified linesreturn result;}const re = new RegExp(`\\b${sanitizedQuery.split(' ')[0]}\\b`, 'i');const matchResult = re.test(sanitizedTitle);log('matchesTitle - Loose match result:', matchResult);return matchResult;}export function createStreamUrl({109 unmodified linesconst result = str.charAt(0).toUpperCase() + str.slice(1);log('capitalizeFirstLetter - Capitalized first letter:', result);return result;}25 unmodified lines262728293031323 unmodified lines363738394041424344454647484950515253545556575859606162636465666768697071723 unmodified lines76777879808182838485109 unmodified lines19519619719819920020120220320420520620720820921021121221321421521621721821922022125 unmodified lines}// Sanitizes the title by removing unnecessary file extensions and metadatafunction sanitizeTitle(title: string): string {let sanitized = title.replace(/\.(1080p|HEVC|x265|AAC|mp4|mkv|avi|mov|webm|flv|wmv)/gi, '') // Remove file extensions.replace(/[-_]/g, ' ') // Replace hyphens and underscores with spaces3 unmodified linessanitized = sanitized.replace(/\b(Americas)\b/g, "America's"); // Preserve apostrophelog('sanitizeTitle - Sanitized title:', sanitized);return sanitized;}// New: Enhanced matching for "common word" casesfunction matchesWithYearAndGenre(fileTitle: string, query: string, year: string | undefined, genre: string | undefined): boolean {const sanitizedQuery = query.toLowerCase().trim();const sanitizedTitle = sanitizeTitle(fileTitle).toLowerCase().trim();// Handle cases where "Alive" is a common word but needs to be matched with the year/genreconst yearPattern = year ? new RegExp(`(${year})`, 'i') : null;const genrePattern = genre ? new RegExp(`(${genre})`, 'i') : null;// Match based on title, and if year or genre is available, match those as welllet match = sanitizedTitle.includes(sanitizedQuery);if (match && yearPattern) {match = yearPattern.test(sanitizedTitle);}if (match && genrePattern) {match = genrePattern.test(sanitizedTitle);}log('matchesWithYearAndGenre - Title matching with year/genre:', { fileTitle, query, match });return match;}export function matchesTitle(fileTitle: string, query: string, strict: boolean, year?: string, genre?: string): boolean {const sanitizedQuery = query.toLowerCase().trim();const sanitizedTitle = sanitizeTitle(fileTitle).toLowerCase().trim();log('matchesTitle - Matching title:', { fileTitle, query, sanitizedTitle });3 unmodified linesreturn result;}// Use year and genre matching logic for problematic casesconst result = matchesWithYearAndGenre(fileTitle, query, year, genre);log('matchesTitle - Loose match result:', result);return result;}export function createStreamUrl({109 unmodified linesconst result = str.charAt(0).toUpperCase() + str.slice(1);log('capitalizeFirstLetter - Capitalized first letter:', result);return result;}export function testTitleFunctions() {log("testTitleFunctions - Sanitize Title Tests:");log(sanitizeTitle("America's")); // Expected: "America's"log(sanitizeTitle("Amérîcâ")); // Expected: "America"log(sanitizeTitle("Am_er-ic.a")); // Expected: "Am er ic a"log(sanitizeTitle("D'où vient-il?")); // Expected: "D'ou vient il"log(sanitizeTitle("Fête du cinéma")); // Expected: "Fete du cinema"log("\ntestTitleFunctions - Matches Title Tests:");log(matchesTitle("America's Next Top Model", "America's", false)); // Expected: truelog(matchesTitle("Amérîcâ's Got Talent", "America", false)); // Expected: truelog(matchesTitle("Am_er-ic.a the Beautiful", "America the Beautiful", false)); // Expected: truelog(matchesTitle("La fête de l'été", "fete", false)); // Expected: truelog(matchesTitle("Fête du cinéma", "cinema", false)); // Expected: truelog(matchesTitle("Alive", "Alive", true)); // Expected: truelog(matchesTitle("Alive (1993)", "Alive", false, "1993", "Drama")); // Expected: truelog("End of Title Functions Test");}// Run the testtestTitleFunctions();
packages/addon/src/utils.ts+55-15