Skip to content

Commit

Permalink
New example in documentation (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
pinksynth committed Oct 9, 2021
1 parent 77df638 commit ae9e576
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ const { pathToRegexp, match, parse, compile } = require("path-to-regexp");
// compile(path)
```

### Path to regexp

The `pathToRegexp` function will return a regular expression object based on the provided `path` argument. It accepts the following arguments:

- **path** A string, array of strings, or a regular expression.
- **keys** An array to populate with keys found in the path.
- **options**
- **keys** _(optional)_ An array to populate with keys found in the path.
- **options** _(optional)_
- **sensitive** When `true` the regexp will be case sensitive. (default: `false`)
- **strict** When `true` the regexp won't allow an optional trailing delimiter to match. (default: `false`)
- **end** When `true` the regexp will match to the end of the string. (default: `true`)
Expand Down Expand Up @@ -194,6 +198,21 @@ fn("/invalid"); //=> false
fn("/user/caf%C3%A9"); //=> { path: '/user/caf%C3%A9', index: 0, params: { id: 'café' } }
```

The `match` function can be used to custom match named parameters. For example, this can be used to whitelist a small number of valid paths:

```js
const urlMatch = match("/users/:id/:tab(home|photos|bio)", { decode: decodeURIComponent });

urlMatch("/users/1234/photos")
//=> { path: '/users/1234/photos', index: 0, params: { id: '1234', tab: 'photos' } }

urlMatch("/users/1234/bio")
//=> { path: '/users/1234/bio', index: 0, params: { id: '1234', tab: 'bio' } }

urlMatch("/users/1234/otherstuff")
//=> false
```

#### Process Pathname

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

0 comments on commit ae9e576

Please sign in to comment.