Skip to content

Commit cb1c5a2

Browse files
committed
feat: flip matchPattern call signature to (pattern, input)
1 parent 8a8c33d commit cb1c5a2

6 files changed

Lines changed: 144 additions & 144 deletions

File tree

README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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
4141
import { 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.
5959
matchPattern('/api', '/api') //
60-
matchPattern('/api/', '/api') //
60+
matchPattern('/api', '/api/') //
6161

6262
// Trailing slash in the pattern? It's required.
6363
matchPattern('/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/*')
123123
A 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

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,8 @@ function decode(value: string): string {
429429
* // { matches: true, params: { userId: '123' } }
430430
*/
431431
export function matchPattern(
432-
input: MatchPatternInput,
433432
pattern: string,
433+
input: MatchPatternInput,
434434
): MatchResult {
435435
const rawInput = typeof input === 'string' ? input : input.href
436436

tests/comparison.bench.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('single param', () => {
5151
const urlPattern = new URLPattern({ pathname: pattern })
5252

5353
bench('matchPattern', () => {
54-
matchPattern(input, pattern)
54+
matchPattern(pattern, input)
5555
})
5656

5757
bench('path-to-regexp', () => {
@@ -79,7 +79,7 @@ describe('multiple params', () => {
7979
const urlPattern = new URLPattern({ pathname: pattern })
8080

8181
bench('matchPattern', () => {
82-
matchPattern(input, pattern)
82+
matchPattern(pattern, input)
8383
})
8484

8585
bench('path-to-regexp', () => {
@@ -106,7 +106,7 @@ describe('param with extension', () => {
106106
const urlPattern = new URLPattern({ pathname: pattern })
107107

108108
bench('matchPattern', () => {
109-
matchPattern(input, pattern)
109+
matchPattern(pattern, input)
110110
})
111111

112112
bench('path-to-regexp', () => {
@@ -130,7 +130,7 @@ describe('non-match (miss)', () => {
130130
const urlPattern = new URLPattern({ pathname: pattern })
131131

132132
bench('matchPattern', () => {
133-
matchPattern(input, pattern)
133+
matchPattern(pattern, input)
134134
})
135135

136136
bench('path-to-regexp', () => {
@@ -158,7 +158,7 @@ describe('deeply nested params', () => {
158158
const urlPattern = new URLPattern({ pathname: pattern })
159159

160160
bench('matchPattern', () => {
161-
matchPattern(input, pattern)
161+
matchPattern(pattern, input)
162162
})
163163

164164
bench('path-to-regexp', () => {
@@ -208,7 +208,7 @@ describe('full URL: single param', () => {
208208
const urlPattern = new URLPattern(pattern)
209209

210210
bench('matchPattern', () => {
211-
matchPattern(input, pattern)
211+
matchPattern(pattern, input)
212212
})
213213

214214
bench('route-pattern', () => {
@@ -228,7 +228,7 @@ describe('full URL: multiple params', () => {
228228
const urlPattern = new URLPattern(pattern)
229229

230230
bench('matchPattern', () => {
231-
matchPattern(input, pattern)
231+
matchPattern(pattern, input)
232232
})
233233

234234
bench('route-pattern', () => {
@@ -248,7 +248,7 @@ describe('full URL: non-match (miss)', () => {
248248
const urlPattern = new URLPattern(pattern)
249249

250250
bench('matchPattern', () => {
251-
matchPattern(input, pattern)
251+
matchPattern(pattern, input)
252252
})
253253

254254
bench('route-pattern', () => {

0 commit comments

Comments
 (0)