path-to-regexp 3.0.0 → 3.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/History.md +6 -0
- package/Readme.md +15 -14
- package/index.d.ts +13 -2
- package/index.js +6 -5
- package/package.json +5 -5
package/History.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
3.0.0 / 2019-01-13
|
2
|
+
==================
|
3
|
+
|
4
|
+
* Always use prefix character as delimiter token, allowing any character to be a delimiter (e.g. `/:att1-:att2-:att3-:att4-:att5`)
|
5
|
+
* Remove `partial` support, prefer escaping the prefix delimiter explicitly (e.g. `\\/(apple-)?icon-:res(\\d+).png`)
|
6
|
+
|
1
7
|
2.4.0 / 2018-08-26
|
2
8
|
==================
|
3
9
|
|
package/Readme.md
CHANGED
@@ -57,8 +57,8 @@ Named parameters are defined by prefixing a colon to the parameter name (`:foo`)
|
|
57
57
|
const regexp = pathToRegexp('/:foo/:bar')
|
58
58
|
// keys = [{ name: 'foo', prefix: '/', ... }, { name: 'bar', prefix: '/', ... }]
|
59
59
|
|
60
|
-
|
61
|
-
//=> ['/test/route', 'test', 'route']
|
60
|
+
regexp.exec('/test/route')
|
61
|
+
//=> [ '/test/route', 'test', 'route', index: 0, input: '/test/route', groups: undefined ]
|
62
62
|
```
|
63
63
|
|
64
64
|
**Please note:** Parameter names must use "word characters" (`[A-Za-z0-9_]`).
|
@@ -73,11 +73,11 @@ Parameters can be suffixed with a question mark (`?`) to make the parameter opti
|
|
73
73
|
const regexp = pathToRegexp('/:foo/:bar?')
|
74
74
|
// keys = [{ name: 'foo', ... }, { name: 'bar', delimiter: '/', optional: true, repeat: false }]
|
75
75
|
|
76
|
-
|
77
|
-
//=> ['/test', 'test', undefined]
|
76
|
+
regexp.exec('/test')
|
77
|
+
//=> [ '/test', 'test', undefined, index: 0, input: '/test', groups: undefined ]
|
78
78
|
|
79
|
-
|
80
|
-
//=> ['/test', 'test', 'route']
|
79
|
+
regexp.exec('/test/route')
|
80
|
+
//=> [ '/test/route', 'test', 'route', index: 0, input: '/test/route', groups: undefined ]
|
81
81
|
```
|
82
82
|
|
83
83
|
**Tip:** The prefix is also optional, escape the prefix `\/` to make it required.
|
@@ -90,11 +90,11 @@ Parameters can be suffixed with an asterisk (`*`) to denote a zero or more param
|
|
90
90
|
const regexp = pathToRegexp('/:foo*')
|
91
91
|
// keys = [{ name: 'foo', delimiter: '/', optional: true, repeat: true }]
|
92
92
|
|
93
|
-
|
94
|
-
//=> ['/', undefined]
|
93
|
+
regexp.exec('/')
|
94
|
+
//=> [ '/', undefined, index: 0, input: '/', groups: undefined ]
|
95
95
|
|
96
|
-
|
97
|
-
//=> ['/bar/baz', 'bar/baz']
|
96
|
+
regexp.exec('/bar/baz')
|
97
|
+
//=> [ '/bar/baz', 'bar/baz', index: 0, input: '/bar/baz', groups: undefined ]
|
98
98
|
```
|
99
99
|
|
100
100
|
##### One or more
|
@@ -105,11 +105,11 @@ Parameters can be suffixed with a plus sign (`+`) to denote a one or more parame
|
|
105
105
|
const regexp = pathToRegexp('/:foo+')
|
106
106
|
// keys = [{ name: 'foo', delimiter: '/', optional: false, repeat: true }]
|
107
107
|
|
108
|
-
|
108
|
+
regexp.exec('/')
|
109
109
|
//=> null
|
110
110
|
|
111
|
-
|
112
|
-
//=> ['/bar/baz', 'bar/baz']
|
111
|
+
regexp.exec('/bar/baz')
|
112
|
+
//=> [ '/bar/baz','bar/baz', index: 0, input: '/bar/baz', groups: undefined ]
|
113
113
|
```
|
114
114
|
|
115
115
|
#### Unnamed Parameters
|
@@ -121,7 +121,7 @@ const regexp = pathToRegexp('/:foo/(.*)')
|
|
121
121
|
// keys = [{ name: 'foo', ... }, { name: 0, ... }]
|
122
122
|
|
123
123
|
regexp.exec('/test/route')
|
124
|
-
//=> ['/test/route', 'test', 'route']
|
124
|
+
//=> [ '/test/route', 'test', 'route', index: 0, input: '/test/route', groups: undefined ]
|
125
125
|
```
|
126
126
|
|
127
127
|
#### Custom Matching Parameters
|
@@ -193,6 +193,7 @@ const toPathRegexp = pathToRegexp.compile('/user/:id(\\d+)')
|
|
193
193
|
toPathRegexp({ id: 123 }) //=> "/user/123"
|
194
194
|
toPathRegexp({ id: '123' }) //=> "/user/123"
|
195
195
|
toPathRegexp({ id: 'abc' }) //=> Throws `TypeError`.
|
196
|
+
toPathRegexp({ id: 'abc' }, { validate: true }) //=> "/user/abc"
|
196
197
|
```
|
197
198
|
|
198
199
|
**Note:** The generated function will throw on invalid input. It will do all necessary checks to ensure the generated path is valid. This method only works with strings.
|
package/index.d.ts
CHANGED
@@ -39,6 +39,13 @@ declare namespace pathToRegexp {
|
|
39
39
|
delimiter?: string;
|
40
40
|
}
|
41
41
|
|
42
|
+
export interface TokensToFunctionOptions {
|
43
|
+
/**
|
44
|
+
* When `true` the regexp will be case sensitive. (default: `false`)
|
45
|
+
*/
|
46
|
+
sensitive?: boolean;
|
47
|
+
}
|
48
|
+
|
42
49
|
/**
|
43
50
|
* Parse an Express-style path into an array of tokens.
|
44
51
|
*/
|
@@ -47,12 +54,12 @@ declare namespace pathToRegexp {
|
|
47
54
|
/**
|
48
55
|
* Transforming an Express-style path into a valid path.
|
49
56
|
*/
|
50
|
-
export function compile <P extends object = object> (path: string, options?: ParseOptions): PathFunction<P>;
|
57
|
+
export function compile <P extends object = object> (path: string, options?: ParseOptions & TokensToFunctionOptions): PathFunction<P>;
|
51
58
|
|
52
59
|
/**
|
53
60
|
* Transform an array of tokens into a path generator function.
|
54
61
|
*/
|
55
|
-
export function tokensToFunction <P extends object = object> (tokens: Token[]): PathFunction<P>;
|
62
|
+
export function tokensToFunction <P extends object = object> (tokens: Token[], options?: TokensToFunctionOptions): PathFunction<P>;
|
56
63
|
|
57
64
|
/**
|
58
65
|
* Transform an array of tokens into a matching regular expression.
|
@@ -73,6 +80,10 @@ declare namespace pathToRegexp {
|
|
73
80
|
* Function for encoding input strings for output.
|
74
81
|
*/
|
75
82
|
encode?: (value: string, token: Key) => string;
|
83
|
+
/**
|
84
|
+
* When `false` the function can produce an invalid (unmatched) path. (default: `true`)
|
85
|
+
*/
|
86
|
+
validate?: boolean;
|
76
87
|
}
|
77
88
|
|
78
89
|
export type Token = string | Key;
|
package/index.js
CHANGED
@@ -117,26 +117,27 @@ function parse (str, options) {
|
|
117
117
|
* @return {!function(Object=, Object=)}
|
118
118
|
*/
|
119
119
|
function compile (str, options) {
|
120
|
-
return tokensToFunction(parse(str, options))
|
120
|
+
return tokensToFunction(parse(str, options), options)
|
121
121
|
}
|
122
122
|
|
123
123
|
/**
|
124
124
|
* Expose a method for transforming tokens into the path function.
|
125
125
|
*/
|
126
|
-
function tokensToFunction (tokens) {
|
126
|
+
function tokensToFunction (tokens, options) {
|
127
127
|
// Compile all the tokens into regexps.
|
128
128
|
var matches = new Array(tokens.length)
|
129
129
|
|
130
130
|
// Compile all the patterns before compilation.
|
131
131
|
for (var i = 0; i < tokens.length; i++) {
|
132
132
|
if (typeof tokens[i] === 'object') {
|
133
|
-
matches[i] = new RegExp('^(?:' + tokens[i].pattern + ')$')
|
133
|
+
matches[i] = new RegExp('^(?:' + tokens[i].pattern + ')$', flags(options))
|
134
134
|
}
|
135
135
|
}
|
136
136
|
|
137
137
|
return function (data, options) {
|
138
138
|
var path = ''
|
139
139
|
var encode = (options && options.encode) || encodeURIComponent
|
140
|
+
var validate = options ? options.validate !== false : true
|
140
141
|
|
141
142
|
for (var i = 0; i < tokens.length; i++) {
|
142
143
|
var token = tokens[i]
|
@@ -163,7 +164,7 @@ function tokensToFunction (tokens) {
|
|
163
164
|
for (var j = 0; j < value.length; j++) {
|
164
165
|
segment = encode(value[j], token)
|
165
166
|
|
166
|
-
if (!matches[i].test(segment)) {
|
167
|
+
if (validate && !matches[i].test(segment)) {
|
167
168
|
throw new TypeError('Expected all "' + token.name + '" to match "' + token.pattern + '"')
|
168
169
|
}
|
169
170
|
|
@@ -176,7 +177,7 @@ function tokensToFunction (tokens) {
|
|
176
177
|
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
177
178
|
segment = encode(String(value), token)
|
178
179
|
|
179
|
-
if (!matches[i].test(segment)) {
|
180
|
+
if (validate && !matches[i].test(segment)) {
|
180
181
|
throw new TypeError('Expected "' + token.name + '" to match "' + token.pattern + '", but got "' + segment + '"')
|
181
182
|
}
|
182
183
|
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "path-to-regexp",
|
3
3
|
"description": "Express style path to RegExp utility",
|
4
|
-
"version": "3.
|
4
|
+
"version": "3.1.0",
|
5
5
|
"main": "index.js",
|
6
6
|
"typings": "index.d.ts",
|
7
7
|
"files": [
|
@@ -34,12 +34,12 @@
|
|
34
34
|
"devDependencies": {
|
35
35
|
"@types/chai": "^4.0.4",
|
36
36
|
"@types/mocha": "^5.2.5",
|
37
|
-
"@types/node": "^
|
37
|
+
"@types/node": "^12.7.3",
|
38
38
|
"chai": "^4.1.1",
|
39
39
|
"istanbul": "^0.4.5",
|
40
|
-
"mocha": "^
|
41
|
-
"standard": "^
|
42
|
-
"ts-node": "^
|
40
|
+
"mocha": "^6.2.0",
|
41
|
+
"standard": "^14.1.0",
|
42
|
+
"ts-node": "^8.3.0",
|
43
43
|
"typescript": "^3.0.1"
|
44
44
|
}
|
45
45
|
}
|