Socket
Socket
Sign inDemoInstall

path-to-regexp

Package Overview
Dependencies
Maintainers
5
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 4.0.5 to 5.0.0

14

dist.es2015/index.js

@@ -6,10 +6,2 @@ /**

/**
* Normalize a pathname for matching, replaces multiple slashes with a single
* slash and normalizes unicode characters to "NFC". When using this method,
* `decode` should be an identity function so you don't decode strings twice.
*/
export function normalizePathname(pathname) {
return decodeURI(pathname).replace(/\/+/g, "/");
}
/**
* Balanced bracket helper function.

@@ -152,3 +144,3 @@ */

var reFlags = flags(options);
var _a = options.encode, encode = _a === void 0 ? encodeURIComponent : _a, _b = options.validate, validate = _b === void 0 ? true : _b;
var _a = options.encode, encode = _a === void 0 ? function (x) { return x; } : _a, _b = options.validate, validate = _b === void 0 ? true : _b;
// Compile all the tokens into regexps.

@@ -295,3 +287,3 @@ var matches = tokens.map(function (token) {

if (options === void 0) { options = {}; }
var strict = options.strict, _a = options.start, start = _a === void 0 ? true : _a, _b = options.end, end = _b === void 0 ? true : _b, _c = options.delimiter, delimiter = _c === void 0 ? DEFAULT_DELIMITER : _c;
var strict = options.strict, _a = options.start, start = _a === void 0 ? true : _a, _b = options.end, end = _b === void 0 ? true : _b, _c = options.delimiter, delimiter = _c === void 0 ? DEFAULT_DELIMITER : _c, _d = options.encode, encode = _d === void 0 ? function (x) { return x; } : _d;
var endsWith = (typeof options.endsWith === "string"

@@ -308,3 +300,3 @@ ? options.endsWith.split("")

if (typeof token === "string") {
route += escapeString(token);
route += escapeString(encode(token));
}

@@ -311,0 +303,0 @@ else {

@@ -12,8 +12,2 @@ export interface ParseOptions {

/**
* Normalize a pathname for matching, replaces multiple slashes with a single
* slash and normalizes unicode characters to "NFC". When using this method,
* `decode` should be an identity function so you don't decode strings twice.
*/
export declare function normalizePathname(pathname: string): string;
/**
* Parse a string for the raw tokens.

@@ -70,3 +64,3 @@ */

*/
export declare function match<P extends object = object>(str: Path, options?: ParseOptions & RegexpOptions & RegexpToFunctionOptions): MatchFunction<P>;
export declare function match<P extends object = object>(str: Path, options?: ParseOptions & TokensToRegexpOptions & RegexpToFunctionOptions): MatchFunction<P>;
/**

@@ -91,7 +85,3 @@ * Create a path match function from `path-to-regexp` output.

export declare type Token = string | Key;
/**
* Expose a function for taking tokens and returning a RegExp.
*/
export declare function tokensToRegexp(tokens: Token[], keys?: Key[], options?: RegexpOptions): RegExp;
export interface RegexpOptions {
export interface TokensToRegexpOptions {
/**

@@ -121,10 +111,12 @@ * When `true` the regexp will be case sensitive. (default: `false`)

endsWith?: string | string[];
}
export interface ParseOptions {
/**
* Set the default delimiter for repeat parameters. (default: `'/'`)
* Encode path tokens for use in the `RegExp`.
*/
delimiter?: string;
encode?: (value: string) => string;
}
/**
* Expose a function for taking tokens and returning a RegExp.
*/
export declare function tokensToRegexp(tokens: Token[], keys?: Key[], options?: TokensToRegexpOptions): RegExp;
/**
* Supported `path-to-regexp` input types.

@@ -140,2 +132,2 @@ */

*/
export declare function pathToRegexp(path: Path, keys?: Key[], options?: RegexpOptions & ParseOptions): RegExp;
export declare function pathToRegexp(path: Path, keys?: Key[], options?: TokensToRegexpOptions & ParseOptions): RegExp;

@@ -8,11 +8,2 @@ "use strict";

/**
* Normalize a pathname for matching, replaces multiple slashes with a single
* slash and normalizes unicode characters to "NFC". When using this method,
* `decode` should be an identity function so you don't decode strings twice.
*/
function normalizePathname(pathname) {
return decodeURI(pathname).replace(/\/+/g, "/");
}
exports.normalizePathname = normalizePathname;
/**
* Balanced bracket helper function.

@@ -157,3 +148,3 @@ */

var reFlags = flags(options);
var _a = options.encode, encode = _a === void 0 ? encodeURIComponent : _a, _b = options.validate, validate = _b === void 0 ? true : _b;
var _a = options.encode, encode = _a === void 0 ? function (x) { return x; } : _a, _b = options.validate, validate = _b === void 0 ? true : _b;
// Compile all the tokens into regexps.

@@ -303,3 +294,3 @@ var matches = tokens.map(function (token) {

if (options === void 0) { options = {}; }
var strict = options.strict, _a = options.start, start = _a === void 0 ? true : _a, _b = options.end, end = _b === void 0 ? true : _b, _c = options.delimiter, delimiter = _c === void 0 ? DEFAULT_DELIMITER : _c;
var strict = options.strict, _a = options.start, start = _a === void 0 ? true : _a, _b = options.end, end = _b === void 0 ? true : _b, _c = options.delimiter, delimiter = _c === void 0 ? DEFAULT_DELIMITER : _c, _d = options.encode, encode = _d === void 0 ? function (x) { return x; } : _d;
var endsWith = (typeof options.endsWith === "string"

@@ -316,3 +307,3 @@ ? options.endsWith.split("")

if (typeof token === "string") {
route += escapeString(token);
route += escapeString(encode(token));
}

@@ -319,0 +310,0 @@ else {

{
"name": "path-to-regexp",
"description": "Express style path to RegExp utility",
"version": "4.0.5",
"version": "5.0.0",
"main": "dist/index.js",

@@ -6,0 +6,0 @@ "typings": "dist/index.d.ts",

@@ -39,2 +39,3 @@ # Path-to-RegExp

- **whitelist** List of characters to consider delimiters when parsing. (default: `undefined`, any character)
- **encode** A function to encode strings before inserting into `RegExp`. (default: `x => x`)

@@ -160,15 +161,33 @@ ```javascript

```js
const match = match("/user/:id");
// Make sure you consistently `decode` segments.
const match = 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é' } }
```
### Normalize Pathname
#### Normalize Pathname
The `normalizePathname` function will return a normalized string for matching with `pathToRegexp`:
You should make sure variations of the same path to match your input `path`. Here's one possible solution:
```js
/**
* Normalize a pathname for matching, replaces multiple slashes with a single
* slash and normalizes unicode characters to "NFC". When using this method,
* `decode` should be an identity function so you don't decode strings twice.
*/
function normalizePathname(pathname: string) {
return (
decodeURI(pathname)
// Replaces repeated slashes in the URL.
.replace(/\/+/g, "/")
// Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize
// Note: Missing native IE support, may want to skip this step.
.normalize()
);
}
const re = pathToRegexp("/caf\u00E9");
const input = encodeURI("/caf\u00E9");
const input = encodeURI("/cafe\u0301");

@@ -179,6 +198,4 @@ re.test(input); //=> false

**Note:** It may be preferable to implement something in your own library that normalizes the pathname for matching. E.g. [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) automatically URI encodes paths for you, which would result in a consistent match.
**Note:** [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) automatically encodes pathnames for you, which would result in a consistent match if you use `encodeURI` in `pathToRegexp` options.
**Tip:** Consider using [`String.prototype.normalize`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize) to resolve unicode variants of the same string.
### Parse

@@ -208,3 +225,4 @@

```js
const toPath = compile("/user/:id");
// Make sure you encode your path segments consistently.
const toPath = compile("/user/:id", { encode: encodeURIComponent });

@@ -216,4 +234,9 @@ toPath({ id: 123 }); //=> "/user/123"

toPath({ id: ":/" }); //=> "/user/%3A%2F"
toPath({ id: ":/" }, { encode: (value, token) => value, validate: false }); //=> "/user/:/"
// Without `encode`, you need to make sure inputs are encoded correctly.
const toPathRaw = compile("/user/:id");
toPathRaw({ id: "%3A%2F" }); //=> "/user/%3A%2F"
toPathRaw({ id: ":/" }, { validate: false }); //=> "/user/:/"
const toPathRepeated = compile("/:segment+");

@@ -220,0 +243,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

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