Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve documentation #256

Merged
merged 2 commits into from
Oct 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:
The `match` function can be used with custom match named parameters. For example, this can be used to whitelist a small number of valid paths:

Is the goal of this section just to demonstrate you can use all the same paths as the previous documentation sections? Would it be better just to say that explicitly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The goal is mostly just to demonstrate how the lib could be used to name some matched parameters. With the existing docs, it took me a while to understand that :tab(about|photos) would actually provide a tab param with the matched value.


```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