Skip to content

Commit

Permalink
Add sensitive option for tokensToFunction (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
boringame authored and blakeembrey committed Aug 31, 2019
1 parent d1ec03a commit bef800d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
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

0 comments on commit bef800d

Please sign in to comment.