Skip to content

feat: backport TokensToFunctionOptions to v1.x #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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