path-to-regexp 2.1.0 → 2.4.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/History.md CHANGED
@@ -1,3 +1,24 @@
1
+ 2.3.0 / 2018-08-20
2
+ ==================
3
+
4
+ * Use `delimiter` when processing repeated matching groups (e.g. `foo/bar` has no prefix, but has a delimiter)
5
+
6
+ 2.2.1 / 2018-04-24
7
+ ==================
8
+
9
+ * Allow empty string with `end: false` to match both relative and absolute paths
10
+
11
+ 2.2.0 / 2018-03-06
12
+ ==================
13
+
14
+ * Pass `token` as second argument to `encode` option (e.g. `encode(value, token)`)
15
+
16
+ 2.1.0 / 2017-10-20
17
+ ==================
18
+
19
+ * Handle non-ending paths where the final character is a delimiter
20
+ * E.g. `/foo/` before required either `/foo/` or `/foo//` to match in non-ending mode
21
+
1
22
  2.0.0 / 2017-08-23
2
23
  ==================
3
24
 
package/Readme.md CHANGED
@@ -26,11 +26,12 @@ var pathToRegexp = require('path-to-regexp')
26
26
  ```
27
27
 
28
28
  - **path** A string, array of strings, or a regular expression.
29
- - **keys** An array to be populated with the keys found in the path.
29
+ - **keys** An array to be populated with keys found in the path.
30
30
  - **options**
31
- - **sensitive** When `true` the route will be case sensitive. (default: `false`)
32
- - **strict** When `false` the trailing slash is optional. (default: `false`)
33
- - **end** When `false` the path will match at the beginning. (default: `true`)
31
+ - **sensitive** When `true` the regexp will be case sensitive. (default: `false`)
32
+ - **strict** When `true` the regexp allows an optional trailing delimiter to match. (default: `false`)
33
+ - **end** When `true` the regexp will match to the end of the string. (default: `true`)
34
+ - **start** When `true` the regexp will match from the beginning of the string. (default: `true`)
34
35
  - Advanced options (use for non-pathname strings, e.g. host names):
35
36
  - **delimiter** The default delimiter for segments. (default: `'/'`)
36
37
  - **endsWith** Optional character, or list of characters, to treat as "end" characters.
@@ -61,15 +62,7 @@ re.exec('/test/route')
61
62
  //=> ['/test/route', 'test', 'route']
62
63
  ```
63
64
 
64
- **Please note:** Named parameters must be made up of "word characters" (`[A-Za-z0-9_]`).
65
-
66
- ```js
67
- var re = pathToRegexp('/(apple-)?icon-:res(\\d+).png')
68
- // keys = [{ name: 0, prefix: '/', ... }, { name: 'res', prefix: '', ... }]
69
-
70
- re.exec('/icon-76.png')
71
- //=> ['/icon-76.png', undefined, '76']
72
- ```
65
+ **Please note:** Parameter names must be made up of "word characters" (`[A-Za-z0-9_]`).
73
66
 
74
67
  #### Parameter Modifiers
75
68
 
@@ -120,18 +113,18 @@ re.exec('/bar/baz')
120
113
  //=> ['/bar/baz', 'bar/baz']
121
114
  ```
122
115
 
123
- #### Custom Match Parameters
116
+ #### Custom Matching Parameters
124
117
 
125
- All parameters can be provided a custom regexp, which overrides the default (`[^\/]+`).
118
+ All parameters can be provided a custom regexp, which overrides the default match (`[^\/]+`). For example, you can match digits in the path:
126
119
 
127
120
  ```js
128
- var re = pathToRegexp('/:foo(\\d+)')
121
+ var re = pathToRegexp('/icon-:foo(\\d+).png')
129
122
  // keys = [{ name: 'foo', ... }]
130
123
 
131
- re.exec('/123')
132
- //=> ['/123', '123']
124
+ re.exec('/icon-123.png')
125
+ //=> ['/icon-123.png', '123']
133
126
 
134
- re.exec('/abc')
127
+ re.exec('/icon-abc.png')
135
128
  //=> null
136
129
  ```
137
130
 
@@ -180,7 +173,7 @@ toPath({ id: 'café' }) //=> "/user/caf%C3%A9"
180
173
  toPath({ id: '/' }) //=> "/user/%2F"
181
174
 
182
175
  toPath({ id: ':/' }) //=> "/user/%3A%2F"
183
- toPath({ id: ':/' }, { encode: (x) => x }) //=> "/user/:/"
176
+ toPath({ id: ':/' }, { encode: (value, token) => value }) //=> "/user/:/"
184
177
 
185
178
  var toPathRepeated = pathToRegexp.compile('/:segment+')
186
179
 
@@ -200,7 +193,7 @@ toPathRegexp({ id: 'abc' }) //=> Throws `TypeError`.
200
193
 
201
194
  Path-To-RegExp exposes the two functions used internally that accept an array of tokens.
202
195
 
203
- * `pathToRegexp.tokensToRegExp(tokens, options)` Transform an array of tokens into a matching regular expression.
196
+ * `pathToRegexp.tokensToRegExp(tokens, keys?, options?)` Transform an array of tokens into a matching regular expression.
204
197
  * `pathToRegexp.tokensToFunction(tokens)` Transform an array of tokens into a path generator function.
205
198
 
206
199
  #### Token Information
package/index.d.ts CHANGED
@@ -3,17 +3,21 @@ declare function pathToRegexp (path: pathToRegexp.Path, keys?: pathToRegexp.Key[
3
3
  declare namespace pathToRegexp {
4
4
  export interface RegExpOptions {
5
5
  /**
6
- * When `true` the route will be case sensitive. (default: `false`)
6
+ * When `true` the regexp will be case sensitive. (default: `false`)
7
7
  */
8
8
  sensitive?: boolean;
9
9
  /**
10
- * When `false` the trailing slash is optional. (default: `false`)
10
+ * When `true` the regexp allows an optional trailing delimiter to match. (default: `false`)
11
11
  */
12
12
  strict?: boolean;
13
13
  /**
14
- * When `false` the path will match at the beginning. (default: `true`)
14
+ * When `true` the regexp will match to the end of the string. (default: `true`)
15
15
  */
16
16
  end?: boolean;
17
+ /**
18
+ * When `true` the regexp will match from the beginning of the string. (default: `true`)
19
+ */
20
+ start?: boolean;
17
21
  /**
18
22
  * Sets the final character for non-ending optimistic matches. (default: `/`)
19
23
  */
@@ -69,7 +73,7 @@ declare namespace pathToRegexp {
69
73
  /**
70
74
  * Function for encoding input strings for output.
71
75
  */
72
- encode?: (value: string) => string;
76
+ encode?: (value: string, token: Key) => string;
73
77
  }
74
78
 
75
79
  export type Token = string | Key;
package/index.js CHANGED
@@ -25,8 +25,8 @@ var PATH_REGEXP = new RegExp([
25
25
  // Match Express-style parameters and un-named parameters with a prefix
26
26
  // and optional suffixes. Matches appear as:
27
27
  //
28
- // "/:test(\\d+)?" => ["/", "test", "\d+", undefined, "?"]
29
- // "/route(\\d+)" => [undefined, undefined, undefined, "\d+", undefined]
28
+ // ":test(\\d+)?" => ["test", "\d+", undefined, "?"]
29
+ // "(\\d+)" => [undefined, undefined, "\d+", undefined]
30
30
  '(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?'
31
31
  ].join('|'), 'g')
32
32
 
@@ -161,7 +161,7 @@ function tokensToFunction (tokens) {
161
161
  }
162
162
 
163
163
  for (var j = 0; j < value.length; j++) {
164
- segment = encode(value[j])
164
+ segment = encode(value[j], token)
165
165
 
166
166
  if (!matches[i].test(segment)) {
167
167
  throw new TypeError('Expected all "' + token.name + '" to match "' + token.pattern + '"')
@@ -174,7 +174,7 @@ function tokensToFunction (tokens) {
174
174
  }
175
175
 
176
176
  if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
177
- segment = encode(String(value))
177
+ segment = encode(String(value), token)
178
178
 
179
179
  if (!matches[i].test(segment)) {
180
180
  throw new TypeError('Expected "' + token.name + '" to match "' + token.pattern + '", but got "' + segment + '"')
@@ -300,12 +300,13 @@ function tokensToRegExp (tokens, keys, options) {
300
300
  options = options || {}
301
301
 
302
302
  var strict = options.strict
303
+ var start = options.start !== false
303
304
  var end = options.end !== false
304
305
  var delimiter = escapeString(options.delimiter || DEFAULT_DELIMITER)
305
306
  var delimiters = options.delimiters || DEFAULT_DELIMITERS
306
307
  var endsWith = [].concat(options.endsWith || []).map(escapeString).concat('$').join('|')
307
- var route = ''
308
- var isEndDelimited = false
308
+ var route = start ? '^' : ''
309
+ var isEndDelimited = tokens.length === 0
309
310
 
310
311
  // Iterate over the tokens and create our regexp string.
311
312
  for (var i = 0; i < tokens.length; i++) {
@@ -315,21 +316,20 @@ function tokensToRegExp (tokens, keys, options) {
315
316
  route += escapeString(token)
316
317
  isEndDelimited = i === tokens.length - 1 && delimiters.indexOf(token[token.length - 1]) > -1
317
318
  } else {
318
- var prefix = escapeString(token.prefix)
319
319
  var capture = token.repeat
320
- ? '(?:' + token.pattern + ')(?:' + prefix + '(?:' + token.pattern + '))*'
320
+ ? '(?:' + token.pattern + ')(?:' + escapeString(token.delimiter) + '(?:' + token.pattern + '))*'
321
321
  : token.pattern
322
322
 
323
323
  if (keys) keys.push(token)
324
324
 
325
325
  if (token.optional) {
326
326
  if (token.partial) {
327
- route += prefix + '(' + capture + ')?'
327
+ route += escapeString(token.prefix) + '(' + capture + ')?'
328
328
  } else {
329
- route += '(?:' + prefix + '(' + capture + '))?'
329
+ route += '(?:' + escapeString(token.prefix) + '(' + capture + '))?'
330
330
  }
331
331
  } else {
332
- route += prefix + '(' + capture + ')'
332
+ route += escapeString(token.prefix) + '(' + capture + ')'
333
333
  }
334
334
  }
335
335
  }
@@ -343,7 +343,7 @@ function tokensToRegExp (tokens, keys, options) {
343
343
  if (!isEndDelimited) route += '(?=' + delimiter + '|' + endsWith + ')'
344
344
  }
345
345
 
346
- return new RegExp('^' + route, flags(options))
346
+ return new RegExp(route, flags(options))
347
347
  }
348
348
 
349
349
  /**
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": "2.1.0",
4
+ "version": "2.4.0",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
7
7
  "files": [
@@ -33,13 +33,13 @@
33
33
  },
34
34
  "devDependencies": {
35
35
  "@types/chai": "^4.0.4",
36
- "@types/mocha": "^2.2.42",
37
- "@types/node": "^8.0.24",
36
+ "@types/mocha": "^5.2.5",
37
+ "@types/node": "^10.7.1",
38
38
  "chai": "^4.1.1",
39
39
  "istanbul": "^0.4.5",
40
- "mocha": "^3.5.0",
41
- "standard": "^10.0.3",
42
- "ts-node": "^3.3.0",
43
- "typescript": "^2.4.2"
40
+ "mocha": "^5.2.0",
41
+ "standard": "^11.0.1",
42
+ "ts-node": "^7.0.1",
43
+ "typescript": "^3.0.1"
44
44
  }
45
45
  }