path-to-regexp 0.1.8 → 0.1.10

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 +56 -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,20 +25,25 @@ 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;
32
32
  var end = options.end !== false;
33
33
  var flags = options.sensitive ? '' : 'i';
34
+ var lookahead = options.lookahead !== false;
34
35
  var extraOffset = 0;
35
36
  var keysOffset = keys.length;
36
37
  var i = 0;
37
38
  var name = 0;
39
+ var pos = 0;
40
+ var backtrack = '';
38
41
  var m;
39
42
 
40
43
  if (path instanceof RegExp) {
41
44
  while (m = MATCHING_GROUP_REGEXP.exec(path.source)) {
45
+ if (m[0][0] === '\\') continue;
46
+
42
47
  keys.push({
43
48
  name: m[1] || name++,
44
49
  optional: false,
@@ -54,20 +59,47 @@ function pathtoRegexp(path, keys, options) {
54
59
  // the same keys and options instance into every generation to get
55
60
  // consistent matching groups before we join the sources together.
56
61
  path = path.map(function (value) {
57
- return pathtoRegexp(value, keys, options).source;
62
+ return pathToRegexp(value, keys, options).source;
58
63
  });
59
64
 
60
- return new RegExp('(?:' + path.join('|') + ')', flags);
65
+ return new RegExp(path.join('|'), flags);
61
66
  }
62
67
 
63
- path = ('^' + path + (strict ? '' : path[path.length - 1] === '/' ? '?' : '/?'))
64
- .replace(/\/\(/g, '/(?:')
65
- .replace(/([\/\.])/g, '\\$1')
66
- .replace(/(\\\/)?(\\\.)?:(\w+)(\(.*?\))?(\*)?(\?)?/g, function (match, slash, format, key, capture, star, optional, offset) {
68
+ path = path.replace(
69
+ /\\.|(\/)?(\.)?:(\w+)(\(.*?\))?(\*)?(\?)?|[.*]|\/\(/g,
70
+ function (match, slash, format, key, capture, star, optional, offset) {
71
+ pos = offset + match.length;
72
+
73
+ if (match[0] === '\\') {
74
+ backtrack += match;
75
+ return match;
76
+ }
77
+
78
+ if (match === '.') {
79
+ backtrack += '\\.';
80
+ extraOffset += 1;
81
+ return '\\.';
82
+ }
83
+
84
+ backtrack = slash || format ? '' : path.slice(pos, offset);
85
+
86
+ if (match === '*') {
87
+ extraOffset += 3;
88
+ return '(.*)';
89
+ }
90
+
91
+ if (match === '/(') {
92
+ backtrack += '/';
93
+ extraOffset += 2;
94
+ return '/(?:';
95
+ }
96
+
67
97
  slash = slash || '';
68
- format = format || '';
69
- capture = capture || '([^\\/' + format + ']+?)';
98
+ format = format ? '\\.' : '';
70
99
  optional = optional || '';
100
+ capture = capture ?
101
+ capture.replace(/\\.|\*/, function (m) { return m === '*' ? '(.*)' : m; }) :
102
+ (backtrack ? '((?:(?!/|' + backtrack + ').)+?)' : '([^/' + format + ']+?)');
71
103
 
72
104
  keys.push({
73
105
  name: key,
@@ -75,41 +107,20 @@ function pathtoRegexp(path, keys, options) {
75
107
  offset: offset + extraOffset
76
108
  });
77
109
 
78
- var result = ''
79
- + (optional ? '' : slash)
80
- + '(?:'
81
- + format + (optional ? slash : '') + capture
82
- + (star ? '((?:[\\/' + format + '].+?)?)' : '')
110
+ var result = '(?:'
111
+ + format + slash + capture
112
+ + (star ? '((?:[/' + format + '].+?)?)' : '')
83
113
  + ')'
84
114
  + optional;
85
115
 
86
116
  extraOffset += result.length - match.length;
87
117
 
88
118
  return result;
89
- })
90
- .replace(/\*/g, function (star, index) {
91
- var len = keys.length
92
-
93
- while (len-- > keysOffset && keys[len].offset > index) {
94
- keys[len].offset += 3; // Replacement length minus asterisk length.
95
- }
96
-
97
- return '(.*)';
98
119
  });
99
120
 
100
121
  // This is a workaround for handling unnamed matching groups.
101
122
  while (m = MATCHING_GROUP_REGEXP.exec(path)) {
102
- var escapeCount = 0;
103
- var index = m.index;
104
-
105
- while (path.charAt(--index) === '\\') {
106
- escapeCount++;
107
- }
108
-
109
- // It's possible to escape the bracket.
110
- if (escapeCount % 2 === 1) {
111
- continue;
112
- }
123
+ if (m[0][0] === '\\') continue;
113
124
 
114
125
  if (keysOffset + i === keys.length || keys[keysOffset + i].offset > m.index) {
115
126
  keys.splice(keysOffset + i, 0, {
@@ -122,8 +133,14 @@ function pathtoRegexp(path, keys, options) {
122
133
  i++;
123
134
  }
124
135
 
136
+ path += strict ? '' : path[path.length - 1] === '/' ? '?' : '/?';
137
+
125
138
  // If the path is non-ending, match until the end or a slash.
126
- path += (end ? '$' : (path[path.length - 1] === '/' ? '' : '(?=\\/|$)'));
139
+ if (end) {
140
+ path += '$';
141
+ } else if (path[path.length - 1] !== '/') {
142
+ path += lookahead ? '(?=/|$)' : '(?:/|$)';
143
+ }
127
144
 
128
- return new RegExp(path, flags);
145
+ return new RegExp('^' + path, flags);
129
146
  };
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.8",
4
+ "version": "0.1.10",
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",