Skip to content

Commit

Permalink
Fix multi-parametrical nodes with diff static ending (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-tymoshenko authored Jun 18, 2022
1 parent 009078c commit 97a9f0d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
8 changes: 6 additions & 2 deletions custom_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ class StaticNode extends ParentNode {
}

parametricChild = new ParametricNode(regex)
this.parametricChildren.push(parametricChild)
if (regex) {
this.parametricChildren.unshift(parametricChild)
} else {
this.parametricChildren.push(parametricChild)
}
return parametricChild
}

Expand Down Expand Up @@ -122,7 +126,7 @@ class StaticNode extends ParentNode {
})
}

for (let i = parametricBrotherNodeIndex; i < this.parametricChildren.length; i++) {
for (let i = this.parametricChildren.length - 1; i >= parametricBrotherNodeIndex; i--) {
nodeStack.push({
paramsCount,
brotherPathIndex: pathIndex,
Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ Router.prototype._on = function _on (method, path, opts, handler, store) {
let isRegexNode = false
const regexps = []

let staticEndingLength = 0
let lastParamStartIndex = i + 1
for (let j = lastParamStartIndex; ; j++) {
const charCode = path.charCodeAt(j)
Expand Down Expand Up @@ -234,6 +235,10 @@ Router.prototype._on = function _on (method, path, opts, handler, store) {

lastParamStartIndex = lastParamEndIndex + 1
j = lastParamEndIndex

if (path.charCodeAt(j) === 47 || j === path.length) {
staticEndingLength = staticPart.length
}
} else if (charCode === 47 || j === path.length) {
const paramName = path.slice(lastParamStartIndex, j)
params.push(paramName)
Expand All @@ -244,7 +249,8 @@ Router.prototype._on = function _on (method, path, opts, handler, store) {
}

if (path.charCodeAt(j) === 47 || j === path.length) {
path = path.slice(0, i + 1) + path.slice(j)
path = path.slice(0, i + 1) + path.slice(j - staticEndingLength)
i += staticEndingLength
break
}
}
Expand Down
81 changes: 81 additions & 0 deletions test/issue-238.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,84 @@ test('Multi-parametric tricky path', t => {
{ param1: 'static-static1', param2: 'static' }
)
})

test('Multi-parametric nodes with different static ending 1', t => {
t.plan(4)
const findMyWay = FindMyWay({
defaultRoute: () => t.fail('Should not be defaultRoute')
})

const paramHandler = () => {}
const multiParamHandler = () => {}

findMyWay.on('GET', '/v1/foo/:code', paramHandler)
findMyWay.on('GET', '/v1/foo/:code.png', multiParamHandler)

t.same(findMyWay.find('GET', '/v1/foo/hello', {}).handler, paramHandler)
t.same(findMyWay.find('GET', '/v1/foo/hello', {}).params, { code: 'hello' })

t.same(findMyWay.find('GET', '/v1/foo/hello.png', {}).handler, multiParamHandler)
t.same(findMyWay.find('GET', '/v1/foo/hello.png', {}).params, { code: 'hello' })
})

test('Multi-parametric nodes with different static ending 2', t => {
t.plan(4)
const findMyWay = FindMyWay({
defaultRoute: () => t.fail('Should not be defaultRoute')
})

const jpgHandler = () => {}
const pngHandler = () => {}

findMyWay.on('GET', '/v1/foo/:code.jpg', jpgHandler)
findMyWay.on('GET', '/v1/foo/:code.png', pngHandler)

t.same(findMyWay.find('GET', '/v1/foo/hello.jpg', {}).handler, jpgHandler)
t.same(findMyWay.find('GET', '/v1/foo/hello.jpg', {}).params, { code: 'hello' })

t.same(findMyWay.find('GET', '/v1/foo/hello.png', {}).handler, pngHandler)
t.same(findMyWay.find('GET', '/v1/foo/hello.png', {}).params, { code: 'hello' })
})

test('Multi-parametric nodes with different static ending 3', t => {
t.plan(4)
const findMyWay = FindMyWay({
defaultRoute: () => t.fail('Should not be defaultRoute')
})

const jpgHandler = () => {}
const pngHandler = () => {}

findMyWay.on('GET', '/v1/foo/:code.jpg/bar', jpgHandler)
findMyWay.on('GET', '/v1/foo/:code.png/bar', pngHandler)

t.same(findMyWay.find('GET', '/v1/foo/hello.jpg/bar', {}).handler, jpgHandler)
t.same(findMyWay.find('GET', '/v1/foo/hello.jpg/bar', {}).params, { code: 'hello' })

t.same(findMyWay.find('GET', '/v1/foo/hello.png/bar', {}).handler, pngHandler)
t.same(findMyWay.find('GET', '/v1/foo/hello.png/bar', {}).params, { code: 'hello' })
})

test('Multi-parametric nodes with different static ending 4', t => {
t.plan(6)
const findMyWay = FindMyWay({
defaultRoute: () => t.fail('Should not be defaultRoute')
})

const handler = () => {}
const jpgHandler = () => {}
const pngHandler = () => {}

findMyWay.on('GET', '/v1/foo/:code/bar', handler)
findMyWay.on('GET', '/v1/foo/:code.jpg/bar', jpgHandler)
findMyWay.on('GET', '/v1/foo/:code.png/bar', pngHandler)

t.same(findMyWay.find('GET', '/v1/foo/hello/bar', {}).handler, handler)
t.same(findMyWay.find('GET', '/v1/foo/hello/bar', {}).params, { code: 'hello' })

t.same(findMyWay.find('GET', '/v1/foo/hello.jpg/bar', {}).handler, jpgHandler)
t.same(findMyWay.find('GET', '/v1/foo/hello.jpg/bar', {}).params, { code: 'hello' })

t.same(findMyWay.find('GET', '/v1/foo/hello.png/bar', {}).handler, pngHandler)
t.same(findMyWay.find('GET', '/v1/foo/hello.png/bar', {}).params, { code: 'hello' })
})

0 comments on commit 97a9f0d

Please sign in to comment.