path-to-regexp 0.1.9 → 0.1.11

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.

Potentially problematic release.


This version of path-to-regexp might be problematic. Click here for more details.

Files changed (2) hide show
  1. package/index.js +55 -39
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -1,13 +1,13 @@
1
1
  /**
2
- * Expose `pathtoRegexp`.
2
+ * Expose `pathToRegexp`.
3
3
  */
4
4
 
5
- module.exports = pathtoRegexp;
5
+ module.exports = pathToRegexp;
6
6
 
7
7
  /**
8
8
  * Match matching groups in a regular expression.
9
9
  */
10
- var MATCHING_GROUP_REGEXP = /\((?:\?<(.*?)>)?(?!\?)/g;
10
+ var MATCHING_GROUP_REGEXP = /\\.|\((?:\?<(.*?)>)?(?!\?)/g;
11
11
 
12
12
  /**
13
13
  * Normalize the given path string,
@@ -25,7 +25,7 @@ var MATCHING_GROUP_REGEXP = /\((?:\?<(.*?)>)?(?!\?)/g;
25
25
  * @api private
26
26
  */
27
27
 
28
- function pathtoRegexp(path, keys, options) {
28
+ function pathToRegexp(path, keys, options) {
29
29
  options = options || {};
30
30
  keys = keys || [];
31
31
  var strict = options.strict;
@@ -36,10 +36,14 @@ function pathtoRegexp(path, keys, options) {
36
36
  var keysOffset = keys.length;
37
37
  var i = 0;
38
38
  var name = 0;
39
+ var pos = 0;
40
+ var backtrack = '';
39
41
  var m;
40
42
 
41
43
  if (path instanceof RegExp) {
42
44
  while (m = MATCHING_GROUP_REGEXP.exec(path.source)) {
45
+ if (m[0][0] === '\\') continue;
46
+
43
47
  keys.push({
44
48
  name: m[1] || name++,
45
49
  optional: false,
@@ -55,20 +59,51 @@ function pathtoRegexp(path, keys, options) {
55
59
  // the same keys and options instance into every generation to get
56
60
  // consistent matching groups before we join the sources together.
57
61
  path = path.map(function (value) {
58
- return pathtoRegexp(value, keys, options).source;
62
+ return pathToRegexp(value, keys, options).source;
59
63
  });
60
64
 
61
- return new RegExp('(?:' + path.join('|') + ')', flags);
65
+ return new RegExp(path.join('|'), flags);
66
+ }
67
+
68
+ if (typeof path !== 'string') {
69
+ throw new TypeError('path must be a string, array of strings, or regular expression');
62
70
  }
63
71
 
64
- path = ('^' + path + (strict ? '' : path[path.length - 1] === '/' ? '?' : '/?'))
65
- .replace(/\/\(/g, '/(?:')
66
- .replace(/([\/\.])/g, '\\$1')
67
- .replace(/(\\\/)?(\\\.)?:(\w+)(\(.*?\))?(\*)?(\?)?/g, function (match, slash, format, key, capture, star, optional, offset) {
72
+ path = path.replace(
73
+ /\\.|(\/)?(\.)?:(\w+)(\(.*?\))?(\*)?(\?)?|[.*]|\/\(/g,
74
+ function (match, slash, format, key, capture, star, optional, offset) {
75
+ pos = offset + match.length;
76
+
77
+ if (match[0] === '\\') {
78
+ backtrack += match;
79
+ return match;
80
+ }
81
+
82
+ if (match === '.') {
83
+ backtrack += '\\.';
84
+ extraOffset += 1;
85
+ return '\\.';
86
+ }
87
+
88
+ backtrack = slash || format ? '' : path.slice(pos, offset);
89
+
90
+ if (match === '*') {
91
+ extraOffset += 3;
92
+ return '(.*)';
93
+ }
94
+
95
+ if (match === '/(') {
96
+ backtrack += '/';
97
+ extraOffset += 2;
98
+ return '/(?:';
99
+ }
100
+
68
101
  slash = slash || '';
69
- format = format || '';
70
- capture = capture || '([^\\/' + format + ']+?)';
102
+ format = format ? '\\.' : '';
71
103
  optional = optional || '';
104
+ capture = capture ?
105
+ capture.replace(/\\.|\*/, function (m) { return m === '*' ? '(.*)' : m; }) :
106
+ (backtrack ? '((?:(?!/|' + backtrack + ').)+?)' : '([^/' + format + ']+?)');
72
107
 
73
108
  keys.push({
74
109
  name: key,
@@ -76,41 +111,20 @@ function pathtoRegexp(path, keys, options) {
76
111
  offset: offset + extraOffset
77
112
  });
78
113
 
79
- var result = ''
80
- + (optional ? '' : slash)
81
- + '(?:'
82
- + format + (optional ? slash : '') + capture
83
- + (star ? '((?:[\\/' + format + '].+?)?)' : '')
114
+ var result = '(?:'
115
+ + format + slash + capture
116
+ + (star ? '((?:[/' + format + '].+?)?)' : '')
84
117
  + ')'
85
118
  + optional;
86
119
 
87
120
  extraOffset += result.length - match.length;
88
121
 
89
122
  return result;
90
- })
91
- .replace(/\*/g, function (star, index) {
92
- var len = keys.length
93
-
94
- while (len-- > keysOffset && keys[len].offset > index) {
95
- keys[len].offset += 3; // Replacement length minus asterisk length.
96
- }
97
-
98
- return '(.*)';
99
123
  });
100
124
 
101
125
  // This is a workaround for handling unnamed matching groups.
102
126
  while (m = MATCHING_GROUP_REGEXP.exec(path)) {
103
- var escapeCount = 0;
104
- var index = m.index;
105
-
106
- while (path.charAt(--index) === '\\') {
107
- escapeCount++;
108
- }
109
-
110
- // It's possible to escape the bracket.
111
- if (escapeCount % 2 === 1) {
112
- continue;
113
- }
127
+ if (m[0][0] === '\\') continue;
114
128
 
115
129
  if (keysOffset + i === keys.length || keys[keysOffset + i].offset > m.index) {
116
130
  keys.splice(keysOffset + i, 0, {
@@ -123,12 +137,14 @@ function pathtoRegexp(path, keys, options) {
123
137
  i++;
124
138
  }
125
139
 
140
+ path += strict ? '' : path[path.length - 1] === '/' ? '?' : '/?';
141
+
126
142
  // If the path is non-ending, match until the end or a slash.
127
143
  if (end) {
128
144
  path += '$';
129
145
  } else if (path[path.length - 1] !== '/') {
130
- path += lookahead ? '(?=\\/|$)' : '(?:\/|$)';
146
+ path += lookahead ? '(?=/|$)' : '(?:/|$)';
131
147
  }
132
148
 
133
- return new RegExp(path, flags);
149
+ return new RegExp('^' + path, flags);
134
150
  };
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": "0.1.9",
4
+ "version": "0.1.11",
5
5
  "files": [
6
6
  "index.js",
7
7
  "LICENSE"
@@ -21,7 +21,7 @@
21
21
  "license": "MIT",
22
22
  "repository": {
23
23
  "type": "git",
24
- "url": "https://github.com/component/path-to-regexp.git"
24
+ "url": "https://github.com/pillarjs/path-to-regexp.git"
25
25
  },
26
26
  "devDependencies": {
27
27
  "mocha": "^1.17.1",