Skip to content

Commit

Permalink
Merge pull request #38 from delvedor/fix-params-object
Browse files Browse the repository at this point in the history
Fixed wrong params object reference
  • Loading branch information
delvedor authored Oct 23, 2017
2 parents da01124 + 05c0660 commit bcacd88
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,18 @@ Router.prototype.find = function find (method, path) {
if (pathLen === 0 || path === prefix) {
var handle = currentNode.getHandler(method)
if (handle !== undefined) {
var paramsObj = {}
if (handle.paramsLength > 0) {
var paramNames = handle.params

for (i = 0; i < handle.paramsLength; i++) {
handle.paramsObj[paramNames[i]] = params[i]
paramsObj[paramNames[i]] = params[i]
}
}

return {
handler: handle.handler,
params: handle.paramsObj,
params: paramsObj,
store: handle.store
}
}
Expand Down
8 changes: 1 addition & 7 deletions node.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,11 @@ Node.prototype.find = function (label, method) {
Node.prototype.setHandler = function (method, handler, params, store) {
if (!handler) return

var paramsObj = {}
for (var i = 0; i < params.length; i++) {
paramsObj[params[i]] = ''
}

this.map[method] = {
handler: handler,
params: params,
store: store || null,
paramsLength: params.length,
paramsObj: paramsObj
paramsLength: params.length
}
}

Expand Down
22 changes: 22 additions & 0 deletions test/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,25 @@ test('parametric route with different method', t => {
findMyWay.lookup({ method: 'GET', url: '/test/hello' }, null)
findMyWay.lookup({ method: 'POST', url: '/test/world' }, null)
})

test('params does not keep the object reference', t => {
t.plan(2)
const findMyWay = FindMyWay()
var first = true

findMyWay.on('GET', '/test/:id', (req, res, params) => {
if (first) {
setTimeout(() => {
t.is(params.id, 'hello')
}, 10)
} else {
setTimeout(() => {
t.is(params.id, 'world')
}, 10)
}
first = false
})

findMyWay.lookup({ method: 'GET', url: '/test/hello' }, null)
findMyWay.lookup({ method: 'GET', url: '/test/world' }, null)
})

0 comments on commit bcacd88

Please sign in to comment.