Skip to content

Commit b3cd2a5

Browse files
committed
Update path normalization documentation
1 parent 72b5f44 commit b3cd2a5

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

Readme.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,21 @@ match("/invalid"); //=> false
181181
match("/user/caf%C3%A9"); //=> { path: '/user/caf%C3%A9', index: 0, params: { id: 'café' } }
182182
```
183183

184-
#### Normalize Pathname
184+
#### Process Pathname
185185

186-
You should make sure variations of the same path to match your input `path`. Here's one possible solution:
186+
You should make sure variations of the same path match the expected `path`. Here's one possible solution using `encode`:
187+
188+
```js
189+
const match = match("/café", { encode: encodeURI, decode: decodeURIComponent });
190+
191+
match("/user/caf%C3%A9"); //=> { path: '/user/caf%C3%A9', index: 0, params: { id: 'café' } }
192+
```
193+
194+
**Note:** [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) automatically encodes pathnames for you.
195+
196+
##### Alternative Using Normalize
197+
198+
Sometimes you won't have an already normalized pathname. You can normalize it yourself before processing:
187199

188200
```js
189201
/**
@@ -202,15 +214,14 @@ function normalizePathname(pathname: string) {
202214
);
203215
}
204216

217+
// Two possible ways of writing `/café`:
205218
const re = pathToRegexp("/caf\u00E9");
206219
const input = encodeURI("/cafe\u0301");
207220

208221
re.test(input); //=> false
209222
re.test(normalizePathname(input)); //=> true
210223
```
211224

212-
**Note:** [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) automatically encodes pathnames for you, which would result in a consistent match if you use `encodeURI` in `pathToRegexp` options.
213-
214225
### Parse
215226

216227
The `parse` function will return a list of strings and keys from a path string:

0 commit comments

Comments
 (0)