Skip to content

Commit ff3fcb8

Browse files
committed
feat: expose caseSensitive option from path-parser
1 parent fdfca85 commit ff3fcb8

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

modules/RouteNode.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export interface MatchOptions {
3939
queryParams?: QueryParamsOptions
4040
strictTrailingSlash?: boolean
4141
strongMatching?: boolean
42+
caseSensitive?: boolean
4243
}
4344
export { QueryParamsOptions }
4445

modules/matchChildren.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const matchChildren = (
1515
const {
1616
queryParamsMode = 'default',
1717
strictTrailingSlash = false,
18-
strongMatching = true
18+
strongMatching = true,
19+
caseSensitive = false
1920
} = options
2021
const isRoot = nodes.length === 1 && nodes[0].name === ''
2122
// for (child of node.children) {
@@ -32,12 +33,16 @@ const matchChildren = (
3233
}
3334

3435
if (!child.children.length) {
35-
match = child.parser.test(segment, options)
36+
match = child.parser.test(segment, {
37+
caseSensitive,
38+
strictTrailingSlash
39+
})
3640
}
3741

3842
if (!match) {
3943
match = child.parser.partialTest(segment, {
40-
delimited: strongMatching
44+
delimited: strongMatching,
45+
caseSensitive
4146
})
4247
}
4348

@@ -46,11 +51,15 @@ const matchChildren = (
4651
let consumedPath = child.parser.build(match, {
4752
ignoreSearch: true
4853
})
54+
4955
if (!strictTrailingSlash && !child.children.length) {
5056
consumedPath = consumedPath.replace(/\/$/, '')
5157
}
5258

53-
remainingPath = segment.replace(consumedPath, '')
59+
remainingPath = segment.replace(
60+
new RegExp('^' + consumedPath, 'i'),
61+
''
62+
)
5463

5564
if (!strictTrailingSlash && !child.children.length) {
5665
remainingPath = remainingPath.replace(/^\/\?/, '?')

test/main.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,14 @@ describe('RouteNode', function() {
726726
params: { path: 'foo', bar: 'bar' }
727727
})
728728
})
729+
730+
it('should be case insensitive by default', () => {
731+
const node = new RouteNode('', '', [new RouteNode('a', '/a')])
732+
733+
node.matchPath('/a').name.should.equal('a')
734+
node.matchPath('/A', { caseSensitive: false }).name.should.equal('a')
735+
should.not.exist(node.matchPath('/A', { caseSensitive: true }))
736+
})
729737
})
730738

731739
function getRoutes(trailingSlash) {

0 commit comments

Comments
 (0)