From ff3fcb8c0788e8bf640ba3a0002ecf9d1d728a19 Mon Sep 17 00:00:00 2001 From: Thomas Roch Date: Thu, 19 Apr 2018 09:20:03 +0100 Subject: [PATCH] feat: expose caseSensitive option from path-parser --- modules/RouteNode.ts | 1 + modules/matchChildren.ts | 17 +++++++++++++---- test/main.js | 8 ++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/modules/RouteNode.ts b/modules/RouteNode.ts index 93a977d..7e60fd9 100644 --- a/modules/RouteNode.ts +++ b/modules/RouteNode.ts @@ -39,6 +39,7 @@ export interface MatchOptions { queryParams?: QueryParamsOptions strictTrailingSlash?: boolean strongMatching?: boolean + caseSensitive?: boolean } export { QueryParamsOptions } diff --git a/modules/matchChildren.ts b/modules/matchChildren.ts index 1017c8f..5bfde7b 100644 --- a/modules/matchChildren.ts +++ b/modules/matchChildren.ts @@ -15,7 +15,8 @@ const matchChildren = ( const { queryParamsMode = 'default', strictTrailingSlash = false, - strongMatching = true + strongMatching = true, + caseSensitive = false } = options const isRoot = nodes.length === 1 && nodes[0].name === '' // for (child of node.children) { @@ -32,12 +33,16 @@ const matchChildren = ( } if (!child.children.length) { - match = child.parser.test(segment, options) + match = child.parser.test(segment, { + caseSensitive, + strictTrailingSlash + }) } if (!match) { match = child.parser.partialTest(segment, { - delimited: strongMatching + delimited: strongMatching, + caseSensitive }) } @@ -46,11 +51,15 @@ const matchChildren = ( let consumedPath = child.parser.build(match, { ignoreSearch: true }) + if (!strictTrailingSlash && !child.children.length) { consumedPath = consumedPath.replace(/\/$/, '') } - remainingPath = segment.replace(consumedPath, '') + remainingPath = segment.replace( + new RegExp('^' + consumedPath, 'i'), + '' + ) if (!strictTrailingSlash && !child.children.length) { remainingPath = remainingPath.replace(/^\/\?/, '?') diff --git a/test/main.js b/test/main.js index 72c388c..c807394 100644 --- a/test/main.js +++ b/test/main.js @@ -726,6 +726,14 @@ describe('RouteNode', function() { params: { path: 'foo', bar: 'bar' } }) }) + + it('should be case insensitive by default', () => { + const node = new RouteNode('', '', [new RouteNode('a', '/a')]) + + node.matchPath('/a').name.should.equal('a') + node.matchPath('/A', { caseSensitive: false }).name.should.equal('a') + should.not.exist(node.matchPath('/A', { caseSensitive: true })) + }) }) function getRoutes(trailingSlash) {