Skip to content

Commit

Permalink
feat: backport TokensToFunctionOptions to v1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
pimlie committed Sep 9, 2019
1 parent 9c0550c commit 1a47442
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
11 changes: 9 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ declare namespace pathToRegexp {
delimiter?: string;
}

export interface TokensToFunctionOptions {
/**
* When `true` the regexp will be case sensitive. (default: `false`)
*/
sensitive?: boolean;
}

/**
* Parse an Express-style path into an array of tokens.
*/
Expand All @@ -41,12 +48,12 @@ declare namespace pathToRegexp {
/**
* Transforming an Express-style path into a valid path.
*/
export function compile (path: string, options?: ParseOptions): PathFunction;
export function compile (path: string, options?: ParseOptions & TokensToFunctionOptions): PathFunction;

/**
* Transform an array of tokens into a path generator function.
*/
export function tokensToFunction (tokens: Token[]): PathFunction;
export function tokensToFunction (tokens: Token[], options?: TokensToFunctionOptions): PathFunction;

/**
* Transform an array of tokens into a matching regular expression.
Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function parse (str, options) {
* @return {!function(Object=, Object=)}
*/
function compile (str, options) {
return tokensToFunction(parse(str, options))
return tokensToFunction(parse(str, options), options)
}

/**
Expand Down Expand Up @@ -138,14 +138,14 @@ function encodeAsterisk (str) {
/**
* Expose a method for transforming tokens into the path function.
*/
function tokensToFunction (tokens) {
function tokensToFunction (tokens, options) {
// Compile all the tokens into regexps.
var matches = new Array(tokens.length)

// Compile all the patterns before compilation.
for (var i = 0; i < tokens.length; i++) {
if (typeof tokens[i] === 'object') {
matches[i] = new RegExp('^(?:' + tokens[i].pattern + ')$')
matches[i] = new RegExp('^(?:' + tokens[i].pattern + ')$', flags(options))
}
}

Expand Down Expand Up @@ -258,7 +258,7 @@ function attachKeys (re, keys) {
* @return {string}
*/
function flags (options) {
return options.sensitive ? '' : 'i'
return options && options.sensitive ? '' : 'i'
}

/**
Expand Down
58 changes: 56 additions & 2 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2238,6 +2238,60 @@ var TESTS: Test[] = [
[
[null, 'this is']
]
],
/**
* Case-sensitive compile tokensToFunction params.
*/
[
'/:test(abc)',
{
sensitive: true
},
[
{
name: 'test',
prefix: '/',
delimiter: '/',
optional: false,
repeat: false,
partial: false,
asterisk: false,
pattern: 'abc'
}
],
[
['/abc', ['/abc', 'abc']],
['/ABC', null]
],
[
[{ test: 'abc' }, '/abc'],
[{ test: 'ABC' }, null]
]
],
[
'/:test(abc)',
{
},
[
{
name: 'test',
prefix: '/',
delimiter: '/',
optional: false,
repeat: false,
partial: false,
asterisk: false,
pattern: 'abc'
}
],
[
['/abc', ['/abc', 'abc']],
['/ABC', ['/ABC', 'ABC']]
],
[
[{ test: 'abc' }, '/abc'],
[{ test: 'ABC' }, '/ABC']
]
]
]

Expand Down Expand Up @@ -2322,7 +2376,7 @@ describe('path-to-regexp', function () {
})

describe(util.inspect(path), function () {
var re = pathToRegexp(path, opts)
var re = pathToRegexp(path as string, opts)

// Parsing and compiling is only supported with string input.
if (typeof path === 'string') {
Expand All @@ -2331,7 +2385,7 @@ describe('path-to-regexp', function () {
})

describe('compile', function () {
var toPath = pathToRegexp.compile(path)
var toPath = pathToRegexp.compile(path as string, opts)

compileCases.forEach(function (io) {
var input = io[0]
Expand Down

0 comments on commit 1a47442

Please sign in to comment.