Skip to content

Commit 140b824

Browse files
committed
Document ; and TokenData better
1 parent ec35fbd commit 140b824

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

Readme.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,20 @@ regexp.exec("/bar/baz");
164164
//=> [ '/bar/baz', 'bar/baz', index: 0 ]
165165
```
166166

167+
##### Custom separator
168+
169+
By default, parameters set the separator as the `prefix + suffix` of the token. Using `;` you can modify this:
170+
171+
```js
172+
const regexp = pathToRegexp("/name{/:parts;-}+");
173+
174+
regexp.exec("/name");
175+
//=> null
176+
177+
regexp.exec("/bar/1-2-3");
178+
//=> [ '/name/1-2-3', '1-2-3', index: 0 ]
179+
```
180+
167181
#### Wildcard
168182

169183
A wildcard can also be used. It is roughly equivalent to `(.*)`.
@@ -240,7 +254,9 @@ toPathRegexp({ id: "123" }); //=> "/user/123"
240254

241255
A `parse` function is available and returns `TokenData`, the set of tokens and other metadata parsed from the input string. `TokenData` is can passed directly into `pathToRegexp`, `match`, and `compile`. It accepts only two options, `delimiter` and `encodePath`, which makes those options redundant in the above methods.
242256

243-
### Token Information
257+
### Tokens
258+
259+
The `tokens` returned by `TokenData` is an array of strings or keys, represented as objects, with the following properties:
244260

245261
- `name` The name of the token
246262
- `prefix` _(optional)_ The prefix string for the segment (e.g. `"/"`)
@@ -249,6 +265,20 @@ A `parse` function is available and returns `TokenData`, the set of tokens and o
249265
- `modifier` _(optional)_ The modifier character used for the segment (e.g. `?`)
250266
- `separator` _(optional)_ The string used to separate repeated parameters
251267

268+
### Custom path
269+
270+
In some applications, you may not be able to use the `path-to-regexp` syntax (e.g. file-based routing), but you can still use this library for `match`, `compile`, and `pathToRegexp` by building your own `TokenData` instance. For example:
271+
272+
```js
273+
import { TokenData, match } from "path-to-regexp";
274+
275+
const tokens = ["/", { name: "foo" }];
276+
const path = new TokenData(tokens, "/");
277+
const fn = match(path);
278+
279+
fn("/test"); //=> { path: '/test', index: 0, params: { foo: 'test' } }
280+
```
281+
252282
## Errors
253283

254284
An effort has been made to ensure ambiguous paths from previous releases throw an error. This means you might be seeing an error when things worked before.

0 commit comments

Comments
 (0)