path-to-regexp 4.0.5 → 6.2.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/History.md +35 -33
- package/Readme.md +129 -67
- package/dist/index.d.ts +15 -24
- package/dist/index.js +189 -153
- package/dist/index.js.map +1 -1
- package/dist.es2015/index.js +188 -152
- package/dist.es2015/index.js.map +1 -1
- package/package.json +16 -16
- package/dist/index.spec.d.ts +0 -1
- package/dist/index.spec.js +0 -2764
- package/dist/index.spec.js.map +0 -1
- package/dist.es2015/index.spec.js +0 -2762
- package/dist.es2015/index.spec.js.map +0 -1
package/History.md
CHANGED
@@ -1,30 +1,32 @@
|
|
1
|
-
#
|
1
|
+
# Moved to [GitHub Releases](https://github.com/pillarjs/path-to-regexp/releases)
|
2
|
+
|
3
|
+
## 3.0.0 / 2019-01-13
|
2
4
|
|
3
5
|
- Always use prefix character as delimiter token, allowing any character to be a delimiter (e.g. `/:att1-:att2-:att3-:att4-:att5`)
|
4
6
|
- Remove `partial` support, prefer escaping the prefix delimiter explicitly (e.g. `\\/(apple-)?icon-:res(\\d+).png`)
|
5
7
|
|
6
|
-
|
8
|
+
## 2.4.0 / 2018-08-26
|
7
9
|
|
8
10
|
- Support `start` option to disable anchoring from beginning of the string
|
9
11
|
|
10
|
-
|
12
|
+
## 2.3.0 / 2018-08-20
|
11
13
|
|
12
14
|
- Use `delimiter` when processing repeated matching groups (e.g. `foo/bar` has no prefix, but has a delimiter)
|
13
15
|
|
14
|
-
|
16
|
+
## 2.2.1 / 2018-04-24
|
15
17
|
|
16
18
|
- Allow empty string with `end: false` to match both relative and absolute paths
|
17
19
|
|
18
|
-
|
20
|
+
## 2.2.0 / 2018-03-06
|
19
21
|
|
20
22
|
- Pass `token` as second argument to `encode` option (e.g. `encode(value, token)`)
|
21
23
|
|
22
|
-
|
24
|
+
## 2.1.0 / 2017-10-20
|
23
25
|
|
24
26
|
- Handle non-ending paths where the final character is a delimiter
|
25
27
|
- E.g. `/foo/` before required either `/foo/` or `/foo//` to match in non-ending mode
|
26
28
|
|
27
|
-
|
29
|
+
## 2.0.0 / 2017-08-23
|
28
30
|
|
29
31
|
- New option! Ability to set `endsWith` to match paths like `/test?query=string` up to the query string
|
30
32
|
- New option! Set `delimiters` for specific characters to be treated as parameter prefixes (e.g. `/:test`)
|
@@ -35,38 +37,38 @@
|
|
35
37
|
- Remove asterisk functionality (it's a real pain to properly encode)
|
36
38
|
- Change `tokensToFunction` (e.g. `compile`) to accept an `encode` function for pretty encoding (e.g. pass your own implementation)
|
37
39
|
|
38
|
-
|
40
|
+
## 1.7.0 / 2016-11-08
|
39
41
|
|
40
42
|
- Allow a `delimiter` option to be passed in with `tokensToRegExp` which will be used for "non-ending" token match situations
|
41
43
|
|
42
|
-
|
44
|
+
## 1.6.0 / 2016-10-03
|
43
45
|
|
44
46
|
- Populate `RegExp.keys` when using the `tokensToRegExp` method (making it consistent with the main export)
|
45
47
|
- Allow a `delimiter` option to be passed in with `parse`
|
46
48
|
- Updated TypeScript definition with `Keys` and `Options` updated
|
47
49
|
|
48
|
-
|
50
|
+
## 1.5.3 / 2016-06-15
|
49
51
|
|
50
52
|
- Add `\\` to the ignore character group to avoid backtracking on mismatched parens
|
51
53
|
|
52
|
-
|
54
|
+
## 1.5.2 / 2016-06-15
|
53
55
|
|
54
56
|
- Escape `\\` in string segments of regexp
|
55
57
|
|
56
|
-
|
58
|
+
## 1.5.1 / 2016-06-08
|
57
59
|
|
58
60
|
- Add `index.d.ts` to NPM package
|
59
61
|
|
60
|
-
|
62
|
+
## 1.5.0 / 2016-05-20
|
61
63
|
|
62
64
|
- Handle partial token segments (better)
|
63
65
|
- Allow compile to handle asterisk token segments
|
64
66
|
|
65
|
-
|
67
|
+
## 1.4.0 / 2016-05-18
|
66
68
|
|
67
69
|
- Handle RegExp unions in path matching groups
|
68
70
|
|
69
|
-
|
71
|
+
## 1.3.0 / 2016-05-08
|
70
72
|
|
71
73
|
- Clarify README language and named parameter token support
|
72
74
|
- Support advanced Closure Compiler with type annotations
|
@@ -74,20 +76,20 @@
|
|
74
76
|
- Add TypeScript definition to project
|
75
77
|
- Improved prefix handling with non-complete segment parameters (E.g. `/:foo?-bar`)
|
76
78
|
|
77
|
-
|
79
|
+
## 1.2.1 / 2015-08-17
|
78
80
|
|
79
81
|
- Encode values before validation with path compilation function
|
80
82
|
- More examples of using compilation in README
|
81
83
|
|
82
|
-
|
84
|
+
## 1.2.0 / 2015-05-20
|
83
85
|
|
84
86
|
- Add support for matching an asterisk (`*`) as an unnamed match everything group (`(.*)`)
|
85
87
|
|
86
|
-
|
88
|
+
## 1.1.1 / 2015-05-11
|
87
89
|
|
88
90
|
- Expose methods for working with path tokens
|
89
91
|
|
90
|
-
|
92
|
+
## 1.1.0 / 2015-05-09
|
91
93
|
|
92
94
|
- Expose the parser implementation to consumers
|
93
95
|
- Implement a compiler function to generate valid strings
|
@@ -95,50 +97,50 @@
|
|
95
97
|
- Use chai in tests
|
96
98
|
- Add .editorconfig
|
97
99
|
|
98
|
-
|
100
|
+
## 1.0.3 / 2015-01-17
|
99
101
|
|
100
102
|
- Optimised function runtime
|
101
103
|
- Added `files` to `package.json`
|
102
104
|
|
103
|
-
|
105
|
+
## 1.0.2 / 2014-12-17
|
104
106
|
|
105
107
|
- Use `Array.isArray` shim
|
106
108
|
- Remove ES5 incompatible code
|
107
109
|
- Fixed repository path
|
108
110
|
- Added new readme badges
|
109
111
|
|
110
|
-
|
112
|
+
## 1.0.1 / 2014-08-27
|
111
113
|
|
112
114
|
- Ensure installation works correctly on 0.8
|
113
115
|
|
114
|
-
|
116
|
+
## 1.0.0 / 2014-08-17
|
115
117
|
|
116
118
|
- No more API changes
|
117
119
|
|
118
|
-
|
120
|
+
## 0.2.5 / 2014-08-07
|
119
121
|
|
120
122
|
- Allow keys parameter to be omitted
|
121
123
|
|
122
|
-
|
124
|
+
## 0.2.4 / 2014-08-02
|
123
125
|
|
124
126
|
- Code coverage badge
|
125
127
|
- Updated readme
|
126
128
|
- Attach keys to the generated regexp
|
127
129
|
|
128
|
-
|
130
|
+
## 0.2.3 / 2014-07-09
|
129
131
|
|
130
132
|
- Add MIT license
|
131
133
|
|
132
|
-
|
134
|
+
## 0.2.2 / 2014-07-06
|
133
135
|
|
134
136
|
- A passed in trailing slash in non-strict mode will become optional
|
135
137
|
- In non-end mode, the optional trailing slash will only match at the end
|
136
138
|
|
137
|
-
|
139
|
+
## 0.2.1 / 2014-06-11
|
138
140
|
|
139
141
|
- Fixed a major capturing group regexp regression
|
140
142
|
|
141
|
-
|
143
|
+
## 0.2.0 / 2014-06-09
|
142
144
|
|
143
145
|
- Improved support for arrays
|
144
146
|
- Improved support for regexps
|
@@ -150,20 +152,20 @@
|
|
150
152
|
- Updated readme
|
151
153
|
- Provide delimiter information with keys array
|
152
154
|
|
153
|
-
|
155
|
+
## 0.1.2 / 2014-03-10
|
154
156
|
|
155
157
|
- Move testing dependencies to `devDependencies`
|
156
158
|
|
157
|
-
|
159
|
+
## 0.1.1 / 2014-03-10
|
158
160
|
|
159
161
|
- Match entire substring with `options.end`
|
160
162
|
- Properly handle ending and non-ending matches
|
161
163
|
|
162
|
-
|
164
|
+
## 0.1.0 / 2014-03-06
|
163
165
|
|
164
166
|
- Add `options.end`
|
165
167
|
|
166
|
-
|
168
|
+
## 0.0.2 / 2013-02-10
|
167
169
|
|
168
170
|
- Update to match current express
|
169
171
|
- Add .license property to component.json
|
package/Readme.md
CHANGED
@@ -30,29 +30,30 @@ const { pathToRegexp, match, parse, compile } = require("path-to-regexp");
|
|
30
30
|
- **keys** An array to populate with keys found in the path.
|
31
31
|
- **options**
|
32
32
|
- **sensitive** When `true` the regexp will be case sensitive. (default: `false`)
|
33
|
-
- **strict** When `true` the regexp
|
33
|
+
- **strict** When `true` the regexp won't allow an optional trailing delimiter to match. (default: `false`)
|
34
34
|
- **end** When `true` the regexp will match to the end of the string. (default: `true`)
|
35
35
|
- **start** When `true` the regexp will match from the beginning of the string. (default: `true`)
|
36
|
-
- **delimiter** The default delimiter for segments. (default: `'
|
36
|
+
- **delimiter** The default delimiter for segments, e.g. `[^/#?]` for `:named` patterns. (default: `'/#?'`)
|
37
37
|
- **endsWith** Optional character, or list of characters, to treat as "end" characters.
|
38
|
-
- **
|
38
|
+
- **encode** A function to encode strings before inserting into `RegExp`. (default: `x => x`)
|
39
|
+
- **prefixes** List of characters to automatically consider prefixes when parsing. (default: `./`)
|
39
40
|
|
40
41
|
```javascript
|
41
42
|
const keys = [];
|
42
43
|
const regexp = pathToRegexp("/foo/:bar", keys);
|
43
|
-
// regexp = /^\/foo
|
44
|
-
// keys = [{ name: 'bar', prefix: '/',
|
44
|
+
// regexp = /^\/foo(?:\/([^\/#\?]+?))[\/#\?]?$/i
|
45
|
+
// keys = [{ name: 'bar', prefix: '/', suffix: '', pattern: '[^\\/#\\?]+?', modifier: '' }]
|
45
46
|
```
|
46
47
|
|
47
|
-
**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).
|
48
|
+
**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). When using paths that contain query strings, you need to escape the question mark (`?`) to ensure it does not flag the parameter as [optional](#optional).
|
48
49
|
|
49
50
|
### Parameters
|
50
51
|
|
51
|
-
The path argument is used to define parameters and populate
|
52
|
+
The path argument is used to define parameters and populate keys.
|
52
53
|
|
53
54
|
#### Named Parameters
|
54
55
|
|
55
|
-
Named parameters are defined by prefixing a colon to the parameter name (`:foo`).
|
56
|
+
Named parameters are defined by prefixing a colon to the parameter name (`:foo`).
|
56
57
|
|
57
58
|
```js
|
58
59
|
const regexp = pathToRegexp("/:foo/:bar");
|
@@ -64,7 +65,61 @@ regexp.exec("/test/route");
|
|
64
65
|
|
65
66
|
**Please note:** Parameter names must use "word characters" (`[A-Za-z0-9_]`).
|
66
67
|
|
67
|
-
|
68
|
+
##### Custom Matching Parameters
|
69
|
+
|
70
|
+
Parameters can have a custom regexp, which overrides the default match (`[^/]+`). For example, you can match digits or names in a path:
|
71
|
+
|
72
|
+
```js
|
73
|
+
const regexpNumbers = pathToRegexp("/icon-:foo(\\d+).png");
|
74
|
+
// keys = [{ name: 'foo', ... }]
|
75
|
+
|
76
|
+
regexpNumbers.exec("/icon-123.png");
|
77
|
+
//=> ['/icon-123.png', '123']
|
78
|
+
|
79
|
+
regexpNumbers.exec("/icon-abc.png");
|
80
|
+
//=> null
|
81
|
+
|
82
|
+
const regexpWord = pathToRegexp("/(user|u)");
|
83
|
+
// keys = [{ name: 0, ... }]
|
84
|
+
|
85
|
+
regexpWord.exec("/u");
|
86
|
+
//=> ['/u', 'u']
|
87
|
+
|
88
|
+
regexpWord.exec("/users");
|
89
|
+
//=> null
|
90
|
+
```
|
91
|
+
|
92
|
+
**Tip:** Backslashes need to be escaped with another backslash in JavaScript strings.
|
93
|
+
|
94
|
+
##### Custom Prefix and Suffix
|
95
|
+
|
96
|
+
Parameters can be wrapped in `{}` to create custom prefixes or suffixes for your segment:
|
97
|
+
|
98
|
+
```js
|
99
|
+
const regexp = pathToRegexp("/:attr1?{-:attr2}?{-:attr3}?");
|
100
|
+
|
101
|
+
regexp.exec("/test");
|
102
|
+
// => ['/test', 'test', undefined, undefined]
|
103
|
+
|
104
|
+
regexp.exec("/test-test");
|
105
|
+
// => ['/test', 'test', 'test', undefined]
|
106
|
+
```
|
107
|
+
|
108
|
+
#### Unnamed Parameters
|
109
|
+
|
110
|
+
It is possible to write an unnamed parameter that only consists of a regexp. It works the same the named parameter, except it will be numerically indexed:
|
111
|
+
|
112
|
+
```js
|
113
|
+
const regexp = pathToRegexp("/:foo/(.*)");
|
114
|
+
// keys = [{ name: 'foo', ... }, { name: 0, ... }]
|
115
|
+
|
116
|
+
regexp.exec("/test/route");
|
117
|
+
//=> [ '/test/route', 'test', 'route', index: 0, input: '/test/route', groups: undefined ]
|
118
|
+
```
|
119
|
+
|
120
|
+
#### Modifiers
|
121
|
+
|
122
|
+
Modifiers must be placed after the parameter (e.g. `/:foo?`, `/(test)?`, `/:foo(test)?`, or `{-:foo(test)}?`).
|
68
123
|
|
69
124
|
##### Optional
|
70
125
|
|
@@ -72,7 +127,7 @@ Parameters can be suffixed with a question mark (`?`) to make the parameter opti
|
|
72
127
|
|
73
128
|
```js
|
74
129
|
const regexp = pathToRegexp("/:foo/:bar?");
|
75
|
-
// keys = [{ name: 'foo', ... }, { name: 'bar',
|
130
|
+
// keys = [{ name: 'foo', ... }, { name: 'bar', prefix: '/', modifier: '?' }]
|
76
131
|
|
77
132
|
regexp.exec("/test");
|
78
133
|
//=> [ '/test', 'test', undefined, index: 0, input: '/test', groups: undefined ]
|
@@ -83,13 +138,26 @@ regexp.exec("/test/route");
|
|
83
138
|
|
84
139
|
**Tip:** The prefix is also optional, escape the prefix `\/` to make it required.
|
85
140
|
|
141
|
+
When dealing with query strings, escape the question mark (`?`) so it doesn't mark the parameter as optional. Handling unordered data is outside the scope of this library.
|
142
|
+
|
143
|
+
```js
|
144
|
+
const regexp = pathToRegexp("/search/:tableName\\?useIndex=true&term=amazing");
|
145
|
+
|
146
|
+
regexp.exec("/search/people?useIndex=true&term=amazing");
|
147
|
+
//=> [ '/search/people?useIndex=true&term=amazing', 'people', index: 0, input: '/search/people?useIndex=true&term=amazing', groups: undefined ]
|
148
|
+
|
149
|
+
// This library does not handle query strings in different orders
|
150
|
+
regexp.exec("/search/people?term=amazing&useIndex=true");
|
151
|
+
//=> null
|
152
|
+
```
|
153
|
+
|
86
154
|
##### Zero or more
|
87
155
|
|
88
|
-
Parameters can be suffixed with an asterisk (`*`) to denote a zero or more parameter matches.
|
156
|
+
Parameters can be suffixed with an asterisk (`*`) to denote a zero or more parameter matches.
|
89
157
|
|
90
158
|
```js
|
91
159
|
const regexp = pathToRegexp("/:foo*");
|
92
|
-
// keys = [{ name: 'foo',
|
160
|
+
// keys = [{ name: 'foo', prefix: '/', modifier: '*' }]
|
93
161
|
|
94
162
|
regexp.exec("/");
|
95
163
|
//=> [ '/', undefined, index: 0, input: '/', groups: undefined ]
|
@@ -100,11 +168,11 @@ regexp.exec("/bar/baz");
|
|
100
168
|
|
101
169
|
##### One or more
|
102
170
|
|
103
|
-
Parameters can be suffixed with a plus sign (`+`) to denote a one or more parameter matches.
|
171
|
+
Parameters can be suffixed with a plus sign (`+`) to denote a one or more parameter matches.
|
104
172
|
|
105
173
|
```js
|
106
174
|
const regexp = pathToRegexp("/:foo+");
|
107
|
-
// keys = [{ name: 'foo',
|
175
|
+
// keys = [{ name: 'foo', prefix: '/', modifier: '+' }]
|
108
176
|
|
109
177
|
regexp.exec("/");
|
110
178
|
//=> null
|
@@ -113,71 +181,60 @@ regexp.exec("/bar/baz");
|
|
113
181
|
//=> [ '/bar/baz','bar/baz', index: 0, input: '/bar/baz', groups: undefined ]
|
114
182
|
```
|
115
183
|
|
116
|
-
|
184
|
+
### Match
|
117
185
|
|
118
|
-
|
186
|
+
The `match` function will return a function for transforming paths into parameters:
|
119
187
|
|
120
188
|
```js
|
121
|
-
|
122
|
-
|
189
|
+
// Make sure you consistently `decode` segments.
|
190
|
+
const match = match("/user/:id", { decode: decodeURIComponent });
|
123
191
|
|
124
|
-
|
125
|
-
|
192
|
+
match("/user/123"); //=> { path: '/user/123', index: 0, params: { id: '123' } }
|
193
|
+
match("/invalid"); //=> false
|
194
|
+
match("/user/caf%C3%A9"); //=> { path: '/user/caf%C3%A9', index: 0, params: { id: 'café' } }
|
126
195
|
```
|
127
196
|
|
128
|
-
####
|
197
|
+
#### Process Pathname
|
129
198
|
|
130
|
-
|
199
|
+
You should make sure variations of the same path match the expected `path`. Here's one possible solution using `encode`:
|
131
200
|
|
132
201
|
```js
|
133
|
-
const
|
134
|
-
// keys = [{ name: 'foo', ... }]
|
202
|
+
const match = match("/café", { encode: encodeURI, decode: decodeURIComponent });
|
135
203
|
|
136
|
-
|
137
|
-
//=> ['/icon-123.png', '123']
|
138
|
-
|
139
|
-
regexpNumbers.exec("/icon-abc.png");
|
140
|
-
//=> null
|
141
|
-
|
142
|
-
const regexpWord = pathToRegexp("/(user|u)");
|
143
|
-
// keys = [{ name: 0, ... }]
|
144
|
-
|
145
|
-
regexpWord.exec("/u");
|
146
|
-
//=> ['/u', 'u']
|
147
|
-
|
148
|
-
regexpWord.exec("/users");
|
149
|
-
//=> null
|
204
|
+
match("/user/caf%C3%A9"); //=> { path: '/user/caf%C3%A9', index: 0, params: { id: 'café' } }
|
150
205
|
```
|
151
206
|
|
152
|
-
**
|
153
|
-
|
154
|
-
### Match
|
155
|
-
|
156
|
-
The `match` function will return a function for transforming paths into parameters:
|
157
|
-
|
158
|
-
```js
|
159
|
-
const match = match("/user/:id");
|
207
|
+
**Note:** [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) automatically encodes pathnames for you.
|
160
208
|
|
161
|
-
|
162
|
-
match("/invalid"); //=> false
|
163
|
-
```
|
209
|
+
##### Alternative Using Normalize
|
164
210
|
|
165
|
-
|
166
|
-
|
167
|
-
The `normalizePathname` function will return a normalized string for matching with `pathToRegexp`:
|
211
|
+
Sometimes you won't have an already normalized pathname. You can normalize it yourself before processing:
|
168
212
|
|
169
213
|
```js
|
214
|
+
/**
|
215
|
+
* Normalize a pathname for matching, replaces multiple slashes with a single
|
216
|
+
* slash and normalizes unicode characters to "NFC". When using this method,
|
217
|
+
* `decode` should be an identity function so you don't decode strings twice.
|
218
|
+
*/
|
219
|
+
function normalizePathname(pathname: string) {
|
220
|
+
return (
|
221
|
+
decodeURI(pathname)
|
222
|
+
// Replaces repeated slashes in the URL.
|
223
|
+
.replace(/\/+/g, "/")
|
224
|
+
// Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize
|
225
|
+
// Note: Missing native IE support, may want to skip this step.
|
226
|
+
.normalize()
|
227
|
+
);
|
228
|
+
}
|
229
|
+
|
230
|
+
// Two possible ways of writing `/café`:
|
170
231
|
const re = pathToRegexp("/caf\u00E9");
|
171
|
-
const input = encodeURI("/
|
232
|
+
const input = encodeURI("/cafe\u0301");
|
172
233
|
|
173
234
|
re.test(input); //=> false
|
174
235
|
re.test(normalizePathname(input)); //=> true
|
175
236
|
```
|
176
237
|
|
177
|
-
**Note:** It may be preferable to implement something in your own library that normalizes the pathname for matching. E.g. [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) automatically URI encodes paths for you, which would result in a consistent match.
|
178
|
-
|
179
|
-
**Tip:** Consider using [`String.prototype.normalize`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize) to resolve unicode variants of the same string.
|
180
|
-
|
181
238
|
### Parse
|
182
239
|
|
183
240
|
The `parse` function will return a list of strings and keys from a path string:
|
@@ -189,10 +246,10 @@ console.log(tokens[0]);
|
|
189
246
|
//=> "/route"
|
190
247
|
|
191
248
|
console.log(tokens[1]);
|
192
|
-
//=> { name: 'foo', prefix: '/',
|
249
|
+
//=> { name: 'foo', prefix: '/', suffix: '', pattern: '[^\\/#\\?]+?', modifier: '' }
|
193
250
|
|
194
251
|
console.log(tokens[2]);
|
195
|
-
//=> { name: 0, prefix: '/',
|
252
|
+
//=> { name: 0, prefix: '/', suffix: '', pattern: '.*', modifier: '' }
|
196
253
|
```
|
197
254
|
|
198
255
|
**Note:** This method only works with strings.
|
@@ -202,14 +259,20 @@ console.log(tokens[2]);
|
|
202
259
|
The `compile` function will return a function for transforming parameters into a valid path:
|
203
260
|
|
204
261
|
```js
|
205
|
-
|
262
|
+
// Make sure you encode your path segments consistently.
|
263
|
+
const toPath = compile("/user/:id", { encode: encodeURIComponent });
|
206
264
|
|
207
265
|
toPath({ id: 123 }); //=> "/user/123"
|
208
266
|
toPath({ id: "café" }); //=> "/user/caf%C3%A9"
|
209
267
|
toPath({ id: "/" }); //=> "/user/%2F"
|
210
268
|
|
211
269
|
toPath({ id: ":/" }); //=> "/user/%3A%2F"
|
212
|
-
|
270
|
+
|
271
|
+
// Without `encode`, you need to make sure inputs are encoded correctly.
|
272
|
+
const toPathRaw = compile("/user/:id");
|
273
|
+
|
274
|
+
toPathRaw({ id: "%3A%2F" }); //=> "/user/%3A%2F"
|
275
|
+
toPathRaw({ id: ":/" }, { validate: false }); //=> "/user/:/"
|
213
276
|
|
214
277
|
const toPathRepeated = compile("/:segment+");
|
215
278
|
|
@@ -224,11 +287,11 @@ toPathRegexp({ id: "abc" }); //=> Throws `TypeError`.
|
|
224
287
|
toPathRegexp({ id: "abc" }, { validate: false }); //=> "/user/abc"
|
225
288
|
```
|
226
289
|
|
227
|
-
**Note:** The generated function will throw on invalid input.
|
290
|
+
**Note:** The generated function will throw on invalid input.
|
228
291
|
|
229
292
|
### Working with Tokens
|
230
293
|
|
231
|
-
Path-To-RegExp exposes the two functions used internally that accept an array of tokens
|
294
|
+
Path-To-RegExp exposes the two functions used internally that accept an array of tokens:
|
232
295
|
|
233
296
|
- `tokensToRegexp(tokens, keys?, options?)` Transform an array of tokens into a matching regular expression.
|
234
297
|
- `tokensToFunction(tokens)` Transform an array of tokens into a path generator function.
|
@@ -236,11 +299,10 @@ Path-To-RegExp exposes the two functions used internally that accept an array of
|
|
236
299
|
#### Token Information
|
237
300
|
|
238
301
|
- `name` The name of the token (`string` for named or `number` for unnamed index)
|
239
|
-
- `prefix` The prefix
|
240
|
-
- `
|
241
|
-
- `optional` Indicates the token is optional (`boolean`)
|
242
|
-
- `repeat` Indicates the token is repeated (`boolean`)
|
302
|
+
- `prefix` The prefix string for the segment (e.g. `"/"`)
|
303
|
+
- `suffix` The suffix string for the segment (e.g. `""`)
|
243
304
|
- `pattern` The RegExp used to match this token (`string`)
|
305
|
+
- `modifier` The modifier character used for the segment (e.g. `?`)
|
244
306
|
|
245
307
|
## Compatibility with Express <= 4.x
|
246
308
|
|
package/dist/index.d.ts
CHANGED
@@ -4,16 +4,10 @@ export interface ParseOptions {
|
|
4
4
|
*/
|
5
5
|
delimiter?: string;
|
6
6
|
/**
|
7
|
-
* List of characters to consider
|
7
|
+
* List of characters to automatically consider prefixes when parsing.
|
8
8
|
*/
|
9
|
-
|
9
|
+
prefixes?: string;
|
10
10
|
}
|
11
|
-
/**
|
12
|
-
* Normalize a pathname for matching, replaces multiple slashes with a single
|
13
|
-
* slash and normalizes unicode characters to "NFC". When using this method,
|
14
|
-
* `decode` should be an identity function so you don't decode strings twice.
|
15
|
-
*/
|
16
|
-
export declare function normalizePathname(pathname: string): string;
|
17
11
|
/**
|
18
12
|
* Parse a string for the raw tokens.
|
19
13
|
*/
|
@@ -66,7 +60,7 @@ export declare type MatchFunction<P extends object = object> = (path: string) =>
|
|
66
60
|
/**
|
67
61
|
* Create path match function from `path-to-regexp` spec.
|
68
62
|
*/
|
69
|
-
export declare function match<P extends object = object>(str: Path, options?: ParseOptions &
|
63
|
+
export declare function match<P extends object = object>(str: Path, options?: ParseOptions & TokensToRegexpOptions & RegexpToFunctionOptions): MatchFunction<P>;
|
70
64
|
/**
|
71
65
|
* Create a path match function from `path-to-regexp` output.
|
72
66
|
*/
|
@@ -77,26 +71,21 @@ export declare function regexpToFunction<P extends object = object>(re: RegExp,
|
|
77
71
|
export interface Key {
|
78
72
|
name: string | number;
|
79
73
|
prefix: string;
|
80
|
-
|
81
|
-
optional: boolean;
|
82
|
-
repeat: boolean;
|
74
|
+
suffix: string;
|
83
75
|
pattern: string;
|
76
|
+
modifier: string;
|
84
77
|
}
|
85
78
|
/**
|
86
79
|
* A token is a string (nothing special) or key metadata (capture group).
|
87
80
|
*/
|
88
81
|
export declare type Token = string | Key;
|
89
|
-
|
90
|
-
* Expose a function for taking tokens and returning a RegExp.
|
91
|
-
*/
|
92
|
-
export declare function tokensToRegexp(tokens: Token[], keys?: Key[], options?: RegexpOptions): RegExp;
|
93
|
-
export interface RegexpOptions {
|
82
|
+
export interface TokensToRegexpOptions {
|
94
83
|
/**
|
95
84
|
* When `true` the regexp will be case sensitive. (default: `false`)
|
96
85
|
*/
|
97
86
|
sensitive?: boolean;
|
98
87
|
/**
|
99
|
-
* When `true` the regexp
|
88
|
+
* When `true` the regexp won't allow an optional trailing delimiter to match. (default: `false`)
|
100
89
|
*/
|
101
90
|
strict?: boolean;
|
102
91
|
/**
|
@@ -114,14 +103,16 @@ export interface RegexpOptions {
|
|
114
103
|
/**
|
115
104
|
* List of characters that can also be "end" characters.
|
116
105
|
*/
|
117
|
-
endsWith?: string
|
118
|
-
}
|
119
|
-
export interface ParseOptions {
|
106
|
+
endsWith?: string;
|
120
107
|
/**
|
121
|
-
*
|
108
|
+
* Encode path tokens for use in the `RegExp`.
|
122
109
|
*/
|
123
|
-
|
110
|
+
encode?: (value: string) => string;
|
124
111
|
}
|
112
|
+
/**
|
113
|
+
* Expose a function for taking tokens and returning a RegExp.
|
114
|
+
*/
|
115
|
+
export declare function tokensToRegexp(tokens: Token[], keys?: Key[], options?: TokensToRegexpOptions): RegExp;
|
125
116
|
/**
|
126
117
|
* Supported `path-to-regexp` input types.
|
127
118
|
*/
|
@@ -133,4 +124,4 @@ export declare type Path = string | RegExp | Array<string | RegExp>;
|
|
133
124
|
* placeholder key descriptions. For example, using `/user/:id`, `keys` will
|
134
125
|
* contain `[{ name: 'id', delimiter: '/', optional: false, repeat: false }]`.
|
135
126
|
*/
|
136
|
-
export declare function pathToRegexp(path: Path, keys?: Key[], options?:
|
127
|
+
export declare function pathToRegexp(path: Path, keys?: Key[], options?: TokensToRegexpOptions & ParseOptions): RegExp;
|