Skip to content

Commit 480008c

Browse files
committed
fix: list usage examples in the readme
1 parent 4e61e4e commit 480008c

File tree

1 file changed

+67
-14
lines changed

1 file changed

+67
-14
lines changed

README.md

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,18 @@ Matches a URL against the given pattern.
3939

4040
```ts
4141
import { matchPattern } from '@msw/url'
42-
43-
matchPattern('/user/abc-123', '/user/:userId')
44-
// { matches: true, params: { userId: 'abc-123' } }
4542
```
4643

47-
This function supports the following features:
44+
`matchPattern` uses token-based comparision to completely forego regular expressions, which should, technically, make it more performant and less prone to vulnerabilities. Doesn't promise full feature parity with `path-to-regexp` but currently uses its test suite as the compliance bar.
4845

49-
- URL strings and `URL` instances as the `input`;
50-
- Absolute and relative URLs;
51-
- Wildcards (`http://*.domain.com:*/route`);
52-
- Path parameters, including optional (`:param?`), splat (`:param*`), and one-or-more (`:param+`) modifiers;
53-
- Encoded URL segments;
46+
#### Absolute and relative URLs
5447

55-
`matchPattern` uses token-based comparision to completely forego regular expressions, which should, technically, make it more performant and less prone to vulnerabilities. Doesn't promise full feature parity with `path-to-regexp` but currently uses its test suite as the compliance bar.
48+
```ts
49+
matchPattern('/user', '/user')
50+
matchPattern('https://acme.com/user', 'https://acme.com/user')
51+
```
5652

57-
### Trailing slashes
53+
#### Trailing slashes
5854

5955
The pattern is the source of truth. If it ends with a trailing slash, then the input must also end with it to match. If the pattern doesn't end with a trailing slash, then the trailing slash in the input is ignored when matching.
6056

@@ -70,16 +66,73 @@ matchPattern('/api', '/api/') // ❌
7066

7167
> This is to accommodate to JavaScript developers not being used to providing trailing slashes.
7268
73-
### Wildcards
69+
#### Path parameters
70+
71+
```ts
72+
matchPattern('/user/123', '/user/:userId')
73+
// { matches: true, params: { userId: '123' } }
74+
```
75+
76+
> Parameter values are always strings, just like any other segment of a URL.
77+
78+
##### Optional parameters
79+
80+
```ts
81+
matchPattern('/user/', '/user/:userId?')
82+
// { matches: true, params: {} }
83+
84+
matchPattern('/user/123', '/user/:userId?')
85+
// { matches: true, params: { userId: '123' } }
86+
```
87+
88+
##### Splat parameters
89+
90+
```ts
91+
matchPattern('/user/', '/user/:userId*')
92+
// { matches: true, params: {} }
93+
94+
matchPattern('/user/123', '/user/:userId*')
95+
// { matches: true, params: { userId: '123' } }
96+
97+
matchPattern('/user/123/messages', '/user/:userId*')
98+
// { matches: true, params: { userId: '123/messages' } }
99+
```
100+
101+
##### One-or-more parameters
102+
103+
```ts
104+
matchPattern('/user/', '/user/:userId+')
105+
// { matches: false }
106+
107+
matchPattern('/user/123', '/user/:userId+')
108+
// { matches: true, params: { userId: '123' } }
109+
110+
matchPattern('/user/123/messages', '/user/:userId+')
111+
// { matches: true, params: { userId: '123/messages' } }
112+
```
74113

75-
Wildcards _do not require value_ at their position.
114+
#### Wildcards
115+
116+
```ts
117+
matchPattern('http://acme.com/user/123', 'http://*.com/user/*')
118+
// { matches: true, params: { '0': 'acme', '1': '123' } }
119+
```
120+
121+
> If no value is present at the wildcard's position, the pattern will still match with the wildcard parameter value `''`. Wildcards are, effectively, unnamed splat parameters.
122+
123+
A slash preceding a wildcard is not a part of the wildcard and is _required_:
76124

77125
```ts
78126
matchPattern('/user/', '/user/*') // ✅ { params: { '0': ''} }
79127
matchPattern('/user', '/user/*') //
80128
```
81129

82-
> Note that the slash before the wildcard is still treated as required.
130+
#### Encoded URL segments
131+
132+
```ts
133+
matchPattern('/%E4%B8%AD%E6%96%87', '/:name')
134+
// { matches: true, params: { name: '中文' } }
135+
```
83136

84137
## Benchmarks
85138

0 commit comments

Comments
 (0)