Skip to content

Commit 49e6e80

Browse files
authored
Populate RegExp.keys from tokensToRegExp (#93)
1 parent ec285ed commit 49e6e80

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

index.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -316,27 +316,23 @@ function arrayToRegexp (path, keys, options) {
316316
* @return {!RegExp}
317317
*/
318318
function stringToRegexp (path, keys, options) {
319-
var tokens = parse(path)
320-
var re = tokensToRegExp(tokens, options)
321-
322-
// Attach keys back to the regexp.
323-
for (var i = 0; i < tokens.length; i++) {
324-
if (typeof tokens[i] !== 'string') {
325-
keys.push(tokens[i])
326-
}
327-
}
328-
329-
return attachKeys(re, keys)
319+
return tokensToRegExp(parse(path), keys, options)
330320
}
331321

332322
/**
333323
* Expose a function for taking tokens and returning a RegExp.
334324
*
335-
* @param {!Array} tokens
336-
* @param {Object=} options
325+
* @param {!Array} tokens
326+
* @param {(Array|Object)=} keys
327+
* @param {Object=} options
337328
* @return {!RegExp}
338329
*/
339-
function tokensToRegExp (tokens, options) {
330+
function tokensToRegExp (tokens, keys, options) {
331+
if (!isarray(keys)) {
332+
options = /** @type {!Object} */ (keys || options)
333+
keys = []
334+
}
335+
340336
options = options || {}
341337

342338
var strict = options.strict
@@ -355,6 +351,8 @@ function tokensToRegExp (tokens, options) {
355351
var prefix = escapeString(token.prefix)
356352
var capture = '(?:' + token.pattern + ')'
357353

354+
keys.push(token)
355+
358356
if (token.repeat) {
359357
capture += '(?:' + prefix + capture + ')*'
360358
}
@@ -389,7 +387,7 @@ function tokensToRegExp (tokens, options) {
389387
route += strict && endsWithSlash ? '' : '(?=\\/|$)'
390388
}
391389

392-
return new RegExp('^' + route, flags(options))
390+
return attachKeys(new RegExp('^' + route, flags(options)), keys)
393391
}
394392

395393
/**
@@ -405,15 +403,13 @@ function tokensToRegExp (tokens, options) {
405403
* @return {!RegExp}
406404
*/
407405
function pathToRegexp (path, keys, options) {
408-
keys = keys || []
409-
410406
if (!isarray(keys)) {
411-
options = /** @type {!Object} */ (keys)
407+
options = /** @type {!Object} */ (keys || options)
412408
keys = []
413-
} else if (!options) {
414-
options = {}
415409
}
416410

411+
options = options || {}
412+
417413
if (path instanceof RegExp) {
418414
return regexpToRegexp(path, /** @type {!Array} */ (keys))
419415
}

test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2156,6 +2156,23 @@ describe('path-to-regexp', function () {
21562156
}
21572157

21582158
describe('arguments', function () {
2159+
it('should work without different call combinations', function () {
2160+
pathToRegexp('/test')
2161+
pathToRegexp('/test', [])
2162+
pathToRegexp('/test', {})
2163+
pathToRegexp('/test', [], {})
2164+
2165+
pathToRegexp(/^\/test/)
2166+
pathToRegexp(/^\/test/, [])
2167+
pathToRegexp(/^\/test/, {})
2168+
pathToRegexp(/^\/test/, [], {})
2169+
2170+
pathToRegexp(['/a', '/b'])
2171+
pathToRegexp(['/a', '/b'], [])
2172+
pathToRegexp(['/a', '/b'], {})
2173+
pathToRegexp(['/a', '/b'], [], {})
2174+
})
2175+
21592176
it('should accept an array of keys as the second argument', function () {
21602177
var keys = []
21612178
var re = pathToRegexp(TEST_PATH, keys, { end: false })

0 commit comments

Comments
 (0)