From f5ff8a531f3746c8075299bfddcaa3e83db46eb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=AB=E6=9B=B0?= Date: Wed, 10 Jul 2019 14:24:50 +0800 Subject: [PATCH] TokensToFunctionOptions --- index.d.ts | 11 +++++++++-- index.js | 6 +++--- test.ts | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 5 deletions(-) diff --git a/index.d.ts b/index.d.ts index 12bd690..a25edef 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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. */ @@ -47,12 +54,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. diff --git a/index.js b/index.js index 85bfd33..460c600 100644 --- a/index.js +++ b/index.js @@ -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)) } } diff --git a/test.ts b/test.ts index abca995..66aab8c 100644 --- a/test.ts +++ b/test.ts @@ -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'] + ] + ], + ] /**