Skip to content

Commit

Permalink
feat: expose caseSensitive option from path-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
troch committed Apr 19, 2018
1 parent fdfca85 commit ff3fcb8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions modules/RouteNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface MatchOptions {
queryParams?: QueryParamsOptions
strictTrailingSlash?: boolean
strongMatching?: boolean
caseSensitive?: boolean
}
export { QueryParamsOptions }

Expand Down
17 changes: 13 additions & 4 deletions modules/matchChildren.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
})
}

Expand All @@ -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(/^\/\?/, '?')
Expand Down
8 changes: 8 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ff3fcb8

Please sign in to comment.