path-to-regexp 0.1.7 → 0.1.12

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.
Files changed (3) hide show
  1. package/index.js +67 -40
  2. package/package.json +2 -2
  3. package/History.md +0 -36
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,22 +25,27 @@ 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
- name: name++,
48
+ name: m[1] || name++,
44
49
  optional: false,
45
50
  offset: m.index
46
51
  });
@@ -54,20 +59,57 @@ 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);
66
+ }
67
+
68
+ if (typeof path !== 'string') {
69
+ throw new TypeError('path must be a string, array of strings, or regular expression');
61
70
  }
62
71
 
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) {
72
+ path = path.replace(
73
+ /\\.|(\/)?(\.)?:(\w+)(\(.*?\))?(\*)?(\?)?|[.*]|\/\(/g,
74
+ function (match, slash, format, key, capture, star, optional, offset) {
75
+ if (match[0] === '\\') {
76
+ backtrack += match;
77
+ pos += 2;
78
+ return match;
79
+ }
80
+
81
+ if (match === '.') {
82
+ backtrack += '\\.';
83
+ extraOffset += 1;
84
+ pos += 1;
85
+ return '\\.';
86
+ }
87
+
88
+ if (slash || format) {
89
+ backtrack = '';
90
+ } else {
91
+ backtrack += path.slice(pos, offset);
92
+ }
93
+
94
+ pos = offset + match.length;
95
+
96
+ if (match === '*') {
97
+ extraOffset += 3;
98
+ return '(.*)';
99
+ }
100
+
101
+ if (match === '/(') {
102
+ backtrack += '/';
103
+ extraOffset += 2;
104
+ return '/(?:';
105
+ }
106
+
67
107
  slash = slash || '';
68
- format = format || '';
69
- capture = capture || '([^\\/' + format + ']+?)';
108
+ format = format ? '\\.' : '';
70
109
  optional = optional || '';
110
+ capture = capture ?
111
+ capture.replace(/\\.|\*/, function (m) { return m === '*' ? '(.*)' : m; }) :
112
+ (backtrack ? '((?:(?!/|' + backtrack + ').)+?)' : '([^/' + format + ']+?)');
71
113
 
72
114
  keys.push({
73
115
  name: key,
@@ -75,41 +117,20 @@ function pathtoRegexp(path, keys, options) {
75
117
  offset: offset + extraOffset
76
118
  });
77
119
 
78
- var result = ''
79
- + (optional ? '' : slash)
80
- + '(?:'
81
- + format + (optional ? slash : '') + capture
82
- + (star ? '((?:[\\/' + format + '].+?)?)' : '')
120
+ var result = '(?:'
121
+ + format + slash + capture
122
+ + (star ? '((?:[/' + format + '].+?)?)' : '')
83
123
  + ')'
84
124
  + optional;
85
125
 
86
126
  extraOffset += result.length - match.length;
87
127
 
88
128
  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
129
  });
99
130
 
100
131
  // This is a workaround for handling unnamed matching groups.
101
132
  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
- }
133
+ if (m[0][0] === '\\') continue;
113
134
 
114
135
  if (keysOffset + i === keys.length || keys[keysOffset + i].offset > m.index) {
115
136
  keys.splice(keysOffset + i, 0, {
@@ -122,8 +143,14 @@ function pathtoRegexp(path, keys, options) {
122
143
  i++;
123
144
  }
124
145
 
146
+ path += strict ? '' : path[path.length - 1] === '/' ? '?' : '/?';
147
+
125
148
  // If the path is non-ending, match until the end or a slash.
126
- path += (end ? '$' : (path[path.length - 1] === '/' ? '' : '(?=\\/|$)'));
149
+ if (end) {
150
+ path += '$';
151
+ } else if (path[path.length - 1] !== '/') {
152
+ path += lookahead ? '(?=/|$)' : '(?:/|$)';
153
+ }
127
154
 
128
- return new RegExp(path, flags);
155
+ return new RegExp('^' + path, flags);
129
156
  };
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.7",
4
+ "version": "0.1.12",
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",
package/History.md DELETED
@@ -1,36 +0,0 @@
1
- 0.1.7 / 2015-07-28
2
- ==================
3
-
4
- * Fixed regression with escaped round brackets and matching groups.
5
-
6
- 0.1.6 / 2015-06-19
7
- ==================
8
-
9
- * Replace `index` feature by outputting all parameters, unnamed and named.
10
-
11
- 0.1.5 / 2015-05-08
12
- ==================
13
-
14
- * Add an index property for position in match result.
15
-
16
- 0.1.4 / 2015-03-05
17
- ==================
18
-
19
- * Add license information
20
-
21
- 0.1.3 / 2014-07-06
22
- ==================
23
-
24
- * Better array support
25
- * Improved support for trailing slash in non-ending mode
26
-
27
- 0.1.0 / 2014-03-06
28
- ==================
29
-
30
- * add options.end
31
-
32
- 0.0.2 / 2013-02-10
33
- ==================
34
-
35
- * Update to match current express
36
- * add .license property to component.json