Skip to content

Commit d919e8a

Browse files
committed
🐛 修复match兼容问题
1 parent dfa0231 commit d919e8a

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

src/pkg/match.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class Match<T> {
99
return {
1010
scheme: match[1],
1111
host: match[2],
12-
path: match[4] || '/',
12+
path: match[4] || (url[url.length - 1] === '*' ? '*' : '/'),
1313
search: match[5],
1414
};
1515
}
@@ -21,7 +21,7 @@ export class Match<T> {
2121
return {
2222
scheme: '*',
2323
host: match[1],
24-
path: match[3] || '/',
24+
path: match[3] || (url[url.length - 1] === '*' ? '*' : '/'),
2525
search: match[4],
2626
};
2727
}

tests/match.test.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,26 @@ describe('UrlMatch', () => {
7676

7777

7878
describe('UrlMatch2', () => {
79-
const url = new UrlMatch<string>();
8079
it('http*', () => {
80+
const url = new UrlMatch<string>();
8181
url.add('http*', 'ok1');
82-
expect(url.match('http://www.http.com')).toEqual(['ok1']);
82+
url.add('*://*', 'ok2');
83+
expect(url.match('http://www.http.com')).toEqual(['ok1', 'ok2']);
84+
expect(url.match('https://pan.baidu.com/disk/home?from=newversion&stayAtHome=true#/all?path=%2F&vmode=list')).toEqual(['ok1', 'ok2']);
8385
});
8486
it('port', () => {
87+
const url = new UrlMatch<string>();
8588
url.add('http://domain:8080', 'ok2');
86-
expect(url.match('http://domain:8080/')).toEqual(['ok1', 'ok2']);
87-
expect(url.match('http://domain:8080/123')).toEqual(['ok1']);
89+
expect(url.match('http://domain:8080/')).toEqual(['ok2']);
90+
expect(url.match('http://domain:8080/123')).toEqual([]);
8891
});
8992
it('无/', () => {
93+
const url = new UrlMatch<string>();
9094
url.add('http://domain2', 'ok3');
9195
url.add('http://domain2*', 'ok4');
92-
expect(url.match('http://domain2/')).toEqual(['ok1', 'ok3', 'ok4']);
93-
expect(url.match('http://domain2.com/')).toEqual(['ok1', 'ok4']);
94-
expect(url.match('http://domain2/123')).toEqual(['ok1']);
96+
expect(url.match('http://domain2/')).toEqual(['ok3', 'ok4']);
97+
expect(url.match('http://domain2.com/')).toEqual(['ok4']);
98+
expect(url.match('http://domain2/123')).toEqual(['ok4']);
9599
})
96100
});
97101

0 commit comments

Comments
 (0)