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 double colon handling (support for paths with colons) #176

Merged
merged 2 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ and the URL of the incoming request is /33/foo/bar,
the second route will be matched because the first chunk (33) matches the static chunk.
If the URL would have been /32/foo/bar, the first route would have been matched.

> If you just want a path containing a colon without declaring a parameter, use a double colon.
> For example, `/name::customVerb` will be interpreted as `/name:customVerb`

<a name="supported-methods"></a>
##### Supported methods
The router is able to route all HTTP methods defined by [`http` core module](https://nodejs.org/api/http.html#http_http_methods).
Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ Router.prototype._on = function _on (method, path, opts, handler, store) {
// search for parametric or wildcard routes
// parametric route
if (path.charCodeAt(i) === 58) {
if (i !== len - 1 && path.charCodeAt(i + 1) === 58) {
delvedor marked this conversation as resolved.
Show resolved Hide resolved
// It's a double colon. Let's just replace it with a single colon and go ahead
path = path.slice(0, i) + path.slice(i + 1)
len = path.length
continue
}

var nodeType = NODE_TYPES.PARAM
j = i + 1
var staticPart = path.slice(0, i)
Expand Down
31 changes: 31 additions & 0 deletions test/issue-175.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

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

test('double colon is replaced with single colon, no parameters', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: () => t.fail('should not be default route')
})

function handler (req, res, params) {
t.deepEqual(params, {})
}

findMyWay.on('GET', '/name::customVerb', handler)

findMyWay.lookup({ method: 'GET', url: '/name:customVerb' }, null)
})

test('exactly one match for static route with colon', t => {
t.plan(2)
const findMyWay = FindMyWay()

function handler () {}
findMyWay.on('GET', '/name::customVerb', handler)

t.equal(findMyWay.find('GET', '/name:customVerb').handler, handler)
t.equal(findMyWay.find('GET', '/name:test'), null)
})