Skip to content

Commit

Permalink
Merge pull request #57 from delvedor/fix-49
Browse files Browse the repository at this point in the history
Fixes #49
  • Loading branch information
delvedor committed Feb 1, 2018
2 parents 71d793c + a311dcb commit 7821471
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 8 deletions.
25 changes: 17 additions & 8 deletions node.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,26 @@ Node.prototype.add = function (node) {
this.wildcardChild = node
}

if ([this.types.PARAM, this.types.REGEX, this.types.MULTI_PARAM].indexOf(node.kind) > -1) {
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++

// Search for a parametric brother and store it in a variable
var parametricBrother = null
for (var i = 0; i < this.numberOfChildren; i++) {
const child = this.children[i]
if ([this.types.PARAM, this.types.REGEX, this.types.MULTI_PARAM].indexOf(child.kind) > -1) {
parametricBrother = child
break
}
}

// Save the parametric brother inside a static child
for (i = 0; i < this.numberOfChildren; i++) {
if (this.children[i].kind === this.types.STATIC && parametricBrother) {
this.children[i].parametricBrother = parametricBrother
}
}
}

Node.prototype.findByLabel = function (label) {
Expand Down
109 changes: 109 additions & 0 deletions test/issue-49.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
'use strict'

const t = require('tap')
const test = t.test
const FindMyWay = require('../')
const noop = () => {}

test('Defining static route after parametric - 1', t => {
t.plan(3)
const findMyWay = FindMyWay()

findMyWay.on('GET', '/static', noop)
findMyWay.on('GET', '/:param', noop)

t.ok(findMyWay.find('GET', '/static'))
t.ok(findMyWay.find('GET', '/para'))
t.ok(findMyWay.find('GET', '/s'))
})

test('Defining static route after parametric - 2', t => {
t.plan(3)
const findMyWay = FindMyWay()

findMyWay.on('GET', '/:param', noop)
findMyWay.on('GET', '/static', noop)

t.ok(findMyWay.find('GET', '/static'))
t.ok(findMyWay.find('GET', '/para'))
t.ok(findMyWay.find('GET', '/s'))
})

test('Defining static route after parametric - 3', t => {
t.plan(4)
const findMyWay = FindMyWay()

findMyWay.on('GET', '/:param', noop)
findMyWay.on('GET', '/static', noop)
findMyWay.on('GET', '/other', noop)

t.ok(findMyWay.find('GET', '/static'))
t.ok(findMyWay.find('GET', '/para'))
t.ok(findMyWay.find('GET', '/s'))
t.ok(findMyWay.find('GET', '/o'))
})

test('Defining static route after parametric - 4', t => {
t.plan(4)
const findMyWay = FindMyWay()

findMyWay.on('GET', '/static', noop)
findMyWay.on('GET', '/other', noop)
findMyWay.on('GET', '/:param', noop)

t.ok(findMyWay.find('GET', '/static'))
t.ok(findMyWay.find('GET', '/para'))
t.ok(findMyWay.find('GET', '/s'))
t.ok(findMyWay.find('GET', '/o'))
})

test('Defining static route after parametric - 5', t => {
t.plan(4)
const findMyWay = FindMyWay()

findMyWay.on('GET', '/static', noop)
findMyWay.on('GET', '/:param', noop)
findMyWay.on('GET', '/other', noop)

t.ok(findMyWay.find('GET', '/static'))
t.ok(findMyWay.find('GET', '/para'))
t.ok(findMyWay.find('GET', '/s'))
t.ok(findMyWay.find('GET', '/o'))
})

test('Should produce the same tree - 1', t => {
t.plan(1)
const findMyWay1 = FindMyWay()
const findMyWay2 = FindMyWay()

findMyWay1.on('GET', '/static', noop)
findMyWay1.on('GET', '/:param', noop)

findMyWay2.on('GET', '/:param', noop)
findMyWay2.on('GET', '/static', noop)

t.deepEqual(findMyWay1.tree, findMyWay2.tree)
})

test('Should produce the same tree - 2', t => {
t.plan(3)
const findMyWay1 = FindMyWay()
const findMyWay2 = FindMyWay()
const findMyWay3 = FindMyWay()

findMyWay1.on('GET', '/:param', noop)
findMyWay1.on('GET', '/static', noop)
findMyWay1.on('GET', '/other', noop)

findMyWay2.on('GET', '/static', noop)
findMyWay2.on('GET', '/:param', noop)
findMyWay2.on('GET', '/other', noop)

findMyWay3.on('GET', '/static', noop)
findMyWay3.on('GET', '/other', noop)
findMyWay3.on('GET', '/:param', noop)

t.deepEqual(findMyWay1.tree, findMyWay2.tree)
t.deepEqual(findMyWay2.tree, findMyWay3.tree)
t.deepEqual(findMyWay1.tree, findMyWay3.tree)
})

0 comments on commit 7821471

Please sign in to comment.