Skip to content

Commit 1a47442

Browse files
committed
feat: backport TokensToFunctionOptions to v1.x
1 parent 9c0550c commit 1a47442

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

index.d.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ declare namespace pathToRegexp {
3333
delimiter?: string;
3434
}
3535

36+
export interface TokensToFunctionOptions {
37+
/**
38+
* When `true` the regexp will be case sensitive. (default: `false`)
39+
*/
40+
sensitive?: boolean;
41+
}
42+
3643
/**
3744
* Parse an Express-style path into an array of tokens.
3845
*/
@@ -41,12 +48,12 @@ declare namespace pathToRegexp {
4148
/**
4249
* Transforming an Express-style path into a valid path.
4350
*/
44-
export function compile (path: string, options?: ParseOptions): PathFunction;
51+
export function compile (path: string, options?: ParseOptions & TokensToFunctionOptions): PathFunction;
4552

4653
/**
4754
* Transform an array of tokens into a path generator function.
4855
*/
49-
export function tokensToFunction (tokens: Token[]): PathFunction;
56+
export function tokensToFunction (tokens: Token[], options?: TokensToFunctionOptions): PathFunction;
5057

5158
/**
5259
* Transform an array of tokens into a matching regular expression.

index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function parse (str, options) {
108108
* @return {!function(Object=, Object=)}
109109
*/
110110
function compile (str, options) {
111-
return tokensToFunction(parse(str, options))
111+
return tokensToFunction(parse(str, options), options)
112112
}
113113

114114
/**
@@ -138,14 +138,14 @@ function encodeAsterisk (str) {
138138
/**
139139
* Expose a method for transforming tokens into the path function.
140140
*/
141-
function tokensToFunction (tokens) {
141+
function tokensToFunction (tokens, options) {
142142
// Compile all the tokens into regexps.
143143
var matches = new Array(tokens.length)
144144

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

@@ -258,7 +258,7 @@ function attachKeys (re, keys) {
258258
* @return {string}
259259
*/
260260
function flags (options) {
261-
return options.sensitive ? '' : 'i'
261+
return options && options.sensitive ? '' : 'i'
262262
}
263263

264264
/**

test.ts

+56-2
Original file line numberDiff line numberDiff line change
@@ -2238,6 +2238,60 @@ var TESTS: Test[] = [
22382238
[
22392239
[null, 'this is']
22402240
]
2241+
],
2242+
/**
2243+
* Case-sensitive compile tokensToFunction params.
2244+
*/
2245+
[
2246+
'/:test(abc)',
2247+
{
2248+
sensitive: true
2249+
},
2250+
[
2251+
{
2252+
name: 'test',
2253+
prefix: '/',
2254+
delimiter: '/',
2255+
optional: false,
2256+
repeat: false,
2257+
partial: false,
2258+
asterisk: false,
2259+
pattern: 'abc'
2260+
}
2261+
],
2262+
[
2263+
['/abc', ['/abc', 'abc']],
2264+
['/ABC', null]
2265+
],
2266+
[
2267+
[{ test: 'abc' }, '/abc'],
2268+
[{ test: 'ABC' }, null]
2269+
]
2270+
],
2271+
[
2272+
'/:test(abc)',
2273+
{
2274+
},
2275+
[
2276+
{
2277+
name: 'test',
2278+
prefix: '/',
2279+
delimiter: '/',
2280+
optional: false,
2281+
repeat: false,
2282+
partial: false,
2283+
asterisk: false,
2284+
pattern: 'abc'
2285+
}
2286+
],
2287+
[
2288+
['/abc', ['/abc', 'abc']],
2289+
['/ABC', ['/ABC', 'ABC']]
2290+
],
2291+
[
2292+
[{ test: 'abc' }, '/abc'],
2293+
[{ test: 'ABC' }, '/ABC']
2294+
]
22412295
]
22422296
]
22432297

@@ -2322,7 +2376,7 @@ describe('path-to-regexp', function () {
23222376
})
23232377

23242378
describe(util.inspect(path), function () {
2325-
var re = pathToRegexp(path, opts)
2379+
var re = pathToRegexp(path as string, opts)
23262380

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

23332387
describe('compile', function () {
2334-
var toPath = pathToRegexp.compile(path)
2388+
var toPath = pathToRegexp.compile(path as string, opts)
23352389

23362390
compileCases.forEach(function (io) {
23372391
var input = io[0]

0 commit comments

Comments
 (0)