path-to-regexp
Advanced tools
Comparing version 2.4.0 to 3.0.0
@@ -0,1 +1,6 @@ | ||
2.4.0 / 2018-08-26 | ||
================== | ||
* Support `start` option to disable anchoring from beginning of the string | ||
2.3.0 / 2018-08-20 | ||
@@ -2,0 +7,0 @@ ================== |
@@ -29,2 +29,6 @@ declare function pathToRegexp (path: pathToRegexp.Path, keys?: pathToRegexp.Key[], options?: pathToRegexp.RegExpOptions & pathToRegexp.ParseOptions): RegExp; | ||
endsWith?: string | string[]; | ||
/** | ||
* List of characters to consider delimiters when parsing. (default: `undefined`, any character) | ||
*/ | ||
whitelist?: string | string[]; | ||
} | ||
@@ -37,6 +41,2 @@ | ||
delimiter?: string; | ||
/** | ||
* List of valid delimiter characters. (default: `'./'`) | ||
*/ | ||
delimiters?: string | string[]; | ||
} | ||
@@ -52,3 +52,3 @@ | ||
*/ | ||
export function compile (path: string, options?: ParseOptions): PathFunction; | ||
export function compile <P extends object = object> (path: string, options?: ParseOptions): PathFunction<P>; | ||
@@ -58,3 +58,3 @@ /** | ||
*/ | ||
export function tokensToFunction (tokens: Token[]): PathFunction; | ||
export function tokensToFunction <P extends object = object> (tokens: Token[]): PathFunction<P>; | ||
@@ -73,3 +73,2 @@ /** | ||
pattern: string; | ||
partial: boolean; | ||
} | ||
@@ -86,5 +85,5 @@ | ||
export type Path = string | RegExp | Array<string | RegExp>; | ||
export type PathFunction = (data?: Object, options?: PathFunctionOptions) => string; | ||
export type PathFunction <P extends object = object> = (data?: P, options?: PathFunctionOptions) => string; | ||
} | ||
export = pathToRegexp; |
46
index.js
@@ -14,3 +14,2 @@ /** | ||
var DEFAULT_DELIMITER = '/' | ||
var DEFAULT_DELIMITERS = './' | ||
@@ -47,3 +46,3 @@ /** | ||
var defaultDelimiter = (options && options.delimiter) || DEFAULT_DELIMITER | ||
var delimiters = (options && options.delimiters) || DEFAULT_DELIMITERS | ||
var whitelist = (options && options.whitelist) || undefined | ||
var pathEscaped = false | ||
@@ -67,3 +66,2 @@ var res | ||
var prev = '' | ||
var next = str[index] | ||
var name = res[2] | ||
@@ -76,5 +74,7 @@ var capture = res[3] | ||
var k = path.length - 1 | ||
var c = path[k] | ||
var matches = whitelist ? whitelist.indexOf(c) > -1 : true | ||
if (delimiters.indexOf(path[k]) > -1) { | ||
prev = path[k] | ||
if (matches) { | ||
prev = c | ||
path = path.slice(0, k) | ||
@@ -91,7 +91,6 @@ } | ||
var partial = prev !== '' && next !== undefined && next !== prev | ||
var repeat = modifier === '+' || modifier === '*' | ||
var optional = modifier === '?' || modifier === '*' | ||
var pattern = capture || group | ||
var delimiter = prev || defaultDelimiter | ||
var pattern = capture || group | ||
@@ -104,4 +103,5 @@ tokens.push({ | ||
repeat: repeat, | ||
partial: partial, | ||
pattern: pattern ? escapeGroup(pattern) : '[^' + escapeString(delimiter) + ']+?' | ||
pattern: pattern | ||
? escapeGroup(pattern) | ||
: '[^' + escapeString(delimiter === defaultDelimiter ? delimiter : (delimiter + defaultDelimiter)) + ']+?' | ||
}) | ||
@@ -193,9 +193,4 @@ } | ||
if (token.optional) { | ||
// Prepend partial segment prefixes. | ||
if (token.partial) path += token.prefix | ||
if (token.optional) continue | ||
continue | ||
} | ||
throw new TypeError('Expected "' + token.name + '" to be ' + (token.repeat ? 'an array' : 'a string')) | ||
@@ -259,3 +254,2 @@ } | ||
repeat: false, | ||
partial: false, | ||
pattern: null | ||
@@ -313,7 +307,5 @@ }) | ||
var end = options.end !== false | ||
var delimiter = escapeString(options.delimiter || DEFAULT_DELIMITER) | ||
var delimiters = options.delimiters || DEFAULT_DELIMITERS | ||
var delimiter = options.delimiter || DEFAULT_DELIMITER | ||
var endsWith = [].concat(options.endsWith || []).map(escapeString).concat('$').join('|') | ||
var route = start ? '^' : '' | ||
var isEndDelimited = tokens.length === 0 | ||
@@ -326,3 +318,2 @@ // Iterate over the tokens and create our regexp string. | ||
route += escapeString(token) | ||
isEndDelimited = i === tokens.length - 1 && delimiters.indexOf(token[token.length - 1]) > -1 | ||
} else { | ||
@@ -336,4 +327,4 @@ var capture = token.repeat | ||
if (token.optional) { | ||
if (token.partial) { | ||
route += escapeString(token.prefix) + '(' + capture + ')?' | ||
if (!token.prefix) { | ||
route += '(' + capture + ')?' | ||
} else { | ||
@@ -349,8 +340,13 @@ route += '(?:' + escapeString(token.prefix) + '(' + capture + '))?' | ||
if (end) { | ||
if (!strict) route += '(?:' + delimiter + ')?' | ||
if (!strict) route += '(?:' + escapeString(delimiter) + ')?' | ||
route += endsWith === '$' ? '$' : '(?=' + endsWith + ')' | ||
} else { | ||
if (!strict) route += '(?:' + delimiter + '(?=' + endsWith + '))?' | ||
if (!isEndDelimited) route += '(?=' + delimiter + '|' + endsWith + ')' | ||
var endToken = tokens[tokens.length - 1] | ||
var isEndDelimited = typeof endToken === 'string' | ||
? endToken[endToken.length - 1] === delimiter | ||
: endToken === undefined | ||
if (!strict) route += '(?:' + escapeString(delimiter) + '(?=' + endsWith + '))?' | ||
if (!isEndDelimited) route += '(?=' + escapeString(delimiter) + '|' + endsWith + ')' | ||
} | ||
@@ -357,0 +353,0 @@ |
{ | ||
"name": "path-to-regexp", | ||
"description": "Express style path to RegExp utility", | ||
"version": "2.4.0", | ||
"version": "3.0.0", | ||
"main": "index.js", | ||
@@ -41,3 +41,3 @@ "typings": "index.d.ts", | ||
"mocha": "^5.2.0", | ||
"standard": "^11.0.1", | ||
"standard": "^12.0.1", | ||
"ts-node": "^7.0.1", | ||
@@ -44,0 +44,0 @@ "typescript": "^3.0.1" |
@@ -21,3 +21,3 @@ # Path-to-RegExp | ||
```javascript | ||
var pathToRegexp = require('path-to-regexp') | ||
const pathToRegexp = require('path-to-regexp') | ||
@@ -30,3 +30,3 @@ // pathToRegexp(path, keys?, options?) | ||
- **path** A string, array of strings, or a regular expression. | ||
- **keys** An array to be populated with keys found in the path. | ||
- **keys** An array to populate with keys found in the path. | ||
- **options** | ||
@@ -37,15 +37,14 @@ - **sensitive** When `true` the regexp will be case sensitive. (default: `false`) | ||
- **start** When `true` the regexp will match from the beginning of the string. (default: `true`) | ||
- Advanced options (use for non-pathname strings, e.g. host names): | ||
- **delimiter** The default delimiter for segments. (default: `'/'`) | ||
- **endsWith** Optional character, or list of characters, to treat as "end" characters. | ||
- **delimiters** List of characters to consider delimiters when parsing. (default: `'./'`) | ||
- **delimiter** The default delimiter for segments. (default: `'/'`) | ||
- **endsWith** Optional character, or list of characters, to treat as "end" characters. | ||
- **whitelist** List of characters to consider delimiters when parsing. (default: `undefined`, any character) | ||
```javascript | ||
var keys = [] | ||
var re = pathToRegexp('/foo/:bar', keys) | ||
// re = /^\/foo\/([^\/]+?)\/?$/i | ||
const keys = [] | ||
const regexp = pathToRegexp('/foo/:bar', keys) | ||
// regexp = /^\/foo\/([^\/]+?)\/?$/i | ||
// keys = [{ name: 'bar', prefix: '/', delimiter: '/', optional: false, repeat: false, pattern: '[^\\/]+?' }] | ||
``` | ||
**Please note:** The `RegExp` returned by `path-to-regexp` is intended for ordered data (e.g. pathnames, hostnames). It does not handle arbitrary data (e.g. query strings, URL fragments, JSON, etc). | ||
**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). | ||
@@ -58,6 +57,6 @@ ### Parameters | ||
Named parameters are defined by prefixing a colon to the parameter name (`:foo`). By default, the parameter will match until the following path segment. | ||
Named parameters are defined by prefixing a colon to the parameter name (`:foo`). By default, the parameter will match until the next prefix (e.g. `[^/]+`). | ||
```js | ||
var re = pathToRegexp('/:foo/:bar') | ||
const regexp = pathToRegexp('/:foo/:bar') | ||
// keys = [{ name: 'foo', prefix: '/', ... }, { name: 'bar', prefix: '/', ... }] | ||
@@ -69,3 +68,3 @@ | ||
**Please note:** Parameter names must be made up of "word characters" (`[A-Za-z0-9_]`). | ||
**Please note:** Parameter names must use "word characters" (`[A-Za-z0-9_]`). | ||
@@ -79,3 +78,3 @@ #### Parameter Modifiers | ||
```js | ||
var re = pathToRegexp('/:foo/:bar?') | ||
const regexp = pathToRegexp('/:foo/:bar?') | ||
// keys = [{ name: 'foo', ... }, { name: 'bar', delimiter: '/', optional: true, repeat: false }] | ||
@@ -90,10 +89,10 @@ | ||
**Tip:** If the parameter is the _only_ value in the segment, the prefix is also optional. | ||
**Tip:** The prefix is also optional, escape the prefix `\/` to make it required. | ||
##### Zero or more | ||
Parameters can be suffixed with an asterisk (`*`) to denote a zero or more parameter matches. The prefix is taken into account for each match. | ||
Parameters can be suffixed with an asterisk (`*`) to denote a zero or more parameter matches. The prefix is used for each match. | ||
```js | ||
var re = pathToRegexp('/:foo*') | ||
const regexp = pathToRegexp('/:foo*') | ||
// keys = [{ name: 'foo', delimiter: '/', optional: true, repeat: true }] | ||
@@ -110,6 +109,6 @@ | ||
Parameters can be suffixed with a plus sign (`+`) to denote a one or more parameter matches. The prefix is taken into account for each match. | ||
Parameters can be suffixed with a plus sign (`+`) to denote a one or more parameter matches. The prefix is used for each match. | ||
```js | ||
var re = pathToRegexp('/:foo+') | ||
const regexp = pathToRegexp('/:foo+') | ||
// keys = [{ name: 'foo', delimiter: '/', optional: false, repeat: true }] | ||
@@ -124,31 +123,40 @@ | ||
#### Unnamed Parameters | ||
It is possible to write an unnamed parameter that only consists of a matching group. It works the same as a named parameter, except it will be numerically indexed. | ||
```js | ||
const regexp = pathToRegexp('/:foo/(.*)') | ||
// keys = [{ name: 'foo', ... }, { name: 0, ... }] | ||
regexp.exec('/test/route') | ||
//=> ['/test/route', 'test', 'route'] | ||
``` | ||
#### Custom Matching Parameters | ||
All parameters can be provided a custom regexp, which overrides the default match (`[^\/]+`). For example, you can match digits in the path: | ||
All parameters can have a custom regexp, which overrides the default match (`[^/]+`). For example, you can match digits or names in a path: | ||
```js | ||
var re = pathToRegexp('/icon-:foo(\\d+).png') | ||
const regexpNumbers = pathToRegexp('/icon-:foo(\\d+).png') | ||
// keys = [{ name: 'foo', ... }] | ||
re.exec('/icon-123.png') | ||
regexpNumbers.exec('/icon-123.png') | ||
//=> ['/icon-123.png', '123'] | ||
re.exec('/icon-abc.png') | ||
regexpNumbers.exec('/icon-abc.png') | ||
//=> null | ||
``` | ||
**Please note:** Backslashes need to be escaped with another backslash in strings. | ||
const regexpWord = pathToRegexp('/(user|u)') | ||
// keys = [{ name: 0, ... }] | ||
#### Unnamed Parameters | ||
regexpWord.exec('/u') | ||
//=> ['/u', 'u'] | ||
It is possible to write an unnamed parameter that only consists of a matching group. It works the same as a named parameter, except it will be numerically indexed. | ||
regexpWord.exec('/users') | ||
//=> null | ||
``` | ||
```js | ||
var re = pathToRegexp('/:foo/(.*)') | ||
// keys = [{ name: 'foo', ... }, { name: 0, ... }] | ||
**Tip:** Backslashes need to be escaped with another backslash in JavaScript strings. | ||
re.exec('/test/route') | ||
//=> ['/test/route', 'test', 'route'] | ||
``` | ||
### Parse | ||
@@ -159,3 +167,3 @@ | ||
```js | ||
var tokens = pathToRegexp.parse('/route/:foo/(.*)') | ||
const tokens = pathToRegexp.parse('/route/:foo/(.*)') | ||
@@ -179,3 +187,3 @@ console.log(tokens[0]) | ||
```js | ||
var toPath = pathToRegexp.compile('/user/:id') | ||
const toPath = pathToRegexp.compile('/user/:id') | ||
@@ -189,3 +197,3 @@ toPath({ id: 123 }) //=> "/user/123" | ||
var toPathRepeated = pathToRegexp.compile('/:segment+') | ||
const toPathRepeated = pathToRegexp.compile('/:segment+') | ||
@@ -195,3 +203,3 @@ toPathRepeated({ segment: 'foo' }) //=> "/foo" | ||
var toPathRegexp = pathToRegexp.compile('/user/:id(\\d+)') | ||
const toPathRegexp = pathToRegexp.compile('/user/:id(\\d+)') | ||
@@ -215,7 +223,6 @@ toPathRegexp({ id: 123 }) //=> "/user/123" | ||
* `name` The name of the token (`string` for named or `number` for index) | ||
* `prefix` The prefix character for the segment (`/` or `.`) | ||
* `delimiter` The delimiter for the segment (same as prefix or `/`) | ||
* `prefix` The prefix character for the segment (e.g. `/`) | ||
* `delimiter` The delimiter for the segment (same as prefix or default delimiter) | ||
* `optional` Indicates the token is optional (`boolean`) | ||
* `repeat` Indicates the token is repeated (`boolean`) | ||
* `partial` Indicates this token is a partial path segment (`boolean`) | ||
* `pattern` The RegExp used to match this token (`string`) | ||
@@ -222,0 +229,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27504
249
383