Skip to content

TokensToFunctionOptions #191

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

Merged
merged 1 commit into from
Aug 31, 2019
Merged
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 @@ -39,6 +39,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 @@ -47,12 +54,12 @@ declare namespace pathToRegexp {
/**
* Transforming an Express-style path into a valid path.
*/
export function compile <P extends object = object> (path: string, options?: ParseOptions): PathFunction<P>;
export function compile <P extends object = object> (path: string, options?: ParseOptions & TokensToFunctionOptions): PathFunction<P>;

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

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

/**
* 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
52 changes: 52 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2651,6 +2651,58 @@ var TESTS: Test[] = [
[{ attr2: 'attr' }, 'name-attr']
]
],

/**
* Case-sensitive compile tokensToFunction params.
*/
[
'/:test(abc)',
{
sensitive: true
},
[
{
name: 'test',
prefix: '/',
delimiter: '/',
optional: false,
repeat: false,
pattern: 'abc'
}
],
[
['/abc', ['/abc', 'abc']],
['/ABC', null]
],
[
[{ test: 'abc' }, '/abc'],
[{ test: 'ABC' }, null]
]
],
[
'/:test(abc)',
{
},
[
{
name: 'test',
prefix: '/',
delimiter: '/',
optional: false,
repeat: false,
pattern: 'abc'
}
],
[
['/abc', ['/abc', 'abc']],
['/ABC', ['/ABC', 'ABC']]
],
[
[{ test: 'abc' }, '/abc'],
[{ test: 'ABC' }, '/ABC']
]
],

]

/**
Expand Down