path-to-regexp 3.1.0 → 3.2.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/Readme.md CHANGED
@@ -21,6 +21,7 @@ npm install path-to-regexp --save
21
21
  const pathToRegexp = require('path-to-regexp')
22
22
 
23
23
  // pathToRegexp(path, keys?, options?)
24
+ // pathToRegexp.match(path)
24
25
  // pathToRegexp.parse(path)
25
26
  // pathToRegexp.compile(path)
26
27
  ```
@@ -150,9 +151,20 @@ regexpWord.exec('/users')
150
151
 
151
152
  **Tip:** Backslashes need to be escaped with another backslash in JavaScript strings.
152
153
 
154
+ ### Match
155
+
156
+ The `match` function will return a function for transforming paths into parameters:
157
+
158
+ ```js
159
+ const match = pathToRegexp.match('/user/:id')
160
+
161
+ match('/user/123') //=> { path: '/user/123', index: 0, params: { id: '123' } }
162
+ match('/invalid') //=> false
163
+ ```
164
+
153
165
  ### Parse
154
166
 
155
- The parse function is exposed via `pathToRegexp.parse`. This will return an array of strings and keys.
167
+ The `parse` function will return a list of strings and keys from a path string:
156
168
 
157
169
  ```js
158
170
  const tokens = pathToRegexp.parse('/route/:foo/(.*)')
@@ -171,7 +183,7 @@ console.log(tokens[2])
171
183
 
172
184
  ### Compile ("Reverse" Path-To-RegExp)
173
185
 
174
- Path-To-RegExp exposes a compile function for transforming a string into a valid path.
186
+ The `compile` function will return a function for transforming parameters into a valid path:
175
187
 
176
188
  ```js
177
189
  const toPath = pathToRegexp.compile('/user/:id')
@@ -181,7 +193,7 @@ toPath({ id: 'café' }) //=> "/user/caf%C3%A9"
181
193
  toPath({ id: '/' }) //=> "/user/%2F"
182
194
 
183
195
  toPath({ id: ':/' }) //=> "/user/%3A%2F"
184
- toPath({ id: ':/' }, { encode: (value, token) => value }) //=> "/user/:/"
196
+ toPath({ id: ':/' }, { encode: (value, token) => value, validate: false }) //=> "/user/:/"
185
197
 
186
198
  const toPathRepeated = pathToRegexp.compile('/:segment+')
187
199
 
@@ -193,7 +205,7 @@ const toPathRegexp = pathToRegexp.compile('/user/:id(\\d+)')
193
205
  toPathRegexp({ id: 123 }) //=> "/user/123"
194
206
  toPathRegexp({ id: '123' }) //=> "/user/123"
195
207
  toPathRegexp({ id: 'abc' }) //=> Throws `TypeError`.
196
- toPathRegexp({ id: 'abc' }, { validate: true }) //=> "/user/abc"
208
+ toPathRegexp({ id: 'abc' }, { validate: false }) //=> "/user/abc"
197
209
  ```
198
210
 
199
211
  **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.
package/index.d.ts CHANGED
@@ -51,6 +51,16 @@ declare namespace pathToRegexp {
51
51
  */
52
52
  export function parse (path: string, options?: ParseOptions): Token[];
53
53
 
54
+ /**
55
+ * Create path match function from `path-to-regexp` spec.
56
+ */
57
+ export function match <P extends object = object> (path: string, options?: ParseOptions): MatchFunction<P>;
58
+
59
+ /**
60
+ * Create a path match function from `path-to-regexp` output.
61
+ */
62
+ export function regexpToFunction <P extends object = object> (re: RegExp, keys: Key[]): MatchFunction<P>;
63
+
54
64
  /**
55
65
  * Transforming an Express-style path into a valid path.
56
66
  */
@@ -86,9 +96,25 @@ declare namespace pathToRegexp {
86
96
  validate?: boolean;
87
97
  }
88
98
 
99
+ interface MatchFunctionOptions {
100
+ /**
101
+ * Function for decoding strings for params.
102
+ */
103
+ decode?: (value: string, token: Key) => string;
104
+ }
105
+
106
+ interface MatchResult <P extends object = object> {
107
+ path: string;
108
+ index: number;
109
+ params: P;
110
+ }
111
+
112
+ type Match <P extends object = object> = false | MatchResult<P>;
113
+
89
114
  export type Token = string | Key;
90
115
  export type Path = string | RegExp | Array<string | RegExp>;
91
116
  export type PathFunction <P extends object = object> = (data?: P, options?: PathFunctionOptions) => string;
117
+ export type MatchFunction <P extends object = object> = (path: string, options?: MatchFunctionOptions) => Match<P>;
92
118
  }
93
119
 
94
120
  export = pathToRegexp;
package/index.js CHANGED
@@ -2,6 +2,8 @@
2
2
  * Expose `pathToRegexp`.
3
3
  */
4
4
  module.exports = pathToRegexp
5
+ module.exports.match = match
6
+ module.exports.regexpToFunction = regexpToFunction
5
7
  module.exports.parse = parse
6
8
  module.exports.compile = compile
7
9
  module.exports.tokensToFunction = tokensToFunction
@@ -120,6 +122,46 @@ function compile (str, options) {
120
122
  return tokensToFunction(parse(str, options), options)
121
123
  }
122
124
 
125
+ /**
126
+ * Create path match function from `path-to-regexp` spec.
127
+ */
128
+ function match (str, options) {
129
+ var keys = []
130
+ var re = pathToRegexp(str, keys, options)
131
+ return regexpToFunction(re, keys)
132
+ }
133
+
134
+ /**
135
+ * Create a path match function from `path-to-regexp` output.
136
+ */
137
+ function regexpToFunction (re, keys) {
138
+ return function (pathname, options) {
139
+ var m = re.exec(pathname)
140
+ if (!m) return false
141
+
142
+ var path = m[0]
143
+ var index = m.index
144
+ var params = {}
145
+ var decode = (options && options.decode) || decodeURIComponent
146
+
147
+ for (var i = 1; i < m.length; i++) {
148
+ if (m[i] === undefined) continue
149
+
150
+ var key = keys[i - 1]
151
+
152
+ if (key.repeat) {
153
+ params[key.name] = m[i].split(key.delimiter).map(function (value) {
154
+ return decode(value, key)
155
+ })
156
+ } else {
157
+ params[key.name] = decode(m[i], key)
158
+ }
159
+ }
160
+
161
+ return { path: path, index: index, params: params }
162
+ }
163
+ }
164
+
123
165
  /**
124
166
  * Expose a method for transforming tokens into the path function.
125
167
  */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "path-to-regexp",
3
3
  "description": "Express style path to RegExp utility",
4
- "version": "3.1.0",
4
+ "version": "3.2.0",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
7
7
  "files": [
@@ -12,7 +12,8 @@
12
12
  "scripts": {
13
13
  "lint": "standard",
14
14
  "test-spec": "mocha --require ts-node/register -R spec --bail test.ts",
15
- "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --require ts-node/register -R spec test.ts",
15
+ "test-cov": "nyc --reporter=lcov mocha -- --require ts-node/register -R spec test.ts",
16
+ "coverage": "nyc report --reporter=text-lcov",
16
17
  "test": "npm run lint && npm run test-cov"
17
18
  },
18
19
  "keywords": [
@@ -36,10 +37,10 @@
36
37
  "@types/mocha": "^5.2.5",
37
38
  "@types/node": "^12.7.3",
38
39
  "chai": "^4.1.1",
39
- "istanbul": "^0.4.5",
40
40
  "mocha": "^6.2.0",
41
+ "nyc": "^14.1.1",
41
42
  "standard": "^14.1.0",
42
43
  "ts-node": "^8.3.0",
43
- "typescript": "^3.0.1"
44
+ "typescript": "^3.7.2"
44
45
  }
45
46
  }