path-to-regexp 7.2.0 → 8.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Readme.md +88 -198
- package/dist/index.d.ts +67 -70
- package/dist/index.js +287 -307
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
package/Readme.md
CHANGED
@@ -17,208 +17,87 @@ npm install path-to-regexp --save
|
|
17
17
|
## Usage
|
18
18
|
|
19
19
|
```js
|
20
|
-
const {
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
### Path to regexp
|
29
|
-
|
30
|
-
The `pathToRegexp` function returns a regular expression with `keys` as a property. It accepts the following arguments:
|
31
|
-
|
32
|
-
- **path** A string.
|
33
|
-
- **options** _(optional)_
|
34
|
-
- **sensitive** Regexp will be case sensitive. (default: `false`)
|
35
|
-
- **trailing** Allows optional trailing delimiter to match. (default: `true`)
|
36
|
-
- **strict** Verify patterns are valid and safe to use. (default: `false`, recommended: `true`)
|
37
|
-
- **end** Match to the end of the string. (default: `true`)
|
38
|
-
- **start** Match from the beginning of the string. (default: `true`)
|
39
|
-
- **loose** Allow the delimiter to be arbitrarily repeated, e.g. `/` or `///`. (default: `true`)
|
40
|
-
- **delimiter** The default delimiter for segments, e.g. `[^/]` for `:named` parameters. (default: `'/'`)
|
41
|
-
- **encodePath** A function for encoding input strings. (default: `x => x`, recommended: [`encodeurl`](https://github.com/pillarjs/encodeurl) for unicode encoding)
|
42
|
-
|
43
|
-
```js
|
44
|
-
const regexp = pathToRegexp("/foo/:bar");
|
45
|
-
// regexp = /^\/+foo(?:\/+([^\/]+?))(?:\/+)?$/i
|
46
|
-
// keys = [{ name: 'bar', prefix: '', suffix: '', pattern: '', modifier: '' }]
|
20
|
+
const {
|
21
|
+
match,
|
22
|
+
pathToRegexp,
|
23
|
+
compile,
|
24
|
+
parse,
|
25
|
+
stringify,
|
26
|
+
} = require("path-to-regexp");
|
47
27
|
```
|
48
28
|
|
49
|
-
**Please note:** The `RegExp` returned by `path-to-regexp` is intended for ordered data (e.g. pathnames, hostnames). It can not handle arbitrarily ordered data (e.g. query strings, URL fragments, JSON, etc).
|
50
|
-
|
51
29
|
### Parameters
|
52
30
|
|
53
|
-
|
54
|
-
|
55
|
-
#### Named parameters
|
56
|
-
|
57
|
-
Named parameters are defined by prefixing a colon to the parameter name (`:foo`). Parameter names can use any valid unicode identifier characters (similar to JavaScript).
|
31
|
+
Parameters match arbitrary strings in a path by matching up to the end of the segment, or up to any proceeding tokens. They are defined by prefixing a colon to the parameter name (`:foo`). Parameter names can use any valid JavaScript identifier, or be double quoted to use other characters (`:"param-name"`).
|
58
32
|
|
59
33
|
```js
|
60
|
-
const
|
61
|
-
// keys = [{ name: 'foo', ... }, { name: 'bar', ... }]
|
62
|
-
|
63
|
-
regexp.exec("/test/route");
|
64
|
-
//=> [ '/test/route', 'test', 'route', index: 0 ]
|
65
|
-
```
|
66
|
-
|
67
|
-
##### Custom matching parameters
|
68
|
-
|
69
|
-
Parameters can have a custom regexp, which overrides the default match (`[^/]+`). For example, you can match digits or names in a path:
|
70
|
-
|
71
|
-
```js
|
72
|
-
const regexpNumbers = pathToRegexp("/icon-:foo(\\d+).png");
|
73
|
-
// keys = [{ name: 'foo', ... }]
|
74
|
-
|
75
|
-
regexpNumbers.exec("/icon-123.png");
|
76
|
-
//=> ['/icon-123.png', '123']
|
77
|
-
|
78
|
-
regexpNumbers.exec("/icon-abc.png");
|
79
|
-
//=> null
|
80
|
-
|
81
|
-
const regexpWord = pathToRegexp("/(user|u)");
|
82
|
-
// keys = [{ name: 0, ... }]
|
83
|
-
|
84
|
-
regexpWord.exec("/u");
|
85
|
-
//=> ['/u', 'u']
|
86
|
-
|
87
|
-
regexpWord.exec("/users");
|
88
|
-
//=> null
|
89
|
-
```
|
90
|
-
|
91
|
-
**Tip:** Backslashes need to be escaped with another backslash in JavaScript strings.
|
92
|
-
|
93
|
-
#### Unnamed parameters
|
94
|
-
|
95
|
-
It is possible to define a parameter without a name. The name will be numerically indexed:
|
96
|
-
|
97
|
-
```js
|
98
|
-
const regexp = pathToRegexp("/:foo/(.*)");
|
99
|
-
// keys = [{ name: 'foo', ... }, { name: '0', ... }]
|
100
|
-
|
101
|
-
regexp.exec("/test/route");
|
102
|
-
//=> [ '/test/route', 'test', 'route', index: 0 ]
|
103
|
-
```
|
104
|
-
|
105
|
-
##### Custom prefix and suffix
|
106
|
-
|
107
|
-
Parameters can be wrapped in `{}` to create custom prefixes or suffixes for your segment:
|
108
|
-
|
109
|
-
```js
|
110
|
-
const regexp = pathToRegexp("{/:attr1}?{-:attr2}?{-:attr3}?");
|
111
|
-
|
112
|
-
regexp.exec("/test");
|
113
|
-
// => ['/test', 'test', undefined, undefined]
|
34
|
+
const fn = match("/:foo/:bar");
|
114
35
|
|
115
|
-
|
116
|
-
|
36
|
+
fn("/test/route");
|
37
|
+
//=> { path: '/test/route', params: { foo: 'test', bar: 'route' } }
|
117
38
|
```
|
118
39
|
|
119
|
-
|
40
|
+
### Wildcard
|
120
41
|
|
121
|
-
|
122
|
-
|
123
|
-
##### Optional
|
124
|
-
|
125
|
-
Parameters can be suffixed with a question mark (`?`) to make the parameter optional.
|
42
|
+
Wildcard parameters match one or more characters across multiple segments. They are defined the same way as regular parameters, but are prefixed with an asterisk (`*foo`).
|
126
43
|
|
127
44
|
```js
|
128
|
-
const
|
129
|
-
// keys = [{ name: 'foo', ... }, { name: 'bar', prefix: '/', modifier: '?' }]
|
130
|
-
|
131
|
-
regexp.exec("/test");
|
132
|
-
//=> [ '/test', 'test', undefined, index: 0 ]
|
45
|
+
const fn = match("/*splat");
|
133
46
|
|
134
|
-
|
135
|
-
//=>
|
47
|
+
fn("/bar/baz");
|
48
|
+
//=> { path: '/bar/baz', params: { splat: [ 'bar', 'baz' ] } }
|
136
49
|
```
|
137
50
|
|
138
|
-
|
51
|
+
### Optional
|
139
52
|
|
140
|
-
|
53
|
+
Braces can be used to define parts of the path that are optional.
|
141
54
|
|
142
55
|
```js
|
143
|
-
const
|
144
|
-
// keys = [{ name: 'foo', prefix: '/', modifier: '*' }]
|
56
|
+
const fn = match("/users{/:id}/delete");
|
145
57
|
|
146
|
-
|
147
|
-
//=>
|
58
|
+
fn("/users/delete");
|
59
|
+
//=> { path: '/users/delete', params: {} }
|
148
60
|
|
149
|
-
|
150
|
-
//=>
|
61
|
+
fn("/users/123/delete");
|
62
|
+
//=> { path: '/users/123/delete', params: { id: '123' } }
|
151
63
|
```
|
152
64
|
|
153
|
-
|
154
|
-
|
155
|
-
Parameters can be suffixed with a plus sign (`+`) to denote a one or more parameter matches.
|
156
|
-
|
157
|
-
```js
|
158
|
-
const regexp = pathToRegexp("{/:foo}+");
|
159
|
-
// keys = [{ name: 'foo', prefix: '/', modifier: '+' }]
|
160
|
-
|
161
|
-
regexp.exec("/");
|
162
|
-
//=> null
|
65
|
+
## Match
|
163
66
|
|
164
|
-
|
165
|
-
//=> [ '/bar/baz', 'bar/baz', index: 0 ]
|
166
|
-
```
|
67
|
+
The `match` function returns a function for matching strings against a path:
|
167
68
|
|
168
|
-
|
169
|
-
|
170
|
-
|
69
|
+
- **path** String or array of strings.
|
70
|
+
- **options** _(optional)_ (Extends [pathToRegexp](#pathToRegexp) options)
|
71
|
+
- **decode** Function for decoding strings to params, or `false` to disable all processing. (default: `decodeURIComponent`)
|
171
72
|
|
172
73
|
```js
|
173
|
-
const
|
174
|
-
|
175
|
-
regexp.exec("/name");
|
176
|
-
//=> null
|
177
|
-
|
178
|
-
regexp.exec("/bar/1-2-3");
|
179
|
-
//=> [ '/name/1-2-3', '1-2-3', index: 0 ]
|
74
|
+
const fn = match("/foo/:bar");
|
180
75
|
```
|
181
76
|
|
182
|
-
|
183
|
-
|
184
|
-
A wildcard can also be used. It is roughly equivalent to `(.*)`.
|
185
|
-
|
186
|
-
```js
|
187
|
-
const regexp = pathToRegexp("/*");
|
188
|
-
// keys = [{ name: '0', pattern: '[^\\/]*', separator: '/', modifier: '*' }]
|
189
|
-
|
190
|
-
regexp.exec("/");
|
191
|
-
//=> [ '/', '', index: 0 ]
|
77
|
+
**Please note:** `path-to-regexp` is intended for ordered data (e.g. paths, hosts). It can not handle arbitrarily ordered data (e.g. query strings, URL fragments, JSON, etc).
|
192
78
|
|
193
|
-
|
194
|
-
//=> [ '/bar/baz', 'bar/baz', index: 0 ]
|
195
|
-
```
|
196
|
-
|
197
|
-
### Match
|
79
|
+
## PathToRegexp
|
198
80
|
|
199
|
-
The `
|
81
|
+
The `pathToRegexp` function returns a regular expression for matching strings against paths. It
|
200
82
|
|
201
|
-
- **path**
|
202
|
-
- **options** _(optional)_
|
203
|
-
- **
|
83
|
+
- **path** String or array of strings.
|
84
|
+
- **options** _(optional)_ (See [parse](#parse) for more options)
|
85
|
+
- **sensitive** Regexp will be case sensitive. (default: `false`)
|
86
|
+
- **end** Validate the match reaches the end of the string. (default: `true`)
|
87
|
+
- **delimiter** The default delimiter for segments, e.g. `[^/]` for `:named` parameters. (default: `'/'`)
|
88
|
+
- **trailing** Allows optional trailing delimiter to match. (default: `true`)
|
204
89
|
|
205
90
|
```js
|
206
|
-
const
|
207
|
-
|
208
|
-
fn("/user/123"); //=> { path: '/user/123', index: 0, params: { id: '123' } }
|
209
|
-
fn("/invalid"); //=> false
|
210
|
-
fn("/user/caf%C3%A9"); //=> { path: '/user/caf%C3%A9', index: 0, params: { id: 'café' } }
|
91
|
+
const { regexp, keys } = pathToRegexp("/foo/:bar");
|
211
92
|
```
|
212
93
|
|
213
|
-
|
214
|
-
|
215
|
-
### Compile ("Reverse" Path-To-RegExp)
|
94
|
+
## Compile ("Reverse" Path-To-RegExp)
|
216
95
|
|
217
96
|
The `compile` function will return a function for transforming parameters into a valid path:
|
218
97
|
|
219
98
|
- **path** A string.
|
220
|
-
- **options**
|
221
|
-
- **
|
99
|
+
- **options** (See [parse](#parse) for more options)
|
100
|
+
- **delimiter** The default delimiter for segments, e.g. `[^/]` for `:named` parameters. (default: `'/'`)
|
222
101
|
- **encode** Function for encoding input strings for output into the path, or `false` to disable entirely. (default: `encodeURIComponent`)
|
223
102
|
|
224
103
|
```js
|
@@ -227,53 +106,61 @@ const toPath = compile("/user/:id");
|
|
227
106
|
toPath({ id: "name" }); //=> "/user/name"
|
228
107
|
toPath({ id: "café" }); //=> "/user/caf%C3%A9"
|
229
108
|
|
109
|
+
const toPathRepeated = compile("/*segment");
|
110
|
+
|
111
|
+
toPathRepeated({ segment: ["foo"] }); //=> "/foo"
|
112
|
+
toPathRepeated({ segment: ["a", "b", "c"] }); //=> "/a/b/c"
|
113
|
+
|
230
114
|
// When disabling `encode`, you need to make sure inputs are encoded correctly. No arrays are accepted.
|
231
115
|
const toPathRaw = compile("/user/:id", { encode: false });
|
232
116
|
|
233
117
|
toPathRaw({ id: "%3A%2F" }); //=> "/user/%3A%2F"
|
234
|
-
|
118
|
+
```
|
235
119
|
|
236
|
-
|
120
|
+
## Stringify
|
237
121
|
|
238
|
-
|
239
|
-
|
122
|
+
Transform `TokenData` (a sequence of tokens) back into a Path-to-RegExp string.
|
123
|
+
|
124
|
+
- **data** A `TokenData` instance
|
240
125
|
|
241
|
-
|
126
|
+
```js
|
127
|
+
const data = new TokenData([
|
128
|
+
{ type: "text", value: "/" },
|
129
|
+
{ type: "param", name: "foo" },
|
130
|
+
]);
|
242
131
|
|
243
|
-
|
132
|
+
const path = stringify(data); //=> "/:foo"
|
244
133
|
```
|
245
134
|
|
246
135
|
## Developers
|
247
136
|
|
248
|
-
- If you are rewriting paths with match and
|
249
|
-
- To ensure matches work on paths containing characters usually encoded, consider using [encodeurl](https://github.com/pillarjs/encodeurl) for `encodePath`.
|
250
|
-
- If matches are intended to be exact, you need to set `loose: false`, `trailing: false`, and `sensitive: true`.
|
251
|
-
- Enable `strict: true` to detect ReDOS issues.
|
137
|
+
- If you are rewriting paths with match and compile, consider using `encode: false` and `decode: false` to keep raw paths passed around.
|
138
|
+
- To ensure matches work on paths containing characters usually encoded, such as emoji, consider using [encodeurl](https://github.com/pillarjs/encodeurl) for `encodePath`.
|
252
139
|
|
253
140
|
### Parse
|
254
141
|
|
255
|
-
|
142
|
+
The `parse` function accepts a string and returns `TokenData`, the set of tokens and other metadata parsed from the input string. `TokenData` is can used with `match` and `compile`.
|
256
143
|
|
257
|
-
|
144
|
+
- **path** A string.
|
145
|
+
- **options** _(optional)_
|
146
|
+
- **encodePath** A function for encoding input strings. (default: `x => x`, recommended: [`encodeurl`](https://github.com/pillarjs/encodeurl))
|
258
147
|
|
259
|
-
|
148
|
+
### Tokens
|
260
149
|
|
261
|
-
|
262
|
-
- `prefix` _(optional)_ The prefix string for the segment (e.g. `"/"`)
|
263
|
-
- `suffix` _(optional)_ The suffix string for the segment (e.g. `""`)
|
264
|
-
- `pattern` _(optional)_ The pattern defined to match this token
|
265
|
-
- `modifier` _(optional)_ The modifier character used for the segment (e.g. `?`)
|
266
|
-
- `separator` _(optional)_ The string used to separate repeated parameters
|
150
|
+
`TokenData` is a sequence of tokens, currently of types `text`, `parameter`, `wildcard`, or `group`.
|
267
151
|
|
268
152
|
### Custom path
|
269
153
|
|
270
|
-
In some applications, you may not be able to use the `path-to-regexp` syntax
|
154
|
+
In some applications, you may not be able to use the `path-to-regexp` syntax, but still want to use this library for `match` and `compile`. For example:
|
271
155
|
|
272
156
|
```js
|
273
157
|
import { TokenData, match } from "path-to-regexp";
|
274
158
|
|
275
|
-
const tokens = [
|
276
|
-
|
159
|
+
const tokens = [
|
160
|
+
{ type: "text", value: "/" },
|
161
|
+
{ type: "parameter", name: "foo" },
|
162
|
+
];
|
163
|
+
const path = new TokenData(tokens);
|
277
164
|
const fn = match(path);
|
278
165
|
|
279
166
|
fn("/test"); //=> { path: '/test', index: 0, params: { foo: 'test' } }
|
@@ -283,31 +170,34 @@ fn("/test"); //=> { path: '/test', index: 0, params: { foo: 'test' } }
|
|
283
170
|
|
284
171
|
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.
|
285
172
|
|
286
|
-
### Unexpected
|
173
|
+
### Unexpected `?` or `+`
|
174
|
+
|
175
|
+
In past releases, `?`, `*`, and `+` were used to denote optional or repeating parameters. As an alternative, try these:
|
176
|
+
|
177
|
+
- For optional (`?`), use an empty segment in a group such as `/:file{.:ext}`.
|
178
|
+
- For repeating (`+`), only wildcard matching is supported, such as `/*path`.
|
179
|
+
- For optional repeating (`*`), use a group and a wildcard parameter such as `/files{/*path}`.
|
287
180
|
|
288
|
-
|
181
|
+
### Unexpected `(`, `)`, `[`, `]`, etc.
|
289
182
|
|
290
|
-
-
|
291
|
-
- `.:key?` → `{.:key}?` or `.:key*` → `{.:key}*` or `.:key+` → `{.:key}+`
|
292
|
-
- `:key?` → `{:key}?` or `:key*` → `{:key}*` or `:key+` → `{:key}+`
|
183
|
+
Previous versions of Path-to-RegExp used these for RegExp features. This version no longer supports them so they've been reserved to avoid ambiguity. To use these characters literally, escape them with a backslash, e.g. `"\\("`.
|
293
184
|
|
294
|
-
###
|
185
|
+
### Missing parameter name
|
295
186
|
|
296
|
-
|
187
|
+
Parameter names, the part after `:` or `*`, must be a valid JavaScript identifier. For example, it cannot start with a number or contain a dash. If you want a parameter name that uses these characters you can wrap the name in quotes, e.g. `:"my-name"`.
|
297
188
|
|
298
|
-
###
|
189
|
+
### Unterminated quote
|
299
190
|
|
300
|
-
|
191
|
+
Parameter names can be wrapped in double quote characters, and this error means you forgot to close the quote character.
|
301
192
|
|
302
193
|
### Express <= 4.x
|
303
194
|
|
304
195
|
Path-To-RegExp breaks compatibility with Express <= `4.x` in the following ways:
|
305
196
|
|
306
|
-
-
|
307
|
-
|
308
|
-
- The `?` optional character must be used after `{}`.
|
197
|
+
- Regexp characters can no longer be provided.
|
198
|
+
- The optional character `?` is no longer supported, use braces instead: `/:file{.:ext}`.
|
309
199
|
- Some characters have new meaning or have been reserved (`{}?*+@!;`).
|
310
|
-
- The parameter name now supports all
|
200
|
+
- The parameter name now supports all JavaScript identifier characters, previously it was only `[a-z0-9]`.
|
311
201
|
|
312
202
|
## License
|
313
203
|
|
package/dist/index.d.ts
CHANGED
@@ -7,40 +7,28 @@ export type Encode = (value: string) => string;
|
|
7
7
|
*/
|
8
8
|
export type Decode = (value: string) => string;
|
9
9
|
export interface ParseOptions {
|
10
|
-
/**
|
11
|
-
* The default delimiter for segments. (default: `'/'`)
|
12
|
-
*/
|
13
|
-
delimiter?: string;
|
14
10
|
/**
|
15
11
|
* A function for encoding input strings.
|
16
12
|
*/
|
17
13
|
encodePath?: Encode;
|
18
14
|
}
|
19
|
-
export interface PathToRegexpOptions
|
15
|
+
export interface PathToRegexpOptions {
|
20
16
|
/**
|
21
|
-
*
|
17
|
+
* Matches the path completely without trailing characters. (default: `true`)
|
22
18
|
*/
|
23
|
-
|
24
|
-
/**
|
25
|
-
* Allow the delimiter to be arbitrarily repeated. (default: `true`)
|
26
|
-
*/
|
27
|
-
loose?: boolean;
|
28
|
-
/**
|
29
|
-
* Verify patterns are valid and safe to use. (default: `false`)
|
30
|
-
*/
|
31
|
-
strict?: boolean;
|
19
|
+
end?: boolean;
|
32
20
|
/**
|
33
|
-
*
|
21
|
+
* Allows optional trailing delimiter to match. (default: `true`)
|
34
22
|
*/
|
35
|
-
|
23
|
+
trailing?: boolean;
|
36
24
|
/**
|
37
|
-
* Match
|
25
|
+
* Match will be case sensitive. (default: `false`)
|
38
26
|
*/
|
39
|
-
|
27
|
+
sensitive?: boolean;
|
40
28
|
/**
|
41
|
-
*
|
29
|
+
* The default delimiter for segments. (default: `'/'`)
|
42
30
|
*/
|
43
|
-
|
31
|
+
delimiter?: string;
|
44
32
|
}
|
45
33
|
export interface MatchOptions extends PathToRegexpOptions {
|
46
34
|
/**
|
@@ -48,35 +36,62 @@ export interface MatchOptions extends PathToRegexpOptions {
|
|
48
36
|
*/
|
49
37
|
decode?: Decode | false;
|
50
38
|
}
|
51
|
-
export interface CompileOptions
|
52
|
-
/**
|
53
|
-
* Regexp will be case sensitive. (default: `false`)
|
54
|
-
*/
|
55
|
-
sensitive?: boolean;
|
56
|
-
/**
|
57
|
-
* Allow the delimiter to be arbitrarily repeated. (default: `true`)
|
58
|
-
*/
|
59
|
-
loose?: boolean;
|
60
|
-
/**
|
61
|
-
* Verify patterns are valid and safe to use. (default: `false`)
|
62
|
-
*/
|
63
|
-
strict?: boolean;
|
64
|
-
/**
|
65
|
-
* Verifies the function is producing a valid path. (default: `true`)
|
66
|
-
*/
|
67
|
-
validate?: boolean;
|
39
|
+
export interface CompileOptions {
|
68
40
|
/**
|
69
41
|
* Function for encoding input strings for output into the path, or `false` to disable entirely. (default: `encodeURIComponent`)
|
70
42
|
*/
|
71
43
|
encode?: Encode | false;
|
44
|
+
/**
|
45
|
+
* The default delimiter for segments. (default: `'/'`)
|
46
|
+
*/
|
47
|
+
delimiter?: string;
|
72
48
|
}
|
73
49
|
/**
|
74
|
-
*
|
50
|
+
* Plain text.
|
51
|
+
*/
|
52
|
+
export interface Text {
|
53
|
+
type: "text";
|
54
|
+
value: string;
|
55
|
+
}
|
56
|
+
/**
|
57
|
+
* A parameter designed to match arbitrary text within a segment.
|
58
|
+
*/
|
59
|
+
export interface Parameter {
|
60
|
+
type: "param";
|
61
|
+
name: string;
|
62
|
+
}
|
63
|
+
/**
|
64
|
+
* A wildcard parameter designed to match multiple segments.
|
65
|
+
*/
|
66
|
+
export interface Wildcard {
|
67
|
+
type: "wildcard";
|
68
|
+
name: string;
|
69
|
+
}
|
70
|
+
/**
|
71
|
+
* A set of possible tokens to expand when matching.
|
72
|
+
*/
|
73
|
+
export interface Group {
|
74
|
+
type: "group";
|
75
|
+
tokens: Token[];
|
76
|
+
}
|
77
|
+
/**
|
78
|
+
* A token that corresponds with a regexp capture.
|
79
|
+
*/
|
80
|
+
export type Key = Parameter | Wildcard;
|
81
|
+
/**
|
82
|
+
* A sequence of `path-to-regexp` keys that match capturing groups.
|
83
|
+
*/
|
84
|
+
export type Keys = Array<Key>;
|
85
|
+
/**
|
86
|
+
* A sequence of path match characters.
|
87
|
+
*/
|
88
|
+
export type Token = Text | Parameter | Wildcard | Group;
|
89
|
+
/**
|
90
|
+
* Tokenized path instance.
|
75
91
|
*/
|
76
92
|
export declare class TokenData {
|
77
93
|
readonly tokens: Token[];
|
78
|
-
|
79
|
-
constructor(tokens: Token[], delimiter: string);
|
94
|
+
constructor(tokens: Token[]);
|
80
95
|
}
|
81
96
|
/**
|
82
97
|
* Parse a string for the raw tokens.
|
@@ -85,7 +100,7 @@ export declare function parse(str: string, options?: ParseOptions): TokenData;
|
|
85
100
|
/**
|
86
101
|
* Compile a string to a template function for the path.
|
87
102
|
*/
|
88
|
-
export declare function compile<P extends ParamData = ParamData>(path: Path, options?: CompileOptions):
|
103
|
+
export declare function compile<P extends ParamData = ParamData>(path: Path, options?: CompileOptions & ParseOptions): (data?: P) => string;
|
89
104
|
export type ParamData = Partial<Record<string, string | string[]>>;
|
90
105
|
export type PathFunction<P extends ParamData> = (data?: P) => string;
|
91
106
|
/**
|
@@ -93,7 +108,6 @@ export type PathFunction<P extends ParamData> = (data?: P) => string;
|
|
93
108
|
*/
|
94
109
|
export interface MatchResult<P extends ParamData> {
|
95
110
|
path: string;
|
96
|
-
index: number;
|
97
111
|
params: P;
|
98
112
|
}
|
99
113
|
/**
|
@@ -105,35 +119,18 @@ export type Match<P extends ParamData> = false | MatchResult<P>;
|
|
105
119
|
*/
|
106
120
|
export type MatchFunction<P extends ParamData> = (path: string) => Match<P>;
|
107
121
|
/**
|
108
|
-
*
|
109
|
-
*/
|
110
|
-
export declare function match<P extends ParamData>(path: Path | Path[], options?: MatchOptions): MatchFunction<P>;
|
111
|
-
/**
|
112
|
-
* A key is a capture group in the regex.
|
113
|
-
*/
|
114
|
-
export interface Key {
|
115
|
-
name: string;
|
116
|
-
prefix?: string;
|
117
|
-
suffix?: string;
|
118
|
-
pattern?: string;
|
119
|
-
modifier?: string;
|
120
|
-
separator?: string;
|
121
|
-
}
|
122
|
-
/**
|
123
|
-
* A token is a string (nothing special) or key metadata (capture group).
|
124
|
-
*/
|
125
|
-
export type Token = string | Key;
|
126
|
-
/**
|
127
|
-
* Repeated and simple input types.
|
122
|
+
* Supported path types.
|
128
123
|
*/
|
129
124
|
export type Path = string | TokenData;
|
130
125
|
/**
|
131
|
-
*
|
132
|
-
*
|
133
|
-
* An empty array can be passed in for the keys, which will hold the
|
134
|
-
* placeholder key descriptions. For example, using `/user/:id`, `keys` will
|
135
|
-
* contain `[{ name: 'id', delimiter: '/', optional: false, repeat: false }]`.
|
126
|
+
* Transform a path into a match function.
|
136
127
|
*/
|
137
|
-
export declare function
|
138
|
-
|
128
|
+
export declare function match<P extends ParamData>(path: Path | Path[], options?: MatchOptions & ParseOptions): MatchFunction<P>;
|
129
|
+
export declare function pathToRegexp(path: Path | Path[], options?: PathToRegexpOptions & ParseOptions): {
|
130
|
+
regexp: RegExp;
|
131
|
+
keys: Keys;
|
139
132
|
};
|
133
|
+
/**
|
134
|
+
* Stringify token data into a path string.
|
135
|
+
*/
|
136
|
+
export declare function stringify(data: TokenData): string;
|