Skip to content

Commit

Permalink
Document encode and decode reasonably
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Nov 12, 2019
1 parent 70eaaf2 commit 6e6f157
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const { pathToRegexp, match, parse, compile } = require("path-to-regexp");
- **delimiter** The default delimiter for segments. (default: `'/'`)
- **endsWith** Optional character, or list of characters, to treat as "end" characters.
- **whitelist** List of characters to consider delimiters when parsing. (default: `undefined`, any character)
- **encode** A function to encode strings before inserting into `RegExp`. (default: `x => x`)

```javascript
const keys = [];
Expand Down Expand Up @@ -156,10 +157,12 @@ regexpWord.exec("/users");
The `match` function will return a function for transforming paths into parameters:

```js
const match = match("/user/:id");
// Make sure you consistently `decode` segments.
const match = match("/user/:id", { decode: decodeURIComponent });

match("/user/123"); //=> { path: '/user/123', index: 0, params: { id: '123' } }
match("/invalid"); //=> false
match("/user/caf%C3%A9"); //=> { path: '/user/caf%C3%A9', index: 0, params: { id: 'café' } }
```

#### Normalize Pathname
Expand Down Expand Up @@ -216,14 +219,20 @@ console.log(tokens[2]);
The `compile` function will return a function for transforming parameters into a valid path:

```js
const toPath = compile("/user/:id");
// Make sure you encode your path segments consistently.
const toPath = compile("/user/:id", { encode: encodeURIComponent });

toPath({ id: 123 }); //=> "/user/123"
toPath({ id: "café" }); //=> "/user/caf%C3%A9"
toPath({ id: "/" }); //=> "/user/%2F"

toPath({ id: ":/" }); //=> "/user/%3A%2F"
toPath({ id: ":/" }, { encode: (value, token) => value, validate: false }); //=> "/user/:/"

// Without `encode`, you need to make sure inputs are encoded correctly.
const toPathRaw = compile("/user/:id");

toPathRaw({ id: "%3A%2F" }); //=> "/user/%3A%2F"
toPathRaw({ id: ":/" }, { validate: false }); //=> "/user/:/"

const toPathRepeated = compile("/:segment+");

Expand Down

0 comments on commit 6e6f157

Please sign in to comment.