path-to-regexp 1.5.1 → 1.7.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 +17 -0
- package/LICENSE +21 -0
- package/Readme.md +3 -0
- package/index.d.ts +27 -9
- package/index.js +33 -33
- package/package.json +1 -1
package/History.md
CHANGED
@@ -1,3 +1,20 @@
|
|
1
|
+
1.6.0 / 2016-10-03
|
2
|
+
==================
|
3
|
+
|
4
|
+
* Populate `RegExp.keys` when using the `tokensToRegExp` method (making it consistent with the main export)
|
5
|
+
* Allow a `delimiter` option to be passed in with `parse`
|
6
|
+
* Updated TypeScript definition with `Keys` and `Options` updated
|
7
|
+
|
8
|
+
1.5.3 / 2016-06-15
|
9
|
+
==================
|
10
|
+
|
11
|
+
* Add `\\` to the ignore character group to avoid backtracking on mismatched parens
|
12
|
+
|
13
|
+
1.5.2 / 2016-06-15
|
14
|
+
==================
|
15
|
+
|
16
|
+
* Escape `\\` in string segments of regexp
|
17
|
+
|
1
18
|
1.5.1 / 2016-06-08
|
2
19
|
==================
|
3
20
|
|
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Blake Embrey (hello@blakeembrey.com)
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
package/Readme.md
CHANGED
@@ -31,6 +31,7 @@ var pathToRegexp = require('path-to-regexp')
|
|
31
31
|
- **sensitive** When `true` the route will be case sensitive. (default: `false`)
|
32
32
|
- **strict** When `false` the trailing slash is optional. (default: `false`)
|
33
33
|
- **end** When `false` the path will match at the beginning. (default: `true`)
|
34
|
+
- **delimiter** Set the default delimiter for repeat parameters. (default: `'/'`)
|
34
35
|
|
35
36
|
```javascript
|
36
37
|
var keys = []
|
@@ -39,6 +40,8 @@ var re = pathToRegexp('/foo/:bar', keys)
|
|
39
40
|
// keys = [{ name: 'bar', prefix: '/', delimiter: '/', optional: false, repeat: false, pattern: '[^\\/]+?' }]
|
40
41
|
```
|
41
42
|
|
43
|
+
**Please note:** The `RegExp` returned by `path-to-regexp` is intended for use with pathnames or hostnames. It can not handle the query strings or fragments of a URL.
|
44
|
+
|
42
45
|
### Parameters
|
43
46
|
|
44
47
|
The path string can be used to define parameters and populate the keys.
|
package/index.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
declare function pathToRegexp (path: pathToRegexp.Path, options?: pathToRegexp.
|
2
|
-
declare function pathToRegexp (path: pathToRegexp.Path, keys
|
1
|
+
declare function pathToRegexp (path: pathToRegexp.Path, options?: pathToRegexp.RegExpOptions & pathToRegexp.ParseOptions): pathToRegexp.PathRegExp;
|
2
|
+
declare function pathToRegexp (path: pathToRegexp.Path, keys?: pathToRegexp.Key[], options?: pathToRegexp.RegExpOptions & pathToRegexp.ParseOptions): pathToRegexp.PathRegExp;
|
3
3
|
|
4
4
|
declare namespace pathToRegexp {
|
5
5
|
export interface PathRegExp extends RegExp {
|
@@ -7,24 +7,41 @@ declare namespace pathToRegexp {
|
|
7
7
|
keys: Key[];
|
8
8
|
}
|
9
9
|
|
10
|
-
export interface
|
11
|
-
|
10
|
+
export interface RegExpOptions {
|
11
|
+
/**
|
12
|
+
* When `true` the route will be case sensitive. (default: `false`)
|
13
|
+
*/
|
12
14
|
sensitive?: boolean;
|
13
|
-
|
15
|
+
/**
|
16
|
+
* When `false` the trailing slash is optional. (default: `false`)
|
17
|
+
*/
|
14
18
|
strict?: boolean;
|
15
|
-
|
19
|
+
/**
|
20
|
+
* When `false` the path will match at the beginning. (default: `true`)
|
21
|
+
*/
|
16
22
|
end?: boolean;
|
23
|
+
/**
|
24
|
+
* Sets the final character for non-ending optimistic matches. (default: `/`)
|
25
|
+
*/
|
26
|
+
delimiter?: string;
|
27
|
+
}
|
28
|
+
|
29
|
+
export interface ParseOptions {
|
30
|
+
/**
|
31
|
+
* Set the default delimiter for repeat parameters. (default: `'/'`)
|
32
|
+
*/
|
33
|
+
delimiter?: string;
|
17
34
|
}
|
18
35
|
|
19
36
|
/**
|
20
37
|
* Parse an Express-style path into an array of tokens.
|
21
38
|
*/
|
22
|
-
export function parse (path: string): Token[];
|
39
|
+
export function parse (path: string, options?: ParseOptions): Token[];
|
23
40
|
|
24
41
|
/**
|
25
42
|
* Transforming an Express-style path into a valid path.
|
26
43
|
*/
|
27
|
-
export function compile (path: string): PathFunction;
|
44
|
+
export function compile (path: string, options?: ParseOptions): PathFunction;
|
28
45
|
|
29
46
|
/**
|
30
47
|
* Transform an array of tokens into a path generator function.
|
@@ -34,7 +51,8 @@ declare namespace pathToRegexp {
|
|
34
51
|
/**
|
35
52
|
* Transform an array of tokens into a matching regular expression.
|
36
53
|
*/
|
37
|
-
export function tokensToRegExp (tokens: Token[], options?:
|
54
|
+
export function tokensToRegExp (tokens: Token[], options?: RegExpOptions): PathRegExp;
|
55
|
+
export function tokensToRegExp (tokens: Token[], keys?: Key[], options?: RegExpOptions): PathRegExp;
|
38
56
|
|
39
57
|
export interface Key {
|
40
58
|
name: string | number;
|
package/index.js
CHANGED
@@ -24,20 +24,22 @@ var PATH_REGEXP = new RegExp([
|
|
24
24
|
// "/:test(\\d+)?" => ["/", "test", "\d+", undefined, "?", undefined]
|
25
25
|
// "/route(\\d+)" => [undefined, undefined, undefined, "\d+", undefined, undefined]
|
26
26
|
// "/*" => ["/", undefined, undefined, undefined, undefined, "*"]
|
27
|
-
'([\\/.])?(?:(?:\\:(\\w+)(?:\\(((?:\\\\.|[
|
27
|
+
'([\\/.])?(?:(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?|(\\*))'
|
28
28
|
].join('|'), 'g')
|
29
29
|
|
30
30
|
/**
|
31
31
|
* Parse a string for the raw tokens.
|
32
32
|
*
|
33
|
-
* @param {string}
|
33
|
+
* @param {string} str
|
34
|
+
* @param {Object=} options
|
34
35
|
* @return {!Array}
|
35
36
|
*/
|
36
|
-
function parse (str) {
|
37
|
+
function parse (str, options) {
|
37
38
|
var tokens = []
|
38
39
|
var key = 0
|
39
40
|
var index = 0
|
40
41
|
var path = ''
|
42
|
+
var defaultDelimiter = options && options.delimiter || '/'
|
41
43
|
var res
|
42
44
|
|
43
45
|
while ((res = PATH_REGEXP.exec(str)) != null) {
|
@@ -70,8 +72,8 @@ function parse (str) {
|
|
70
72
|
var partial = prefix != null && next != null && next !== prefix
|
71
73
|
var repeat = modifier === '+' || modifier === '*'
|
72
74
|
var optional = modifier === '?' || modifier === '*'
|
73
|
-
var delimiter = res[2] ||
|
74
|
-
var pattern = capture || group
|
75
|
+
var delimiter = res[2] || defaultDelimiter
|
76
|
+
var pattern = capture || group
|
75
77
|
|
76
78
|
tokens.push({
|
77
79
|
name: name || key++,
|
@@ -81,7 +83,7 @@ function parse (str) {
|
|
81
83
|
repeat: repeat,
|
82
84
|
partial: partial,
|
83
85
|
asterisk: !!asterisk,
|
84
|
-
pattern: escapeGroup(pattern)
|
86
|
+
pattern: pattern ? escapeGroup(pattern) : (asterisk ? '.*' : '[^' + escapeString(delimiter) + ']+?')
|
85
87
|
})
|
86
88
|
}
|
87
89
|
|
@@ -102,10 +104,11 @@ function parse (str) {
|
|
102
104
|
* Compile a string to a template function for the path.
|
103
105
|
*
|
104
106
|
* @param {string} str
|
107
|
+
* @param {Object=} options
|
105
108
|
* @return {!function(Object=, Object=)}
|
106
109
|
*/
|
107
|
-
function compile (str) {
|
108
|
-
return tokensToFunction(parse(str))
|
110
|
+
function compile (str, options) {
|
111
|
+
return tokensToFunction(parse(str, options))
|
109
112
|
}
|
110
113
|
|
111
114
|
/**
|
@@ -223,7 +226,7 @@ function tokensToFunction (tokens) {
|
|
223
226
|
* @return {string}
|
224
227
|
*/
|
225
228
|
function escapeString (str) {
|
226
|
-
return str.replace(/([.+*?=^!:${}()[\]
|
229
|
+
return str.replace(/([.+*?=^!:${}()[\]|\/\\])/g, '\\$1')
|
227
230
|
}
|
228
231
|
|
229
232
|
/**
|
@@ -316,34 +319,28 @@ function arrayToRegexp (path, keys, options) {
|
|
316
319
|
* @return {!RegExp}
|
317
320
|
*/
|
318
321
|
function stringToRegexp (path, keys, options) {
|
319
|
-
|
320
|
-
var re = tokensToRegExp(tokens, options)
|
321
|
-
|
322
|
-
// Attach keys back to the regexp.
|
323
|
-
for (var i = 0; i < tokens.length; i++) {
|
324
|
-
if (typeof tokens[i] !== 'string') {
|
325
|
-
keys.push(tokens[i])
|
326
|
-
}
|
327
|
-
}
|
328
|
-
|
329
|
-
return attachKeys(re, keys)
|
322
|
+
return tokensToRegExp(parse(path, options), keys, options)
|
330
323
|
}
|
331
324
|
|
332
325
|
/**
|
333
326
|
* Expose a function for taking tokens and returning a RegExp.
|
334
327
|
*
|
335
|
-
* @param {!Array}
|
336
|
-
* @param {Object=}
|
328
|
+
* @param {!Array} tokens
|
329
|
+
* @param {(Array|Object)=} keys
|
330
|
+
* @param {Object=} options
|
337
331
|
* @return {!RegExp}
|
338
332
|
*/
|
339
|
-
function tokensToRegExp (tokens, options) {
|
333
|
+
function tokensToRegExp (tokens, keys, options) {
|
334
|
+
if (!isarray(keys)) {
|
335
|
+
options = /** @type {!Object} */ (keys || options)
|
336
|
+
keys = []
|
337
|
+
}
|
338
|
+
|
340
339
|
options = options || {}
|
341
340
|
|
342
341
|
var strict = options.strict
|
343
342
|
var end = options.end !== false
|
344
343
|
var route = ''
|
345
|
-
var lastToken = tokens[tokens.length - 1]
|
346
|
-
var endsWithSlash = typeof lastToken === 'string' && /\/$/.test(lastToken)
|
347
344
|
|
348
345
|
// Iterate over the tokens and create our regexp string.
|
349
346
|
for (var i = 0; i < tokens.length; i++) {
|
@@ -355,6 +352,8 @@ function tokensToRegExp (tokens, options) {
|
|
355
352
|
var prefix = escapeString(token.prefix)
|
356
353
|
var capture = '(?:' + token.pattern + ')'
|
357
354
|
|
355
|
+
keys.push(token)
|
356
|
+
|
358
357
|
if (token.repeat) {
|
359
358
|
capture += '(?:' + prefix + capture + ')*'
|
360
359
|
}
|
@@ -373,12 +372,15 @@ function tokensToRegExp (tokens, options) {
|
|
373
372
|
}
|
374
373
|
}
|
375
374
|
|
375
|
+
var delimiter = escapeString(options.delimiter || '/')
|
376
|
+
var endsWithDelimiter = route.slice(-delimiter.length) === delimiter
|
377
|
+
|
376
378
|
// In non-strict mode we allow a slash at the end of match. If the path to
|
377
379
|
// match already ends with a slash, we remove it for consistency. The slash
|
378
380
|
// is valid at the end of a path match, not in the middle. This is important
|
379
381
|
// in non-ending mode, where "/test/" shouldn't match "/test//route".
|
380
382
|
if (!strict) {
|
381
|
-
route = (
|
383
|
+
route = (endsWithDelimiter ? route.slice(0, -delimiter.length) : route) + '(?:' + delimiter + '(?=$))?'
|
382
384
|
}
|
383
385
|
|
384
386
|
if (end) {
|
@@ -386,10 +388,10 @@ function tokensToRegExp (tokens, options) {
|
|
386
388
|
} else {
|
387
389
|
// In non-ending mode, we need the capturing groups to match as much as
|
388
390
|
// possible by using a positive lookahead to the end or next path segment.
|
389
|
-
route += strict &&
|
391
|
+
route += strict && endsWithDelimiter ? '' : '(?=' + delimiter + '|$)'
|
390
392
|
}
|
391
393
|
|
392
|
-
return new RegExp('^' + route, flags(options))
|
394
|
+
return attachKeys(new RegExp('^' + route, flags(options)), keys)
|
393
395
|
}
|
394
396
|
|
395
397
|
/**
|
@@ -405,15 +407,13 @@ function tokensToRegExp (tokens, options) {
|
|
405
407
|
* @return {!RegExp}
|
406
408
|
*/
|
407
409
|
function pathToRegexp (path, keys, options) {
|
408
|
-
keys = keys || []
|
409
|
-
|
410
410
|
if (!isarray(keys)) {
|
411
|
-
options = /** @type {!Object} */ (keys)
|
411
|
+
options = /** @type {!Object} */ (keys || options)
|
412
412
|
keys = []
|
413
|
-
} else if (!options) {
|
414
|
-
options = {}
|
415
413
|
}
|
416
414
|
|
415
|
+
options = options || {}
|
416
|
+
|
417
417
|
if (path instanceof RegExp) {
|
418
418
|
return regexpToRegexp(path, /** @type {!Array} */ (keys))
|
419
419
|
}
|