11import type { FileDetails } from "$lib/diff-viewer.svelte" ;
22import { FILE_STATUSES } from "$lib/github.svelte" ;
3+ import type { TryCompileRegexSuccess } from "$lib/util" ;
34import { SvelteSet } from "svelte/reactivity" ;
45
56export type FilePathFilterMode = "include" | "exclude" ;
@@ -15,30 +16,16 @@ export class FilePathFilter {
1516 }
1617}
1718
18- function tryCompileRegex ( pattern : string ) : RegExp | undefined {
19- try {
20- return new RegExp ( pattern ) ;
21- } catch {
22- return undefined ;
23- }
24- }
25-
2619export class DiffFilterDialogState {
2720 filePathFilters = new SvelteSet < FilePathFilter > ( ) ;
2821 reverseFilePathFilters = $derived ( [ ...this . filePathFilters ] . toReversed ( ) ) ;
29- filePathInclusions = $derived ( this . reverseFilePathFilters . filter ( ( f ) => f . mode === "include" ) ) ;
30- filePathExclusions = $derived ( this . reverseFilePathFilters . filter ( ( f ) => f . mode === "exclude" ) ) ;
22+ filterFunction = $derived ( this . createFilterFunction ( ) ) ;
3123
3224 selectedFileStatuses : string [ ] = $state ( [ ...FILE_STATUSES ] ) ;
3325
34- addFilePathFilter ( filterString : string , mode : FilePathFilterMode ) : { invalidRegex : boolean } {
35- const compiled = tryCompileRegex ( filterString ) ;
36- if ( ! compiled ) {
37- return { invalidRegex : true } ;
38- }
39- const newFilter = new FilePathFilter ( filterString , compiled , mode ) ;
26+ addFilePathFilter ( regex : TryCompileRegexSuccess , mode : FilePathFilterMode ) {
27+ const newFilter = new FilePathFilter ( regex . input , regex . regex , mode ) ;
4028 this . filePathFilters . add ( newFilter ) ;
41- return { invalidRegex : false } ;
4229 }
4330
4431 setDefaults ( ) {
@@ -47,23 +34,33 @@ export class DiffFilterDialogState {
4734 }
4835
4936 filterFile ( file : FileDetails ) : boolean {
50- const statusAllowed = this . selectedFileStatuses . includes ( file . status ) ;
51- if ( ! statusAllowed ) {
52- return false ;
53- }
54- for ( const exclude of this . filePathExclusions ) {
55- if ( exclude . regex . test ( file . toFile ) || exclude . regex . test ( file . fromFile ) ) {
37+ return this . filterFunction ( file ) ;
38+ }
39+
40+ private createFilterFunction ( ) {
41+ const filePathInclusions = this . reverseFilePathFilters . filter ( ( f ) => f . mode === "include" ) ;
42+ const filePathExclusions = this . reverseFilePathFilters . filter ( ( f ) => f . mode === "exclude" ) ;
43+ const selectedFileStatuses = [ ...this . selectedFileStatuses ] ;
44+
45+ return ( file : FileDetails ) => {
46+ const statusAllowed = selectedFileStatuses . includes ( file . status ) ;
47+ if ( ! statusAllowed ) {
5648 return false ;
5749 }
58- }
59- if ( this . filePathInclusions . length > 0 ) {
60- for ( const include of this . filePathInclusions ) {
61- if ( include . regex . test ( file . toFile ) || include . regex . test ( file . fromFile ) ) {
62- return true ;
50+ for ( const exclude of filePathExclusions ) {
51+ if ( exclude . regex . test ( file . toFile ) || exclude . regex . test ( file . fromFile ) ) {
52+ return false ;
53+ }
54+ }
55+ if ( filePathInclusions . length > 0 ) {
56+ for ( const include of filePathInclusions ) {
57+ if ( include . regex . test ( file . toFile ) || include . regex . test ( file . fromFile ) ) {
58+ return true ;
59+ }
6360 }
61+ return false ;
6462 }
65- return false ;
66- }
67- return true ;
63+ return true ;
64+ } ;
6865 }
6966}
0 commit comments