Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to disable case-sensitive routing #84

Merged
merged 5 commits into from Jul 2, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Expand Up @@ -54,7 +54,7 @@ const router = require('find-my-way')({
ignoreTrailingSlash: true
})
function handler (req, res, params) {
res.send('foo')
res.end('foo')
}
// maps "/foo/" and "/foo" to `handler`
router.on('GET', '/foo/', handler)
Expand All @@ -75,6 +75,19 @@ const router = require('find-my-way')({
allowUnsafeRegex: true
})
```

According to [RFC3986](https://tools.ietf.org/html/rfc3986#section-6.2.2.1), find-my-way is case sensitive by default.
You can disable this by setting the `caseSensitive` option to `false`:
in that case all paths will be lowercased before routing, including
parametric and regexp-matched values. You can turn off case sensitivity
with:

```js
const router = require('find-my-way')({
caseSensitive: false
})
```

<a name="on"></a>
#### on(method, path, [opts], handler, [store])
Register a new route.
Expand Down
17 changes: 16 additions & 1 deletion index.js
Expand Up @@ -32,6 +32,7 @@ function Router (opts) {
this.defaultRoute = null
}

this.caseSensitive = opts.caseSensitive === undefined ? true : opts.caseSensitive
this.ignoreTrailingSlash = opts.ignoreTrailingSlash || false
this.maxParamLength = opts.maxParamLength || 100
this.allowUnsafeRegex = opts.allowUnsafeRegex || false
Expand Down Expand Up @@ -96,8 +97,14 @@ Router.prototype._on = function _on (method, path, opts, handler, store) {
if (path.charCodeAt(i) === 58) {
var nodeType = NODE_TYPES.PARAM
j = i + 1
var staticPart = path.slice(0, i)

if (!this.caseSensitive) {
staticPart = staticPart.toLowerCase()
}

// add the static part of the route to the tree
this._insert(method, path.slice(0, i), 0, null, null, null, null, version)
this._insert(method, staticPart, 0, null, null, null, null, version)

// isolate the parameter name
var isRegex = false
Expand Down Expand Up @@ -149,6 +156,11 @@ Router.prototype._on = function _on (method, path, opts, handler, store) {
return this._insert(method, path.slice(0, len), 2, params, handler, store, null, version)
}
}

if (!this.caseSensitive) {
path = path.toLowerCase()
}

// static route
this._insert(method, path, 0, params, handler, store, null, version)
}
Expand Down Expand Up @@ -308,6 +320,9 @@ Router.prototype.lookup = function lookup (req, res) {
}

Router.prototype.find = function find (method, path, version) {
if (!this.caseSensitive) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (this.caseSensitive === false)?
It should make V8 happier (but I'm not super sure because the value is already a Boolean).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

path = path.toLowerCase()
}
var maxParamLength = this.maxParamLength
var currentNode = this.tree
var wildcardNode = null
Expand Down
73 changes: 73 additions & 0 deletions test/case-insesitive.js
@@ -0,0 +1,73 @@
'use strict'

const t = require('tap')
const test = t.test
const FindMyWay = require('../')

test('case insensitive static routes of level 1', t => {
t.plan(1)

const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})

findMyWay.on('GET', '/woo', (req, res, params) => {
t.pass('we should be here')
})

findMyWay.lookup({ method: 'GET', url: '/WOO', headers: {} }, null)
})

test('case insensitive static routes of level 3', t => {
t.plan(1)

const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})

findMyWay.on('GET', '/foo/bar/woo', (req, res, params) => {
t.pass('we should be here')
})

findMyWay.lookup({ method: 'GET', url: '/Foo/bAR/WoO', headers: {} }, null)
})

test('parametric case insensitive', t => {
t.plan(1)

const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})

findMyWay.on('GET', '/foo/:param', (req, res, params) => {
t.equal(params.param, 'bar')
})

findMyWay.lookup({ method: 'GET', url: '/Foo/bAR', headers: {} }, null)
})

test('parametric case insensitive with a static part', t => {
t.plan(1)

const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})

findMyWay.on('GET', '/foo/my-:param', (req, res, params) => {
t.equal(params.param, 'bar')
})

findMyWay.lookup({ method: 'GET', url: '/Foo/MY-bAR', headers: {} }, null)
})