mikmc commited on
Commit
954adc6
·
verified ·
1 Parent(s): 8a35c81

Update packages/addon/src/utils.ts

Browse files
Files changed (1) hide show
  1. packages/addon/src/utils.ts +8 -14
packages/addon/src/utils.ts CHANGED
@@ -30,11 +30,6 @@ function sanitizeTitle(title: string): string {
30
  return sanitized;
31
  }
32
 
33
- // Determines if the title is a single word (no spaces)
34
- function isSingleWord(title: string): boolean {
35
- return title.split(' ').length === 1;
36
- }
37
-
38
  export function matchesTitle(fileTitle: string, query: string, strict: boolean): boolean {
39
  const sanitizedQuery = query.toLowerCase().trim();
40
  const sanitizedTitle = sanitizeTitle(fileTitle).toLowerCase().trim();
@@ -43,22 +38,23 @@ export function matchesTitle(fileTitle: string, query: string, strict: boolean):
43
  console.log('Query:', query);
44
  console.log('Sanitized title:', sanitizedTitle);
45
 
46
- // If the title is a single word, apply strict matching
47
- if (isSingleWord(sanitizedTitle)) {
 
 
 
48
  const result = sanitizedTitle === sanitizedQuery;
49
- console.log('Strict match result for single word:', result);
50
  return result;
51
  }
52
 
53
- // For multi-word titles, apply loose matching based on the first word of the query
54
  if (strict) {
55
  const result = sanitizedTitle === sanitizedQuery;
56
- console.log('Strict match result for multi-word:', result);
57
  return result;
58
  }
59
 
60
- // Loose match for multi-word titles (matches the first word of the query)
61
- const re = new RegExp(`\\b${sanitizedQuery.split(' ')[0]}\\b`, 'i'); // Match the first word of the query
62
  const matchResult = re.test(sanitizedTitle); // test() returns a boolean
63
  console.log('Loose match result:', matchResult);
64
 
@@ -202,8 +198,6 @@ export function testTitleFunctions() {
202
  console.log(sanitizeTitle("Fête du cinéma")); // Expected: "Fete du cinema"
203
 
204
  console.log("\nMatches Title Tests:");
205
- console.log(matchesTitle("Alive", "Alive", true)); // Expected: true (strict match)
206
- console.log(matchesTitle("Alive", "Live", true)); // Expected: false (strict match)
207
  console.log(matchesTitle("America's Next Top Model", "America's", false)); // Expected: true
208
  console.log(matchesTitle("Amérîcâ's Got Talent", "America", false)); // Expected: true
209
  console.log(matchesTitle("Am_er-ic.a the Beautiful", "America the Beautiful", false)); // Expected: true
 
30
  return sanitized;
31
  }
32
 
 
 
 
 
 
33
  export function matchesTitle(fileTitle: string, query: string, strict: boolean): boolean {
34
  const sanitizedQuery = query.toLowerCase().trim();
35
  const sanitizedTitle = sanitizeTitle(fileTitle).toLowerCase().trim();
 
38
  console.log('Query:', query);
39
  console.log('Sanitized title:', sanitizedTitle);
40
 
41
+ // Handle case for single word titles strictly
42
+ const isSingleWordQuery = sanitizedQuery.split(' ').length === 1;
43
+ const isSingleWordTitle = sanitizedTitle.split(' ').length === 1;
44
+
45
+ if (isSingleWordQuery && isSingleWordTitle) {
46
  const result = sanitizedTitle === sanitizedQuery;
47
+ console.log('Strict match result for single-word title:', result);
48
  return result;
49
  }
50
 
 
51
  if (strict) {
52
  const result = sanitizedTitle === sanitizedQuery;
53
+ console.log('Strict match result for multi-word title:', result);
54
  return result;
55
  }
56
 
57
+ const re = new RegExp(`\\b${sanitizedQuery.split(' ')[0]}\\b`, 'i'); // Match the first part of the query only
 
58
  const matchResult = re.test(sanitizedTitle); // test() returns a boolean
59
  console.log('Loose match result:', matchResult);
60
 
 
198
  console.log(sanitizeTitle("Fête du cinéma")); // Expected: "Fete du cinema"
199
 
200
  console.log("\nMatches Title Tests:");
 
 
201
  console.log(matchesTitle("America's Next Top Model", "America's", false)); // Expected: true
202
  console.log(matchesTitle("Amérîcâ's Got Talent", "America", false)); // Expected: true
203
  console.log(matchesTitle("Am_er-ic.a the Beautiful", "America the Beautiful", false)); // Expected: true