@@ -6,6 +6,9 @@ import type { TraktHistory } from '~/models/trakt/trakt-history.model';
6
6
7
7
import { TraktService } from '~/services/trakt.service' ;
8
8
9
+ const codesRegex = / [ s S ] ? \d + ( [ e E x X ] ) \d + / g;
10
+ const getCodeRegex = ( season : number , episode : number ) => new RegExp ( `^[sS]?0*${ season } ([eExX])0*${ episode } $` ) ;
11
+
9
12
export const useHistoryStore = defineStore ( 'data.history' , ( ) => {
10
13
const loading = ref ( true ) ;
11
14
const pageSize = ref ( 100 ) ;
@@ -29,19 +32,21 @@ export const useHistoryStore = defineStore('data.history', () => {
29
32
const searchHistory = ref ( '' ) ;
30
33
const filteredHistory = computed < TraktHistory [ ] > ( ( ) => {
31
34
if ( ! searchHistory . value ) return history . value ;
35
+ const _searchRaw = searchHistory . value . toLowerCase ( ) . trim ( ) ;
36
+ const _searchCode = _searchRaw . match ( codesRegex ) ;
37
+ const _search = _searchRaw . replace ( codesRegex , '' ) . trim ( ) ;
32
38
return history . value . filter ( ( item : TraktHistory ) => {
33
- if ( 'movie' in item && item . movie ?. title ?. toLowerCase ( ) . includes ( searchHistory . value . toLowerCase ( ) ) ) return true ;
34
- if ( 'show' in item && item . show . title ?. toLowerCase ( ) . includes ( searchHistory . value . toLowerCase ( ) ) ) return true ;
35
39
if ( 'episode' in item ) {
36
- if ( item . episode ?. title ?. toLowerCase ( ) . includes ( searchHistory . value . toLowerCase ( ) ) ) return true ;
37
-
38
- const shorthands = [
39
- `s${ item . episode ?. season ?. toString ( ) . padStart ( 2 , '0' ) } e${ item . episode ?. number ?. toString ( ) . padStart ( 2 , '0' ) } ` ,
40
- `${ item . episode ?. season } x${ item . episode ?. number } ` ,
41
- ] ;
42
- if ( shorthands . some ( shorthand => searchHistory . value . toLowerCase ( ) . includes ( shorthand ) ) ) return true ;
40
+ const codeRegex = getCodeRegex ( item . episode . season , item . episode . number ) ;
41
+ const matchCode = _searchCode ?. some ( _code => codeRegex . test ( _code ) ) ;
42
+ if ( _search && item . episode ?. title ?. toLowerCase ( ) . includes ( _search ) ) return _searchCode ? matchCode : true ;
43
+ if ( _search && 'show' in item && item . show . title ?. toLowerCase ( ) . includes ( _search ) ) return _searchCode ? matchCode : true ;
44
+ if ( codeRegex . test ( _searchRaw ) ) return true ;
43
45
}
44
- return ! ! ( item ?. watched_at && new Date ( item . watched_at ) . toLocaleString ( ) . toLowerCase ( ) . includes ( searchHistory . value . toLowerCase ( ) ) ) ;
46
+ if ( ! _search ) return false ;
47
+ if ( 'show' in item && item . show . title ?. toLowerCase ( ) . includes ( _search ) ) return true ;
48
+ if ( 'movie' in item && item . movie ?. title ?. toLowerCase ( ) . includes ( _search ) ) return true ;
49
+ return ! ! ( item ?. watched_at && new Date ( item . watched_at ) . toLocaleString ( ) . toLowerCase ( ) . includes ( _search ) ) ;
45
50
} ) ;
46
51
} ) ;
47
52
0 commit comments