path-to-regexp
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -0,1 +1,10 @@ | ||
1.1.0 / 2015-05-09 | ||
================== | ||
* Expose the parser implementation to consumers | ||
* Implement a compiler function to generate valid strings | ||
* Huge refactor of tests to be more DRY and cover new parse and compile functions | ||
* Use chai in tests | ||
* Add .editorconfig | ||
1.0.3 / 2015-01-17 | ||
@@ -2,0 +11,0 @@ ================== |
59
index.js
@@ -9,2 +9,4 @@ var isarray = require('isarray') | ||
module.exports.compile = compile | ||
module.exports.tokensToFunction = tokensToFunction | ||
module.exports.tokensToRegExp = tokensToRegExp | ||
@@ -100,8 +102,16 @@ /** | ||
function compile (str) { | ||
var keys = parse(str) | ||
return tokensToFunction(parse(str)) | ||
} | ||
/** | ||
* Expose a method for transforming tokens into the path function. | ||
*/ | ||
function tokensToFunction (tokens) { | ||
// Compile all the tokens into regexps. | ||
var matches = new Array(tokens.length) | ||
// Compile all the patterns before compilation. | ||
for (var i = 0; i < keys.length; i++) { | ||
if (typeof keys[i] === 'object') { | ||
keys[i].regexp = new RegExp('^' + keys[i].pattern + '$') | ||
for (var i = 0; i < tokens.length; i++) { | ||
if (typeof tokens[i] === 'object') { | ||
matches[i] = new RegExp('^' + tokens[i].pattern + '$') | ||
} | ||
@@ -115,4 +125,4 @@ } | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i] | ||
for (var i = 0; i < tokens.length; i++) { | ||
var key = tokens[i] | ||
@@ -149,3 +159,3 @@ if (typeof key === 'string') { | ||
for (var j = 0; j < value.length; j++) { | ||
if (!key.regexp.test(value[j])) { | ||
if (!matches[i].test(value[j])) { | ||
throw new TypeError('Expected all "' + key.name + '" to match "' + key.pattern + '"') | ||
@@ -160,3 +170,3 @@ } | ||
if (!key.regexp.test(value)) { | ||
if (!matches[i].test(value)) { | ||
throw new TypeError('Expected "' + key.name + '" to match "' + key.pattern + '"') | ||
@@ -270,7 +280,31 @@ } | ||
function stringToRegexp (path, keys, options) { | ||
var tokens = parse(path) | ||
var re = tokensToRegExp(tokens, options) | ||
// Attach keys back to the regexp. | ||
for (var i = 0; i < tokens.length; i++) { | ||
if (typeof tokens[i] !== 'string') { | ||
keys.push(tokens[i]) | ||
} | ||
} | ||
return attachKeys(re, keys) | ||
} | ||
/** | ||
* Expose a function for taking tokens and returning a RegExp. | ||
* | ||
* @param {Array} tokens | ||
* @param {Array} keys | ||
* @param {Object} options | ||
* @return {RegExp} | ||
*/ | ||
function tokensToRegExp (tokens, options) { | ||
options = options || {} | ||
var strict = options.strict | ||
var end = options.end !== false | ||
var route = '' | ||
var endsWithSlash = path.charAt(path.length - 1) === '/' | ||
var tokens = parse(path) | ||
var lastToken = tokens[tokens.length - 1] | ||
var endsWithSlash = typeof lastToken === 'string' && /\/$/.test(lastToken) | ||
@@ -287,5 +321,2 @@ // Iterate over the tokens and create our regexp string. | ||
// Push non-string tokens into the keys array. | ||
keys.push(token) | ||
if (token.repeat) { | ||
@@ -325,3 +356,3 @@ capture += '(?:' + prefix + capture + ')*' | ||
return attachKeys(new RegExp('^' + route, flags(options)), keys) | ||
return new RegExp('^' + route, flags(options)) | ||
} | ||
@@ -328,0 +359,0 @@ |
{ | ||
"name": "path-to-regexp", | ||
"description": "Express style path to RegExp utility", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"files": [ | ||
@@ -6,0 +6,0 @@ "index.js", |
@@ -166,2 +166,9 @@ # Path-to-RegExp | ||
### Working with Tokens | ||
Path-To-RegExp exposes the two functions used internally that accept an array of tokens. | ||
* `pathToRegexp.tokensToRegExp(tokens, options)` Transform an array of tokens into a matching regular expression. | ||
* `pathToRegexp.tokensToFunction(tokens)` Transform an array of tokens into a path generator function. | ||
## Compatibility with Express <= 4.x | ||
@@ -168,0 +175,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
19530
324
203
0