mikmc commited on
Commit
8a35c81
·
verified ·
1 Parent(s): 8534f79

Update packages/addon/src/utils.ts

Browse files
Files changed (1) hide show
  1. packages/addon/src/utils.ts +18 -8
packages/addon/src/utils.ts CHANGED
@@ -30,6 +30,11 @@ function sanitizeTitle(title: string): string {
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,19 +43,22 @@ export function matchesTitle(fileTitle: string, query: string, strict: boolean):
38
  console.log('Query:', query);
39
  console.log('Sanitized title:', sanitizedTitle);
40
 
41
- // Strict match - title must exactly match query
42
- if (strict) {
43
  const result = sanitizedTitle === sanitizedQuery;
44
- console.log('Strict match result:', result);
45
  return result;
46
  }
47
 
48
- // Loose match - first word in query matches any part of the sanitized title
49
- // Improved handling to make sure we match whole words properly.
50
- const queryWords = sanitizedQuery.split(' ');
51
- const firstQueryWord = queryWords[0]; // Consider the first word for matching
 
 
52
 
53
- const re = new RegExp(`\\b${firstQueryWord}\\b`, 'i'); // Match first word from query
 
54
  const matchResult = re.test(sanitizedTitle); // test() returns a boolean
55
  console.log('Loose match result:', matchResult);
56
 
@@ -194,6 +202,8 @@ export function testTitleFunctions() {
194
  console.log(sanitizeTitle("Fête du cinéma")); // Expected: "Fete du cinema"
195
 
196
  console.log("\nMatches Title Tests:");
 
 
197
  console.log(matchesTitle("America's Next Top Model", "America's", false)); // Expected: true
198
  console.log(matchesTitle("Amérîcâ's Got Talent", "America", false)); // Expected: true
199
  console.log(matchesTitle("Am_er-ic.a the Beautiful", "America the Beautiful", false)); // Expected: true
 
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
  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
  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