path-to-regexp 1.7.0 → 1.9.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
@@ -82,7 +82,7 @@ re.exec('/test')
82
82
  //=> ['/test', 'test', undefined]
83
83
 
84
84
  re.exec('/test/route')
85
- //=> ['/test', 'test', 'route']
85
+ //=> ['/test/route', 'test', 'route']
86
86
  ```
87
87
 
88
88
  ##### Zero or more
package/index.d.ts CHANGED
@@ -33,6 +33,13 @@ declare namespace pathToRegexp {
33
33
  delimiter?: string;
34
34
  }
35
35
 
36
+ export interface TokensToFunctionOptions {
37
+ /**
38
+ * When `true` the regexp will be case sensitive. (default: `false`)
39
+ */
40
+ sensitive?: boolean;
41
+ }
42
+
36
43
  /**
37
44
  * Parse an Express-style path into an array of tokens.
38
45
  */
@@ -41,12 +48,12 @@ declare namespace pathToRegexp {
41
48
  /**
42
49
  * Transforming an Express-style path into a valid path.
43
50
  */
44
- export function compile (path: string, options?: ParseOptions): PathFunction;
51
+ export function compile (path: string, options?: ParseOptions & TokensToFunctionOptions): PathFunction;
45
52
 
46
53
  /**
47
54
  * Transform an array of tokens into a path generator function.
48
55
  */
49
- export function tokensToFunction (tokens: Token[]): PathFunction;
56
+ export function tokensToFunction (tokens: Token[], options?: TokensToFunctionOptions): PathFunction;
50
57
 
51
58
  /**
52
59
  * Transform an array of tokens into a matching regular expression.
package/index.js CHANGED
@@ -72,8 +72,9 @@ function parse (str, options) {
72
72
  var partial = prefix != null && next != null && next !== prefix
73
73
  var repeat = modifier === '+' || modifier === '*'
74
74
  var optional = modifier === '?' || modifier === '*'
75
- var delimiter = res[2] || defaultDelimiter
75
+ var delimiter = prefix || defaultDelimiter
76
76
  var pattern = capture || group
77
+ var prevText = prefix || (typeof tokens[tokens.length - 1] === 'string' ? tokens[tokens.length - 1] : '')
77
78
 
78
79
  tokens.push({
79
80
  name: name || key++,
@@ -83,7 +84,7 @@ function parse (str, options) {
83
84
  repeat: repeat,
84
85
  partial: partial,
85
86
  asterisk: !!asterisk,
86
- pattern: pattern ? escapeGroup(pattern) : (asterisk ? '.*' : '[^' + escapeString(delimiter) + ']+?')
87
+ pattern: pattern ? escapeGroup(pattern) : (asterisk ? '.*' : restrictBacktrack(delimiter, prevText))
87
88
  })
88
89
  }
89
90
 
@@ -100,6 +101,14 @@ function parse (str, options) {
100
101
  return tokens
101
102
  }
102
103
 
104
+ function restrictBacktrack(delimiter, prevText) {
105
+ if (!prevText || prevText.indexOf(delimiter) > -1) {
106
+ return '[^' + escapeString(delimiter) + ']+?'
107
+ }
108
+
109
+ return escapeString(prevText) + '|(?:(?!' + escapeString(prevText) + ')[^' + escapeString(delimiter) + '])+?'
110
+ }
111
+
103
112
  /**
104
113
  * Compile a string to a template function for the path.
105
114
  *
@@ -108,7 +117,7 @@ function parse (str, options) {
108
117
  * @return {!function(Object=, Object=)}
109
118
  */
110
119
  function compile (str, options) {
111
- return tokensToFunction(parse(str, options))
120
+ return tokensToFunction(parse(str, options), options)
112
121
  }
113
122
 
114
123
  /**
@@ -138,14 +147,14 @@ function encodeAsterisk (str) {
138
147
  /**
139
148
  * Expose a method for transforming tokens into the path function.
140
149
  */
141
- function tokensToFunction (tokens) {
150
+ function tokensToFunction (tokens, options) {
142
151
  // Compile all the tokens into regexps.
143
152
  var matches = new Array(tokens.length)
144
153
 
145
154
  // Compile all the patterns before compilation.
146
155
  for (var i = 0; i < tokens.length; i++) {
147
156
  if (typeof tokens[i] === 'object') {
148
- matches[i] = new RegExp('^(?:' + tokens[i].pattern + ')$')
157
+ matches[i] = new RegExp('^(?:' + tokens[i].pattern + ')$', flags(options))
149
158
  }
150
159
  }
151
160
 
@@ -258,7 +267,7 @@ function attachKeys (re, keys) {
258
267
  * @return {string}
259
268
  */
260
269
  function flags (options) {
261
- return options.sensitive ? '' : 'i'
270
+ return options && options.sensitive ? '' : 'i'
262
271
  }
263
272
 
264
273
  /**
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": "1.7.0",
4
+ "version": "1.9.0",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
7
7
  "files": [
@@ -11,10 +11,9 @@
11
11
  ],
12
12
  "scripts": {
13
13
  "lint": "standard",
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",
16
- "prepublish": "typings install",
17
- "test": "npm run lint && npm run test-cov"
14
+ "test-spec": "mocha -R spec --bail test.js",
15
+ "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- -R spec test.js",
16
+ "test": "npm run test-cov"
18
17
  },
19
18
  "keywords": [
20
19
  "express",
@@ -38,8 +37,7 @@
38
37
  "mocha": "~2.2.4",
39
38
  "standard": "~3.7.3",
40
39
  "ts-node": "^0.5.5",
41
- "typescript": "^1.8.7",
42
- "typings": "^1.0.4"
40
+ "typescript": "^1.8.7"
43
41
  },
44
42
  "dependencies": {
45
43
  "isarray": "0.0.1"
package/History.md DELETED
@@ -1,153 +0,0 @@
1
- 1.6.0 / 2016-10-03
2
- ==================
3
-
4
- * Populate `RegExp.keys` when using the `tokensToRegExp` method (making it consistent with the main export)
5
- * Allow a `delimiter` option to be passed in with `parse`
6
- * Updated TypeScript definition with `Keys` and `Options` updated
7
-
8
- 1.5.3 / 2016-06-15
9
- ==================
10
-
11
- * Add `\\` to the ignore character group to avoid backtracking on mismatched parens
12
-
13
- 1.5.2 / 2016-06-15
14
- ==================
15
-
16
- * Escape `\\` in string segments of regexp
17
-
18
- 1.5.1 / 2016-06-08
19
- ==================
20
-
21
- * Add `index.d.ts` to NPM package
22
-
23
- 1.5.0 / 2016-05-20
24
- ==================
25
-
26
- * Handle partial token segments (better)
27
- * Allow compile to handle asterisk token segments
28
-
29
- 1.4.0 / 2016-05-18
30
- ==================
31
-
32
- * Handle RegExp unions in path matching groups
33
-
34
- 1.3.0 / 2016-05-08
35
- ==================
36
-
37
- * Clarify README language and named parameter token support
38
- * Support advanced Closure Compiler with type annotations
39
- * Add pretty paths options to compiled function output
40
- * Add TypeScript definition to project
41
- * Improved prefix handling with non-complete segment parameters (E.g. `/:foo?-bar`)
42
-
43
- 1.2.1 / 2015-08-17
44
- ==================
45
-
46
- * Encode values before validation with path compilation function
47
- * More examples of using compilation in README
48
-
49
- 1.2.0 / 2015-05-20
50
- ==================
51
-
52
- * Add support for matching an asterisk (`*`) as an unnamed match everything group (`(.*)`)
53
-
54
- 1.1.1 / 2015-05-11
55
- ==================
56
-
57
- * Expose methods for working with path tokens
58
-
59
- 1.1.0 / 2015-05-09
60
- ==================
61
-
62
- * Expose the parser implementation to consumers
63
- * Implement a compiler function to generate valid strings
64
- * Huge refactor of tests to be more DRY and cover new parse and compile functions
65
- * Use chai in tests
66
- * Add .editorconfig
67
-
68
- 1.0.3 / 2015-01-17
69
- ==================
70
-
71
- * Optimised function runtime
72
- * Added `files` to `package.json`
73
-
74
- 1.0.2 / 2014-12-17
75
- ==================
76
-
77
- * Use `Array.isArray` shim
78
- * Remove ES5 incompatible code
79
- * Fixed repository path
80
- * Added new readme badges
81
-
82
- 1.0.1 / 2014-08-27
83
- ==================
84
-
85
- * Ensure installation works correctly on 0.8
86
-
87
- 1.0.0 / 2014-08-17
88
- ==================
89
-
90
- * No more API changes
91
-
92
- 0.2.5 / 2014-08-07
93
- ==================
94
-
95
- * Allow keys parameter to be omitted
96
-
97
- 0.2.4 / 2014-08-02
98
- ==================
99
-
100
- * Code coverage badge
101
- * Updated readme
102
- * Attach keys to the generated regexp
103
-
104
- 0.2.3 / 2014-07-09
105
- ==================
106
-
107
- * Add MIT license
108
-
109
- 0.2.2 / 2014-07-06
110
- ==================
111
-
112
- * A passed in trailing slash in non-strict mode will become optional
113
- * In non-end mode, the optional trailing slash will only match at the end
114
-
115
- 0.2.1 / 2014-06-11
116
- ==================
117
-
118
- * Fixed a major capturing group regexp regression
119
-
120
- 0.2.0 / 2014-06-09
121
- ==================
122
-
123
- * Improved support for arrays
124
- * Improved support for regexps
125
- * Better support for non-ending strict mode matches with a trailing slash
126
- * Travis CI support
127
- * Block using regexp special characters in the path
128
- * Removed support for the asterisk to match all
129
- * New support for parameter suffixes - `*`, `+` and `?`
130
- * Updated readme
131
- * Provide delimiter information with keys array
132
-
133
- 0.1.2 / 2014-03-10
134
- ==================
135
-
136
- * Move testing dependencies to `devDependencies`
137
-
138
- 0.1.1 / 2014-03-10
139
- ==================
140
-
141
- * Match entire substring with `options.end`
142
- * Properly handle ending and non-ending matches
143
-
144
- 0.1.0 / 2014-03-06
145
- ==================
146
-
147
- * Add `options.end`
148
-
149
- 0.0.2 / 2013-02-10
150
- ==================
151
-
152
- * Update to match current express
153
- * Add .license property to component.json