Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

path-to-regexp

Package Overview
Dependencies
Maintainers
32
Versions
67
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

path-to-regexp - npm Package Compare versions

Comparing version 1.2.1 to 1.3.0

6

History.md

@@ -0,1 +1,7 @@

Unreleased
==================
* Clarify README intentions and named parameter token support
* Support advanced Closure Compiler with type annotations
1.2.1 / 2015-08-17

@@ -2,0 +8,0 @@ ==================

116

index.js

@@ -33,4 +33,4 @@ var isarray = require('isarray')

*
* @param {String} str
* @return {Array}
* @param {string} str
* @return {!Array}
*/

@@ -57,2 +57,16 @@ function parse (str) {

var next = str[index]
var prefix = res[2]
var name = res[3]
var capture = res[4]
var group = res[5]
var modifier = res[6]
var asterisk = res[7]
// Only use the prefix when followed by another path segment.
if (prefix != null && next != null && next !== prefix) {
path += prefix
prefix = null
}
// Push the current path onto the tokens.

@@ -64,12 +78,5 @@ if (path) {

var prefix = res[2]
var name = res[3]
var capture = res[4]
var group = res[5]
var suffix = res[6]
var asterisk = res[7]
var repeat = suffix === '+' || suffix === '*'
var optional = suffix === '?' || suffix === '*'
var delimiter = prefix || '/'
var repeat = modifier === '+' || modifier === '*'
var optional = modifier === '?' || modifier === '*'
var delimiter = res[2] || '/'
var pattern = capture || group || (asterisk ? '.*' : '[^' + delimiter + ']+?')

@@ -103,4 +110,4 @@

*
* @param {String} str
* @return {Function}
* @param {string} str
* @return {!function(Object=, Object=)}
*/

@@ -112,2 +119,14 @@ function compile (str) {

/**
* Encode characters for segment that could cause trouble for parsing.
*
* @param {string}
* @return {string}
*/
function encodeURIComponentPretty (str) {
return encodeURI(str).replace(/[/?#'"]/g, function (c) {
return '%' + c.charCodeAt(0).toString(16).toUpperCase()
})
}
/**
* Expose a method for transforming tokens into the path function.

@@ -126,5 +145,7 @@ */

return function (obj) {
return function (obj, opts) {
var path = ''
var data = obj || {}
var options = opts || {}
var encode = options.pretty ? encodeURIComponentPretty : encodeURIComponent

@@ -165,3 +186,3 @@ for (var i = 0; i < tokens.length; i++) {

for (var j = 0; j < value.length; j++) {
segment = encodeURIComponent(value[j])
segment = encode(value[j])

@@ -178,3 +199,3 @@ if (!matches[i].test(segment)) {

segment = encodeURIComponent(value)
segment = encode(value)

@@ -195,4 +216,4 @@ if (!matches[i].test(segment)) {

*
* @param {String} str
* @return {String}
* @param {string} str
* @return {string}
*/

@@ -206,4 +227,4 @@ function escapeString (str) {

*
* @param {String} group
* @return {String}
* @param {string} group
* @return {string}
*/

@@ -217,5 +238,5 @@ function escapeGroup (group) {

*
* @param {RegExp} re
* @param {Array} keys
* @return {RegExp}
* @param {!RegExp} re
* @param {Array} keys
* @return {!RegExp}
*/

@@ -231,3 +252,3 @@ function attachKeys (re, keys) {

* @param {Object} options
* @return {String}
* @return {string}
*/

@@ -241,5 +262,5 @@ function flags (options) {

*
* @param {RegExp} path
* @param {Array} keys
* @return {RegExp}
* @param {!RegExp} path
* @param {!Array} keys
* @return {!RegExp}
*/

@@ -269,6 +290,6 @@ function regexpToRegexp (path, keys) {

*
* @param {Array} path
* @param {Array} keys
* @param {Object} options
* @return {RegExp}
* @param {!Array} path
* @param {Array} keys
* @param {!Object} options
* @return {!RegExp}
*/

@@ -290,6 +311,6 @@ function arrayToRegexp (path, keys, options) {

*
* @param {String} path
* @param {Array} keys
* @param {Object} options
* @return {RegExp}
* @param {string} path
* @param {!Array} keys
* @param {!Object} options
* @return {!RegExp}
*/

@@ -313,6 +334,5 @@ function stringToRegexp (path, keys, options) {

*
* @param {Array} tokens
* @param {Array} keys
* @param {Object} options
* @return {RegExp}
* @param {!Array} tokens
* @param {Object=} options
* @return {!RegExp}
*/

@@ -382,6 +402,6 @@ function tokensToRegExp (tokens, options) {

*
* @param {(String|RegExp|Array)} path
* @param {Array} [keys]
* @param {Object} [options]
* @return {RegExp}
* @param {(string|RegExp|Array)} path
* @param {(Array|Object)=} keys
* @param {Object=} options
* @return {!RegExp}
*/

@@ -392,3 +412,3 @@ function pathToRegexp (path, keys, options) {

if (!isarray(keys)) {
options = keys
options = /** @type {!Object} */ (keys)
keys = []

@@ -400,10 +420,10 @@ } else if (!options) {

if (path instanceof RegExp) {
return regexpToRegexp(path, keys, options)
return regexpToRegexp(path, /** @type {!Array} */ (keys))
}
if (isarray(path)) {
return arrayToRegexp(path, keys, options)
return arrayToRegexp(/** @type {!Array} */ (path), /** @type {!Array} */ (keys), options)
}
return stringToRegexp(path, keys, options)
return stringToRegexp(/** @type {string} */ (path), /** @type {!Array} */ (keys), options)
}
{
"name": "path-to-regexp",
"description": "Express style path to RegExp utility",
"version": "1.2.1",
"version": "1.3.0",
"main": "index.js",
"typings": "index.d.ts",
"files": [

@@ -11,4 +13,5 @@ "index.js",

"lint": "standard",
"test-spec": "mocha -R spec --bail",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- -R spec",
"test-spec": "mocha --require ts-node/register -R spec --bail test.ts",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --require ts-node/register -R spec test.ts",
"prepublish": "typings install",
"test": "npm run lint && npm run test-cov"

@@ -37,3 +40,6 @@ },

"pre-commit": "~1.0.5",
"standard": "~3.7.3"
"standard": "~3.7.3",
"ts-node": "^0.5.5",
"typescript": "^1.8.7",
"typings": "^0.6.9"
},

@@ -40,0 +46,0 @@ "dependencies": {

@@ -28,4 +28,4 @@ # Path-to-RegExp

- **path** A string in the express format, an array of strings, or a regular expression.
- **keys** An array to be populated with the keys present in the url.
- **path** An Express-style string, an array of strings, or a regular expression.
- **keys** An array to be populated with the keys found in the path.
- **options**

@@ -45,11 +45,11 @@ - **sensitive** When `true` the route will be case sensitive. (default: `false`)

The path has the ability to define parameters and automatically populate the keys array.
The path string can be used to define parameters and populate the keys.
#### Named Parameters
Named parameters are defined by prefixing a colon to the parameter name (`:foo`). By default, this parameter will match up to the next path segment.
Named parameters are defined by prefixing a colon to the parameter name (`:foo`). By default, the parameter will match until the following path segment.
```js
var re = pathToRegexp('/:foo/:bar', keys)
// keys = [{ name: 'foo', ... }, { name: 'bar', ... }]
// keys = [{ name: 'foo', prefix: '/', ... }, { name: 'bar', prefix: '/', ... }]

@@ -60,7 +60,19 @@ re.exec('/test/route')

#### Suffixed Parameters
**Please note:** Named parameters must be made up of "word characters" (`[A-Za-z0-9_]`).
Path segments are defined by "prefix" characters (`.` or `/`). If a prefix is used, and the parameter is followed by the same prefix character or end of the path, it is considered a segment and the prefix is part of the match. This behavior is apparent when using optional parameters.
```js
var re = pathToRegexp('/:prefix(apple-)?icon-:res(\\d+).png', keys)
// keys = [{ name: 'prefix', prefix: '', ... }, { name: 'res', prefix: '', ... }]
re.exec('/icon-76.png')
//=> ['/icon-76.png', undefined, '76']
```
#### Modified Parameters
##### Optional
Parameters can be suffixed with a question mark (`?`) to make the entire parameter optional. This will also make any prefixed path delimiter optional (`/` or `.`).
Parameters can be suffixed with a question mark (`?`) to make the parameter optional. This will also make the prefix optional.

@@ -80,3 +92,3 @@ ```js

Parameters can be suffixed with an asterisk (`*`) to denote a zero or more parameter match. The prefixed path delimiter is also taken into account for the match.
Parameters can be suffixed with an asterisk (`*`) to denote a zero or more parameter matches. The prefix is taken into account for each match.

@@ -96,3 +108,3 @@ ```js

Parameters can be suffixed with a plus sign (`+`) to denote a one or more parameters match. The prefixed path delimiter is included in the match.
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.

@@ -112,3 +124,3 @@ ```js

All parameters can be provided a custom matching regexp and override the default. Please note: Backslashes need to be escaped in strings.
All parameters can be provided a custom regexp, which overrides the default (`[^\/]+`).

@@ -126,9 +138,11 @@ ```js

**Please note:** Backslashes need to be escaped with another backslash in strings.
#### Unnamed Parameters
It is possible to write an unnamed parameter that is only a matching group. It works the same as a named parameter, except it will be numerically indexed.
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
var re = pathToRegexp('/:foo/(.*)', keys)
// keys = [{ name: 'foo', ... }, { name: '0', ... }]
// keys = [{ name: 'foo', ... }, { name: 0, ... }]

@@ -153,3 +167,3 @@ re.exec('/test/route')

The parse function is exposed via `pathToRegexp.parse`. This will yield an array of strings and keys.
The parse function is exposed via `pathToRegexp.parse`. This will return an array of strings and keys.

@@ -169,7 +183,7 @@ ```js

**Note:** This method only works with strings.
**Note:** This method only works with Express-style strings.
### Compile ("Reverse" Path-To-RegExp)
Path-To-RegExp exposes a compile function for transforming an express path into valid path. Confusing enough? This example will straighten everything out for you.
Path-To-RegExp exposes a compile function for transforming an Express-style path into a valid path.

@@ -181,4 +195,7 @@ ```js

toPath({ id: 'café' }) //=> "/user/caf%C3%A9"
toPath({ id: '/' }) //=> "%2F"
toPath({ id: '/' }) //=> "/user/%2F"
toPath({ id: ':' }) //=> "/user/%3A"
toPath({ id: ':' }, { pretty: true }) //=> "/user/:"
var toPathRepeated = pathToRegexp.compile('/:segment+')

@@ -193,6 +210,6 @@

toPathRegexp({ id: '123' }) //=> "/user/123"
toPathRegexp({ id: 'abc' }) //=> throws TypeError
toPathRegexp({ id: 'abc' }) //=> Throws `TypeError`.
```
**Note:** The generated function will throw on any invalid input. It will execute all necessary checks to ensure the generated path is valid. This method only works with strings.
**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.

@@ -216,2 +233,6 @@ ### Working with Tokens

## TypeScript
Includes a [`.d.ts`](index.d.ts) file for TypeScript users.
## Live Demo

@@ -218,0 +239,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc