8000 Update README encode example · pillarjs/path-to-regexp@b39edd4 · GitHub
[go: up one dir, main page]

Skip to content

Commit b39edd4

Browse files
committed
Update README encode example
1 parent 125c43e commit b39edd4

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

Readme.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,28 +187,28 @@ The `match` function will return a function for transforming paths into paramete
187187

188188
```js
189189
// Make sure you consistently `decode` segments.
190-
const match = match("/user/:id", { decode: decodeURIComponent });
190+
const fn = match("/user/:id", { decode: decodeURIComponent });
191191

192-
match("/user/123"); //=> { path: '/user/123', index: 0, params: { id: '123' } }
193-
match("/invalid"); //=> false
194-
match("/user/caf%C3%A9"); //=> { path: '/user/caf%C3%A9', index: 0, params: { id: 'café' } }
192+
fn("/user/123"); //=> { path: '/user/123', index: 0, params: { id: '123' } }
193+
fn("/invalid"); //=> false
194+
fn("/user/caf%C3%A9"); //=> { path: '/user/caf%C3%A9', index: 0, params: { id: 'café' } }
195195
```
196196

197197
#### Process Pathname
198198

199199
You should make sure variations of the same path match the expected `path`. Here's one possible solution using `encode`:
200200

201201
```js
202-
const match = match("/café", { encode: encodeURI, decode: decodeURIComponent });
202+
const fn = match("/café", { encode: encodeURI });
203203

204-
match("/user/caf%C3%A9"); //=> { path: '/user/caf%C3%A9', index: 0, params: { id: 'café' } }
204+
fn("/caf%C3%A9"); //=> { path: '/caf%C3%A9', index: 0, params: {} }
205205
```
206206

207-
**Note:** [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) automatically encodes pathnames for you.
207+
**Note:** [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) encodes paths, so `/café` would be normalized to `/caf%C3%A9` and match in the above example.
208208

209209
##### Alternative Using Normalize
210210

211-
Sometimes you won't have an already normalized pathname. You can normalize it yourself before processing:
211+
Sometimes you won't have already normalized paths to use, so you could normalize it yourself before matching:
212212

213213
```js
214214
/**

0 commit comments

Comments
 (0)
0