@@ -6,12 +6,15 @@ export default function isMatch(s: string, p: string): boolean {
6
6
const words = p . split ( / \* + / g) ;
7
7
// console.log(words);
8
8
9
- if ( words . length === 1 ) {
10
- return check_regular ( s , p ) ;
11
- }
12
9
if ( words . length === 2 && words [ 0 ] === "" && words [ 1 ] === "" ) {
13
10
return true ;
14
11
}
12
+ if (
13
+ words . length === 1 ||
14
+ ( words . length === 2 && ( words [ 0 ] === "" || words [ 1 ] === "" ) )
15
+ ) {
16
+ return check_regular ( s , p ) ;
17
+ }
15
18
if ( ! check_includes ( s , p ) ) return false ;
16
19
if ( words . length >= 2 ) {
17
20
return check_fixs ( s , words ) ;
@@ -21,7 +24,6 @@ export default function isMatch(s: string, p: string): boolean {
21
24
}
22
25
function check_includes ( s : string , p : string ) {
23
26
const words = Array . from ( new Set ( p . split ( / \? | \* + / g) . filter ( Boolean ) ) ) ;
24
-
25
27
if (
26
28
words . some ( ( word ) => {
27
29
return ! s . includes ( word ) ;
@@ -40,15 +42,15 @@ function check_fixs(s: string, words: string[]): boolean {
40
42
if ( suffix ) {
41
43
const matched = str . match (
42
44
//@ts -ignore
43
- new RegExp ( `^(.*?)${ suffix . replaceAll ( "?" , "." ) } $` ) ,
45
+ new RegExp ( `^(.*?)${ suffix . replaceAll ( "?" , "." ) } $` )
44
46
) ;
45
47
if ( ! matched ) return false ;
46
48
str = matched [ 1 ] ;
47
49
}
48
50
if ( prefix ) {
49
51
const matched = str . match (
50
52
//@ts -ignore
51
- new RegExp ( `^${ prefix . replaceAll ( "?" , "." ) } (.*?)$` ) ,
53
+ new RegExp ( `^${ prefix . replaceAll ( "?" , "." ) } (.*?)$` )
52
54
) ;
53
55
if ( ! matched ) return false ;
54
56
str = matched [ 1 ] ;
@@ -62,22 +64,22 @@ function check_regular(s: string, p: string): boolean {
62
64
// console.log("check_regular", s, p);
63
65
return new RegExp (
64
66
//@ts -ignore
65
- "^" + p . replaceAll ( "?" , "." ) + "$" ,
66
- "g" ,
67
+ "^" + p . replaceAll ( "?" , "." ) . replaceAll ( / \* + / g , ".*" ) + "$" ,
68
+ "g"
67
69
) . test ( s ) ;
68
70
}
69
71
function check_words ( s : string , words : string [ ] ) : boolean {
70
72
// console.log("check_words", s, words);
71
73
if ( words . length === 0 ) return true ;
72
74
const mid_index = words . reduce (
73
75
( a , v , i ) => ( v . length > words [ a ] . length ? i : a ) ,
74
- Math . floor ( words . length / 2 ) ,
76
+ Math . floor ( words . length / 2 )
75
77
) ;
76
78
// const mid_index = Math.floor(words.length / 2);
77
79
const middle = words [ mid_index ] ;
78
80
const matched_array = Array . from (
79
81
//@ts -ignore
80
- s . matchAll ( new RegExp ( `${ middle . replaceAll ( "?" , "." ) } ` , "g" ) ) ,
82
+ s . matchAll ( new RegExp ( `${ middle . replaceAll ( "?" , "." ) } ` , "g" ) )
81
83
) ;
82
84
83
85
// console.log(matched_array);
0 commit comments