@@ -33,9 +33,9 @@ npm i @msw/url
3333
3434## API
3535
36- ### ` matchPattern(input, pattern ) `
36+ ### ` matchPattern(pattern, input ) `
3737
38- Matches a URL against the given pattern .
38+ Match a patteern against the given URL .
3939
4040``` ts
4141import { matchPattern } from ' @msw/url'
@@ -57,19 +57,19 @@ The pattern is the source of truth. If it ends with a trailing slash, then the i
5757``` ts
5858// No trailing slash in the pattern? Ignore it.
5959matchPattern (' /api' , ' /api' ) // ✅
60- matchPattern (' /api/ ' , ' /api' ) // ✅
60+ matchPattern (' /api' , ' /api/ ' ) // ✅
6161
6262// Trailing slash in the pattern? It's required.
6363matchPattern (' /api/' , ' /api/' ) // ✅
64- matchPattern (' /api' , ' /api/ ' ) // ❌
64+ matchPattern (' /api/ ' , ' /api' ) // ❌
6565```
6666
6767> This is to accommodate to JavaScript developers not being used to providing trailing slashes.
6868
6969#### Path parameters
7070
7171``` ts
72- matchPattern (' /user/123 ' , ' /user/:userId ' )
72+ matchPattern (' /user/:userId ' , ' /user/123 ' )
7373// { matches: true, params: { userId: '123' } }
7474```
7575
@@ -78,43 +78,43 @@ matchPattern('/user/123', '/user/:userId')
7878##### Optional parameters
7979
8080``` ts
81- matchPattern (' /user/' , ' /user/:userId? ' )
81+ matchPattern (' /user/:userId? ' , ' /user/' )
8282// { matches: true, params: {} }
8383
84- matchPattern (' /user/123 ' , ' /user/:userId? ' )
84+ matchPattern (' /user/:userId? ' , ' /user/123 ' )
8585// { matches: true, params: { userId: '123' } }
8686```
8787
8888##### Splat parameters
8989
9090``` ts
91- matchPattern (' /user/' , ' /user/:userId* ' )
91+ matchPattern (' /user/:userId* ' , ' /user/' )
9292// { matches: true, params: {} }
9393
94- matchPattern (' /user/123 ' , ' /user/:userId* ' )
94+ matchPattern (' /user/:userId* ' , ' /user/123 ' )
9595// { matches: true, params: { userId: '123' } }
9696
97- matchPattern (' /user/123/messages ' , ' /user/:userId* ' )
97+ matchPattern (' /user/:userId* ' , ' /user/123/messages ' )
9898// { matches: true, params: { userId: '123/messages' } }
9999```
100100
101101##### One-or-more parameters
102102
103103``` ts
104- matchPattern (' /user/' , ' /user/:userId+ ' )
104+ matchPattern (' /user/:userId+ ' , ' /user/' )
105105// { matches: false }
106106
107- matchPattern (' /user/123 ' , ' /user/:userId+ ' )
107+ matchPattern (' /user/:userId+ ' , ' /user/123 ' )
108108// { matches: true, params: { userId: '123' } }
109109
110- matchPattern (' /user/123/messages ' , ' /user/:userId+ ' )
110+ matchPattern (' /user/:userId+ ' , ' /user/123/messages ' )
111111// { matches: true, params: { userId: '123/messages' } }
112112```
113113
114114#### Wildcards
115115
116116``` ts
117- matchPattern (' http://acme .com/user/123 ' , ' http://* .com/user/* ' )
117+ matchPattern (' http://* .com/user/* ' , ' http://acme .com/user/123 ' )
118118// { matches: true, params: { '0': 'acme', '1': '123' } }
119119```
120120
@@ -123,14 +123,14 @@ matchPattern('http://acme.com/user/123', 'http://*.com/user/*')
123123A slash preceding a wildcard is not a part of the wildcard and is _ required_ :
124124
125125``` ts
126- matchPattern (' /user/' , ' /user/* ' ) // ✅ { params: { '0': ''} }
127- matchPattern (' /user' , ' /user/* ' ) // ❌
126+ matchPattern (' /user/* ' , ' /user/' ) // ✅ { params: { '0': ''} }
127+ matchPattern (' /user/* ' , ' /user' ) // ❌
128128```
129129
130130#### Encoded URL segments
131131
132132``` ts
133- matchPattern (' /%E4%B8%AD%E6%96%87' , ' /:name ' )
133+ matchPattern (' /:name ' , ' / %E4%B8%AD%E6%96%87' )
134134// { matches: true, params: { name: '中文' } }
135135```
136136
0 commit comments