Skip to content

Commit

Permalink
Merge pull request #45 from delvedor/fix-44
Browse files Browse the repository at this point in the history
Fixes 44
  • Loading branch information
delvedor authored Dec 5, 2017
2 parents dc2293f + d1dd7b0 commit 2d8e8ba
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 8 deletions.
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ Router.prototype.find = function find (method, path) {
var prefix = currentNode.prefix
var prefixLen = prefix.length
var len = 0
var previousPath = path

// found the route
if (pathLen === 0 || path === prefix) {
Expand Down Expand Up @@ -283,7 +284,13 @@ Router.prototype.find = function find (method, path) {

var node = currentNode.find(path[0], method)
if (node === null) {
return getWildcardNode(wildcardNode, method, originalPath, pathLenWildcard)
node = currentNode.parametricBrother
if (node === null) {
return getWildcardNode(wildcardNode, method, originalPath, pathLenWildcard)
}
path = previousPath
pathLen = previousPath.length
len = prefixLen
}
var kind = node.kind

Expand Down
10 changes: 10 additions & 0 deletions node.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@ function Node (prefix, children, kind, map, regex) {
this.map = map || {}
this.regex = regex || null
this.wildcardChild = null
this.parametricBrother = null
}

Node.prototype.add = function (node) {
if (node.kind === 2) {
this.wildcardChild = node
}

if (node.kind === 1 || node.kind === 3 || node.kind === 4) {
for (var i = 0; i < this.numberOfChildren; i++) {
if (this.children[i].kind === 0) {
this.children[i].parametricBrother = node
}
}
}

this.children.push(node)
this.children.sort((n1, n2) => n1.kind - n2.kind)
this.numberOfChildren++
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"main": "index.js",
"scripts": {
"bench": "node bench.js",
"test": "standard && tap test/*.test.js",
"coveralls": "tap test/*.test.js --cov --coverage-report=text-lcov | coveralls"
"test": "standard && tap -j4 test/*.test.js",
"coveralls": "tap -j4 test/*.test.js --cov --coverage-report=text-lcov | coveralls"
},
"repository": {
"type": "git",
Expand All @@ -27,9 +27,9 @@
"homepage": "https://github.com/delvedor/find-my-way#readme",
"devDependencies": {
"benchmark": "^2.1.4",
"coveralls": "^2.13.1",
"request": "^2.81.0",
"coveralls": "^3.0.0",
"request": "^2.83.0",
"standard": "^10.0.3",
"tap": "^10.7.2"
"tap": "^11.0.0"
}
}
131 changes: 131 additions & 0 deletions test/issue-44.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
'use strict'

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

test('Parametric and static with shared prefix / 1', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})

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

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

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

test('Parametric and static with shared prefix / 2', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})

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

findMyWay.on('GET', '/:param', (req, res, params) => {
t.fail('we should not be here')
})

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

test('Parametric and static with shared prefix (nested)', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.ok('We should be here')
}
})

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

findMyWay.on('GET', '/:param', (req, res, params) => {
t.fail('we should not be here')
})

findMyWay.lookup({ method: 'GET', url: '/winter/coming' }, null)
})

test('Parametric and static with shared prefix (with wildcard)', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})

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

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

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

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

test('Parametric and static with shared prefix (nested with wildcard)', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})

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

findMyWay.on('GET', '/:param', (req, res, params) => {
t.fail('we should not be here')
})

findMyWay.on('GET', '/*', (req, res, params) => {
t.equal(params['*'], 'winter/coming')
})

findMyWay.lookup({ method: 'GET', url: '/winter/coming' }, null)
})

test('Parametric and static with shared prefix (nested with split)', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here')
}
})

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

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

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

findMyWay.lookup({ method: 'GET', url: '/winter' }, null)
})
4 changes: 2 additions & 2 deletions test/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ test('Static parametric with shared part of the path', t => {

const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.is(req.url, '/example/shared/nested/oops')
t.is(req.url, '/example/shared/nested/oopss')
}
})

Expand All @@ -582,7 +582,7 @@ test('Static parametric with shared part of the path', t => {
t.is(params.param, 'other')
})

findMyWay.lookup({ method: 'GET', url: '/example/shared/nested/oops' }, null)
findMyWay.lookup({ method: 'GET', url: '/example/shared/nested/oopss' }, null)
findMyWay.lookup({ method: 'GET', url: '/example/other/nested/oops' }, null)
})

Expand Down

0 comments on commit 2d8e8ba

Please sign in to comment.