path-to-regexp
Advanced tools
Comparing version 6.2.0 to 6.2.1
@@ -45,3 +45,3 @@ /** | ||
if (!name) | ||
throw new TypeError("Missing parameter name at " + i); | ||
throw new TypeError("Missing parameter name at ".concat(i)); | ||
tokens.push({ type: "NAME", index: i, value: name }); | ||
@@ -56,3 +56,3 @@ i = j; | ||
if (str[j] === "?") { | ||
throw new TypeError("Pattern cannot start with \"?\" at " + j); | ||
throw new TypeError("Pattern cannot start with \"?\" at ".concat(j)); | ||
} | ||
@@ -74,3 +74,3 @@ while (j < str.length) { | ||
if (str[j + 1] !== "?") { | ||
throw new TypeError("Capturing groups are not allowed at " + j); | ||
throw new TypeError("Capturing groups are not allowed at ".concat(j)); | ||
} | ||
@@ -81,5 +81,5 @@ } | ||
if (count) | ||
throw new TypeError("Unbalanced pattern at " + i); | ||
throw new TypeError("Unbalanced pattern at ".concat(i)); | ||
if (!pattern) | ||
throw new TypeError("Missing pattern at " + i); | ||
throw new TypeError("Missing pattern at ".concat(i)); | ||
tokens.push({ type: "PATTERN", index: i, value: pattern }); | ||
@@ -101,3 +101,3 @@ i = j; | ||
var _a = options.prefixes, prefixes = _a === void 0 ? "./" : _a; | ||
var defaultPattern = "[^" + escapeString(options.delimiter || "/#?") + "]+?"; | ||
var defaultPattern = "[^".concat(escapeString(options.delimiter || "/#?"), "]+?"); | ||
var result = []; | ||
@@ -116,3 +116,3 @@ var key = 0; | ||
var _a = tokens[i], nextType = _a.type, index = _a.index; | ||
throw new TypeError("Unexpected " + nextType + " at " + index + ", expected " + type); | ||
throw new TypeError("Unexpected ".concat(nextType, " at ").concat(index, ", expected ").concat(type)); | ||
}; | ||
@@ -122,3 +122,2 @@ var consumeText = function () { | ||
var value; | ||
// tslint:disable-next-line | ||
while ((value = tryConsume("CHAR") || tryConsume("ESCAPED_CHAR"))) { | ||
@@ -148,3 +147,3 @@ result += value; | ||
pattern: pattern || defaultPattern, | ||
modifier: tryConsume("MODIFIER") || "" | ||
modifier: tryConsume("MODIFIER") || "", | ||
}); | ||
@@ -174,3 +173,3 @@ continue; | ||
suffix: suffix, | ||
modifier: tryConsume("MODIFIER") || "" | ||
modifier: tryConsume("MODIFIER") || "", | ||
}); | ||
@@ -199,3 +198,3 @@ continue; | ||
if (typeof token === "object") { | ||
return new RegExp("^(?:" + token.pattern + ")$", reFlags); | ||
return new RegExp("^(?:".concat(token.pattern, ")$"), reFlags); | ||
} | ||
@@ -216,3 +215,3 @@ }); | ||
if (!repeat) { | ||
throw new TypeError("Expected \"" + token.name + "\" to not repeat, but got an array"); | ||
throw new TypeError("Expected \"".concat(token.name, "\" to not repeat, but got an array")); | ||
} | ||
@@ -222,3 +221,3 @@ if (value.length === 0) { | ||
continue; | ||
throw new TypeError("Expected \"" + token.name + "\" to not be empty"); | ||
throw new TypeError("Expected \"".concat(token.name, "\" to not be empty")); | ||
} | ||
@@ -228,3 +227,3 @@ for (var j = 0; j < value.length; j++) { | ||
if (validate && !matches[i].test(segment)) { | ||
throw new TypeError("Expected all \"" + token.name + "\" to match \"" + token.pattern + "\", but got \"" + segment + "\""); | ||
throw new TypeError("Expected all \"".concat(token.name, "\" to match \"").concat(token.pattern, "\", but got \"").concat(segment, "\"")); | ||
} | ||
@@ -238,3 +237,3 @@ path += token.prefix + segment + token.suffix; | ||
if (validate && !matches[i].test(segment)) { | ||
throw new TypeError("Expected \"" + token.name + "\" to match \"" + token.pattern + "\", but got \"" + segment + "\""); | ||
throw new TypeError("Expected \"".concat(token.name, "\" to match \"").concat(token.pattern, "\", but got \"").concat(segment, "\"")); | ||
} | ||
@@ -247,3 +246,3 @@ path += token.prefix + segment + token.suffix; | ||
var typeOfMessage = repeat ? "an array" : "a string"; | ||
throw new TypeError("Expected \"" + token.name + "\" to be " + typeOfMessage); | ||
throw new TypeError("Expected \"".concat(token.name, "\" to be ").concat(typeOfMessage)); | ||
} | ||
@@ -274,3 +273,2 @@ return path; | ||
var _loop_1 = function (i) { | ||
// tslint:disable-next-line | ||
if (m[i] === undefined) | ||
@@ -322,3 +320,3 @@ return "continue"; | ||
modifier: "", | ||
pattern: "" | ||
pattern: "", | ||
}); | ||
@@ -334,3 +332,3 @@ execResult = groupsRegex.exec(path.source); | ||
var parts = paths.map(function (path) { return pathToRegexp(path, keys, options).source; }); | ||
return new RegExp("(?:" + parts.join("|") + ")", flags(options)); | ||
return new RegExp("(?:".concat(parts.join("|"), ")"), flags(options)); | ||
} | ||
@@ -348,5 +346,5 @@ /** | ||
if (options === void 0) { options = {}; } | ||
var _a = options.strict, strict = _a === void 0 ? false : _a, _b = options.start, start = _b === void 0 ? true : _b, _c = options.end, end = _c === void 0 ? true : _c, _d = options.encode, encode = _d === void 0 ? function (x) { return x; } : _d; | ||
var endsWith = "[" + escapeString(options.endsWith || "") + "]|$"; | ||
var delimiter = "[" + escapeString(options.delimiter || "/#?") + "]"; | ||
var _a = options.strict, strict = _a === void 0 ? false : _a, _b = options.start, start = _b === void 0 ? true : _b, _c = options.end, end = _c === void 0 ? true : _c, _d = options.encode, encode = _d === void 0 ? function (x) { return x; } : _d, _e = options.delimiter, delimiter = _e === void 0 ? "/#?" : _e, _f = options.endsWith, endsWith = _f === void 0 ? "" : _f; | ||
var endsWithRe = "[".concat(escapeString(endsWith), "]|$"); | ||
var delimiterRe = "[".concat(escapeString(delimiter), "]"); | ||
var route = start ? "^" : ""; | ||
@@ -368,14 +366,19 @@ // Iterate over the tokens and create our regexp string. | ||
var mod = token.modifier === "*" ? "?" : ""; | ||
route += "(?:" + prefix + "((?:" + token.pattern + ")(?:" + suffix + prefix + "(?:" + token.pattern + "))*)" + suffix + ")" + mod; | ||
route += "(?:".concat(prefix, "((?:").concat(token.pattern, ")(?:").concat(suffix).concat(prefix, "(?:").concat(token.pattern, "))*)").concat(suffix, ")").concat(mod); | ||
} | ||
else { | ||
route += "(?:" + prefix + "(" + token.pattern + ")" + suffix + ")" + token.modifier; | ||
route += "(?:".concat(prefix, "(").concat(token.pattern, ")").concat(suffix, ")").concat(token.modifier); | ||
} | ||
} | ||
else { | ||
route += "(" + token.pattern + ")" + token.modifier; | ||
if (token.modifier === "+" || token.modifier === "*") { | ||
route += "((?:".concat(token.pattern, ")").concat(token.modifier, ")"); | ||
} | ||
else { | ||
route += "(".concat(token.pattern, ")").concat(token.modifier); | ||
} | ||
} | ||
} | ||
else { | ||
route += "(?:" + prefix + suffix + ")" + token.modifier; | ||
route += "(?:".concat(prefix).concat(suffix, ")").concat(token.modifier); | ||
} | ||
@@ -386,4 +389,4 @@ } | ||
if (!strict) | ||
route += delimiter + "?"; | ||
route += !options.endsWith ? "$" : "(?=" + endsWith + ")"; | ||
route += "".concat(delimiterRe, "?"); | ||
route += !options.endsWith ? "$" : "(?=".concat(endsWithRe, ")"); | ||
} | ||
@@ -393,10 +396,9 @@ else { | ||
var isEndDelimited = typeof endToken === "string" | ||
? delimiter.indexOf(endToken[endToken.length - 1]) > -1 | ||
: // tslint:disable-next-line | ||
endToken === undefined; | ||
? delimiterRe.indexOf(endToken[endToken.length - 1]) > -1 | ||
: endToken === undefined; | ||
if (!strict) { | ||
route += "(?:" + delimiter + "(?=" + endsWith + "))?"; | ||
route += "(?:".concat(delimiterRe, "(?=").concat(endsWithRe, "))?"); | ||
} | ||
if (!isEndDelimited) { | ||
route += "(?=" + delimiter + "|" + endsWith + ")"; | ||
route += "(?=".concat(delimiterRe, "|").concat(endsWithRe, ")"); | ||
} | ||
@@ -403,0 +405,0 @@ } |
@@ -48,3 +48,3 @@ "use strict"; | ||
if (!name) | ||
throw new TypeError("Missing parameter name at " + i); | ||
throw new TypeError("Missing parameter name at ".concat(i)); | ||
tokens.push({ type: "NAME", index: i, value: name }); | ||
@@ -59,3 +59,3 @@ i = j; | ||
if (str[j] === "?") { | ||
throw new TypeError("Pattern cannot start with \"?\" at " + j); | ||
throw new TypeError("Pattern cannot start with \"?\" at ".concat(j)); | ||
} | ||
@@ -77,3 +77,3 @@ while (j < str.length) { | ||
if (str[j + 1] !== "?") { | ||
throw new TypeError("Capturing groups are not allowed at " + j); | ||
throw new TypeError("Capturing groups are not allowed at ".concat(j)); | ||
} | ||
@@ -84,5 +84,5 @@ } | ||
if (count) | ||
throw new TypeError("Unbalanced pattern at " + i); | ||
throw new TypeError("Unbalanced pattern at ".concat(i)); | ||
if (!pattern) | ||
throw new TypeError("Missing pattern at " + i); | ||
throw new TypeError("Missing pattern at ".concat(i)); | ||
tokens.push({ type: "PATTERN", index: i, value: pattern }); | ||
@@ -104,3 +104,3 @@ i = j; | ||
var _a = options.prefixes, prefixes = _a === void 0 ? "./" : _a; | ||
var defaultPattern = "[^" + escapeString(options.delimiter || "/#?") + "]+?"; | ||
var defaultPattern = "[^".concat(escapeString(options.delimiter || "/#?"), "]+?"); | ||
var result = []; | ||
@@ -119,3 +119,3 @@ var key = 0; | ||
var _a = tokens[i], nextType = _a.type, index = _a.index; | ||
throw new TypeError("Unexpected " + nextType + " at " + index + ", expected " + type); | ||
throw new TypeError("Unexpected ".concat(nextType, " at ").concat(index, ", expected ").concat(type)); | ||
}; | ||
@@ -125,3 +125,2 @@ var consumeText = function () { | ||
var value; | ||
// tslint:disable-next-line | ||
while ((value = tryConsume("CHAR") || tryConsume("ESCAPED_CHAR"))) { | ||
@@ -151,3 +150,3 @@ result += value; | ||
pattern: pattern || defaultPattern, | ||
modifier: tryConsume("MODIFIER") || "" | ||
modifier: tryConsume("MODIFIER") || "", | ||
}); | ||
@@ -177,3 +176,3 @@ continue; | ||
suffix: suffix, | ||
modifier: tryConsume("MODIFIER") || "" | ||
modifier: tryConsume("MODIFIER") || "", | ||
}); | ||
@@ -204,3 +203,3 @@ continue; | ||
if (typeof token === "object") { | ||
return new RegExp("^(?:" + token.pattern + ")$", reFlags); | ||
return new RegExp("^(?:".concat(token.pattern, ")$"), reFlags); | ||
} | ||
@@ -221,3 +220,3 @@ }); | ||
if (!repeat) { | ||
throw new TypeError("Expected \"" + token.name + "\" to not repeat, but got an array"); | ||
throw new TypeError("Expected \"".concat(token.name, "\" to not repeat, but got an array")); | ||
} | ||
@@ -227,3 +226,3 @@ if (value.length === 0) { | ||
continue; | ||
throw new TypeError("Expected \"" + token.name + "\" to not be empty"); | ||
throw new TypeError("Expected \"".concat(token.name, "\" to not be empty")); | ||
} | ||
@@ -233,3 +232,3 @@ for (var j = 0; j < value.length; j++) { | ||
if (validate && !matches[i].test(segment)) { | ||
throw new TypeError("Expected all \"" + token.name + "\" to match \"" + token.pattern + "\", but got \"" + segment + "\""); | ||
throw new TypeError("Expected all \"".concat(token.name, "\" to match \"").concat(token.pattern, "\", but got \"").concat(segment, "\"")); | ||
} | ||
@@ -243,3 +242,3 @@ path += token.prefix + segment + token.suffix; | ||
if (validate && !matches[i].test(segment)) { | ||
throw new TypeError("Expected \"" + token.name + "\" to match \"" + token.pattern + "\", but got \"" + segment + "\""); | ||
throw new TypeError("Expected \"".concat(token.name, "\" to match \"").concat(token.pattern, "\", but got \"").concat(segment, "\"")); | ||
} | ||
@@ -252,3 +251,3 @@ path += token.prefix + segment + token.suffix; | ||
var typeOfMessage = repeat ? "an array" : "a string"; | ||
throw new TypeError("Expected \"" + token.name + "\" to be " + typeOfMessage); | ||
throw new TypeError("Expected \"".concat(token.name, "\" to be ").concat(typeOfMessage)); | ||
} | ||
@@ -281,3 +280,2 @@ return path; | ||
var _loop_1 = function (i) { | ||
// tslint:disable-next-line | ||
if (m[i] === undefined) | ||
@@ -330,3 +328,3 @@ return "continue"; | ||
modifier: "", | ||
pattern: "" | ||
pattern: "", | ||
}); | ||
@@ -342,3 +340,3 @@ execResult = groupsRegex.exec(path.source); | ||
var parts = paths.map(function (path) { return pathToRegexp(path, keys, options).source; }); | ||
return new RegExp("(?:" + parts.join("|") + ")", flags(options)); | ||
return new RegExp("(?:".concat(parts.join("|"), ")"), flags(options)); | ||
} | ||
@@ -356,5 +354,5 @@ /** | ||
if (options === void 0) { options = {}; } | ||
var _a = options.strict, strict = _a === void 0 ? false : _a, _b = options.start, start = _b === void 0 ? true : _b, _c = options.end, end = _c === void 0 ? true : _c, _d = options.encode, encode = _d === void 0 ? function (x) { return x; } : _d; | ||
var endsWith = "[" + escapeString(options.endsWith || "") + "]|$"; | ||
var delimiter = "[" + escapeString(options.delimiter || "/#?") + "]"; | ||
var _a = options.strict, strict = _a === void 0 ? false : _a, _b = options.start, start = _b === void 0 ? true : _b, _c = options.end, end = _c === void 0 ? true : _c, _d = options.encode, encode = _d === void 0 ? function (x) { return x; } : _d, _e = options.delimiter, delimiter = _e === void 0 ? "/#?" : _e, _f = options.endsWith, endsWith = _f === void 0 ? "" : _f; | ||
var endsWithRe = "[".concat(escapeString(endsWith), "]|$"); | ||
var delimiterRe = "[".concat(escapeString(delimiter), "]"); | ||
var route = start ? "^" : ""; | ||
@@ -376,14 +374,19 @@ // Iterate over the tokens and create our regexp string. | ||
var mod = token.modifier === "*" ? "?" : ""; | ||
route += "(?:" + prefix + "((?:" + token.pattern + ")(?:" + suffix + prefix + "(?:" + token.pattern + "))*)" + suffix + ")" + mod; | ||
route += "(?:".concat(prefix, "((?:").concat(token.pattern, ")(?:").concat(suffix).concat(prefix, "(?:").concat(token.pattern, "))*)").concat(suffix, ")").concat(mod); | ||
} | ||
else { | ||
route += "(?:" + prefix + "(" + token.pattern + ")" + suffix + ")" + token.modifier; | ||
route += "(?:".concat(prefix, "(").concat(token.pattern, ")").concat(suffix, ")").concat(token.modifier); | ||
} | ||
} | ||
else { | ||
route += "(" + token.pattern + ")" + token.modifier; | ||
if (token.modifier === "+" || token.modifier === "*") { | ||
route += "((?:".concat(token.pattern, ")").concat(token.modifier, ")"); | ||
} | ||
else { | ||
route += "(".concat(token.pattern, ")").concat(token.modifier); | ||
} | ||
} | ||
} | ||
else { | ||
route += "(?:" + prefix + suffix + ")" + token.modifier; | ||
route += "(?:".concat(prefix).concat(suffix, ")").concat(token.modifier); | ||
} | ||
@@ -394,4 +397,4 @@ } | ||
if (!strict) | ||
route += delimiter + "?"; | ||
route += !options.endsWith ? "$" : "(?=" + endsWith + ")"; | ||
route += "".concat(delimiterRe, "?"); | ||
route += !options.endsWith ? "$" : "(?=".concat(endsWithRe, ")"); | ||
} | ||
@@ -401,10 +404,9 @@ else { | ||
var isEndDelimited = typeof endToken === "string" | ||
? delimiter.indexOf(endToken[endToken.length - 1]) > -1 | ||
: // tslint:disable-next-line | ||
endToken === undefined; | ||
? delimiterRe.indexOf(endToken[endToken.length - 1]) > -1 | ||
: endToken === undefined; | ||
if (!strict) { | ||
route += "(?:" + delimiter + "(?=" + endsWith + "))?"; | ||
route += "(?:".concat(delimiterRe, "(?=").concat(endsWithRe, "))?"); | ||
} | ||
if (!isEndDelimited) { | ||
route += "(?=" + delimiter + "|" + endsWith + ")"; | ||
route += "(?=".concat(delimiterRe, "|").concat(endsWithRe, ")"); | ||
} | ||
@@ -411,0 +413,0 @@ } |
102
package.json
{ | ||
"name": "path-to-regexp", | ||
"version": "6.2.1", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"description": "Express style path to RegExp utility", | ||
"version": "6.2.0", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/pillarjs/path-to-regexp.git" | ||
}, | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts", | ||
"module": "dist.es2015/index.js", | ||
"sideEffects": false, | ||
"files": [ | ||
"dist/", | ||
"dist.es2015/" | ||
], | ||
"scripts": { | ||
"prettier": "prettier --write", | ||
"lint": "tslint \"src/**/*\" --project tsconfig.json", | ||
"format": "npm run prettier -- \"{.,src/**}/*.{js,jsx,ts,tsx,json,md,yml,yaml}\"", | ||
"build": "rimraf dist/ dist.es2015/ && tsc && tsc -P tsconfig.es2015.json", | ||
"specs": "jest --coverage", | ||
"test": "npm run build && npm run lint && npm run specs && npm run size", | ||
"build": "ts-scripts build", | ||
"format": "ts-scripts format", | ||
"lint": "ts-scripts lint", | ||
"prepare": "ts-scripts install && npm run build", | ||
"size": "size-limit", | ||
"prepare": "npm run build" | ||
"specs": "ts-scripts specs", | ||
"test": "ts-scripts test && npm run size" | ||
}, | ||
"files": [ | ||
"dist.es2015/", | ||
"dist/" | ||
], | ||
"keywords": [ | ||
@@ -29,59 +34,30 @@ "express", | ||
], | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/pillarjs/path-to-regexp.git" | ||
"devDependencies": { | ||
"@borderless/ts-scripts": "^0.8.0", | ||
"@size-limit/preset-small-lib": "^7.0.8", | ||
"@types/jest": "^27.4.0", | ||
"@types/node": "^17.0.17", | ||
"@types/semver": "^7.3.1", | ||
"semver": "^7.3.5", | ||
"size-limit": "^7.0.8", | ||
"typescript": "^4.5.5" | ||
}, | ||
"typings": "dist/index.d.ts", | ||
"sideEffects": false, | ||
"size-limit": [ | ||
{ | ||
"path": "dist/index.js", | ||
"limit": "2 kB" | ||
"path": "dist.es2015/index.js", | ||
"limit": "2.1 kB" | ||
} | ||
], | ||
"jest": { | ||
"roots": [ | ||
"<rootDir>/src/" | ||
"ts-scripts": { | ||
"dist": [ | ||
"dist", | ||
"dist.es2015" | ||
], | ||
"transform": { | ||
"\\.tsx?$": "ts-jest" | ||
}, | ||
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$", | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"tsx", | ||
"js", | ||
"jsx", | ||
"json", | ||
"node" | ||
"project": [ | ||
"tsconfig.build.json", | ||
"tsconfig.es2015.json" | ||
] | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
}, | ||
"lint-staged": { | ||
"*.{js,jsx,ts,tsx,json,md,yml,yaml}": "npm run prettier" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"devDependencies": { | ||
"@size-limit/preset-small-lib": "^4.5.6", | ||
"@types/jest": "^26.0.9", | ||
"@types/node": "^14.0.27", | ||
"@types/semver": "^7.3.1", | ||
"husky": "^4.2.5", | ||
"jest": "^26.2.2", | ||
"lint-staged": "^10.2.11", | ||
"prettier": "^2.0.5", | ||
"rimraf": "^3.0.0", | ||
"semver": "^7.3.2", | ||
"size-limit": "^4.5.6", | ||
"ts-jest": "^26.1.4", | ||
"tslint": "^6.1.3", | ||
"tslint-config-prettier": "^1.18.0", | ||
"tslint-config-standard": "^9.0.0", | ||
"typescript": "^4.0.3" | ||
} | ||
} |
@@ -6,7 +6,6 @@ # Path-to-RegExp | ||
[![NPM version][npm-image]][npm-url] | ||
[![Build status][travis-image]][travis-url] | ||
[![Test coverage][coveralls-image]][coveralls-url] | ||
[![Dependency Status][david-image]][david-url] | ||
[![NPM downloads][downloads-image]][downloads-url] | ||
[![Build status][build-image]][build-url] | ||
[![Build coverage][coverage-image]][coverage-url] | ||
[![License][license-image]][license-url] | ||
[![Downloads][downloads-image]][downloads-url] | ||
@@ -30,5 +29,9 @@ ## Installation | ||
### Path to regexp | ||
The `pathToRegexp` function will return a regular expression object based on the provided `path` argument. It accepts the following arguments: | ||
- **path** A string, array of strings, or a regular expression. | ||
- **keys** An array to populate with keys found in the path. | ||
- **options** | ||
- **keys** _(optional)_ An array to populate with keys found in the path. | ||
- **options** _(optional)_ | ||
- **sensitive** When `true` the regexp will be case sensitive. (default: `false`) | ||
@@ -192,9 +195,26 @@ - **strict** When `true` the regexp won't allow an optional trailing delimiter to match. (default: `false`) | ||
// Make sure you consistently `decode` segments. | ||
const match = match("/user/:id", { decode: decodeURIComponent }); | ||
const fn = match("/user/:id", { decode: decodeURIComponent }); | ||
match("/user/123"); //=> { path: '/user/123', index: 0, params: { id: '123' } } | ||
match("/invalid"); //=> false | ||
match("/user/caf%C3%A9"); //=> { path: '/user/caf%C3%A9', index: 0, params: { id: 'café' } } | ||
fn("/user/123"); //=> { path: '/user/123', index: 0, params: { id: '123' } } | ||
fn("/invalid"); //=> false | ||
fn("/user/caf%C3%A9"); //=> { path: '/user/caf%C3%A9', index: 0, params: { id: 'café' } } | ||
``` | ||
The `match` function can be used to custom match named parameters. For example, this can be used to whitelist a small number of valid paths: | ||
```js | ||
const urlMatch = match("/users/:id/:tab(home|photos|bio)", { | ||
decode: decodeURIComponent, | ||
}); | ||
urlMatch("/users/1234/photos"); | ||
//=> { path: '/users/1234/photos', index: 0, params: { id: '1234', tab: 'photos' } } | ||
urlMatch("/users/1234/bio"); | ||
//=> { path: '/users/1234/bio', index: 0, params: { id: '1234', tab: 'bio' } } | ||
urlMatch("/users/1234/otherstuff"); | ||
//=> false | ||
``` | ||
#### Process Pathname | ||
@@ -205,12 +225,12 @@ | ||
```js | ||
const match = match("/café", { encode: encodeURI, decode: decodeURIComponent }); | ||
const fn = match("/café", { encode: encodeURI }); | ||
match("/user/caf%C3%A9"); //=> { path: '/user/caf%C3%A9', index: 0, params: { id: 'café' } } | ||
fn("/caf%C3%A9"); //=> { path: '/caf%C3%A9', index: 0, params: {} } | ||
``` | ||
**Note:** [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) automatically encodes pathnames for you. | ||
**Note:** [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) encodes paths, so `/café` would be normalized to `/caf%C3%A9` and match in the above example. | ||
##### Alternative Using Normalize | ||
Sometimes you won't have an already normalized pathname. You can normalize it yourself before processing: | ||
Sometimes you won't have already normalized paths to use, so you could normalize it yourself before matching: | ||
@@ -322,3 +342,3 @@ ```js | ||
You can see a live demo of this library in use at [express-route-tester](http://forbeslindesay.github.com/express-route-tester/). | ||
You can see a live demo of this library in use at [express-route-tester](http://forbeslindesay.github.io/express-route-tester/). | ||
@@ -329,13 +349,11 @@ ## License | ||
[npm-image]: https://img.shields.io/npm/v/path-to-regexp.svg?style=flat | ||
[npm-image]: https://img.shields.io/npm/v/path-to-regexp | ||
[npm-url]: https://npmjs.org/package/path-to-regexp | ||
[travis-image]: https://img.shields.io/travis/pillarjs/path-to-regexp.svg?style=flat | ||
[travis-url]: https://travis-ci.org/pillarjs/path-to-regexp | ||
[coveralls-image]: https://img.shields.io/coveralls/pillarjs/path-to-regexp.svg?style=flat | ||
[coveralls-url]: https://coveralls.io/r/pillarjs/path-to-regexp?branch=master | ||
[david-image]: http://img.shields.io/david/pillarjs/path-to-regexp.svg?style=flat | ||
[david-url]: https://david-dm.org/pillarjs/path-to-regexp | ||
[downloads-image]: https://img.shields.io/npm/dm/path-to-regexp | ||
[downloads-url]: https://npmjs.org/package/path-to-regexp | ||
[build-image]: https://img.shields.io/github/workflow/status/pillarjs/path-to-regexp/CI/master | ||
[build-url]: https://github.com/pillarjs/path-to-regexp/actions/workflows/ci.yml?query=branch%3Amaster | ||
[coverage-image]: https://img.shields.io/codecov/c/gh/pillarjs/path-to-regexp | ||
[coverage-url]: https://codecov.io/gh/pillarjs/path-to-regexp | ||
[license-image]: http://img.shields.io/npm/l/path-to-regexp.svg?style=flat | ||
[license-url]: LICENSE.md | ||
[downloads-image]: http://img.shields.io/npm/dm/path-to-regexp.svg?style=flat | ||
[downloads-url]: https://npmjs.org/package/path-to-regexp |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
8
937
354
0
107798
8