Skip to content

Commit

Permalink
Merge pull request #11 from evangelion1204/error-with-empty-paths
Browse files Browse the repository at this point in the history
FIxed a bug with / and /: or /* generating an empty node
  • Loading branch information
evangelion1204 committed Jan 31, 2016
2 parents 3647460 + 62c3244 commit d2f0a36
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "radix-tree",
"version": "0.3.3",
"version": "0.3.4",
"description": "Node.js version of a radix tree usable for routers or url path based storage.",
"main": "index.js",
"engines": {
Expand Down
20 changes: 12 additions & 8 deletions src/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,13 @@ export class Tree {
throw new Error('Param node can not be appended to an already existing path')
}

child.path = path.substr(offset, index - offset)
if (offset < index - offset) {
child.path = path.substr(offset, index - offset)

offset = index
node.append(child)
node = child
offset = index
node.append(child)
node = child
}

child = new Node()
child.type = Node.PARAM
Expand All @@ -125,11 +127,13 @@ export class Tree {
throw new Error('Param node can not be appended to an already existing path')
}

child.path = path.substr(offset, index - offset)
if (offset < index - offset) {
child.path = path.substr(offset, index - offset)

offset = index
node.append(child)
node = child
offset = index
node.append(child)
node = child
}

child = new Node()
child.type = Node.CATCHALL
Expand Down
21 changes: 21 additions & 0 deletions test/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ describe('Tree', function() {
expect(instance.find('/users/testuser/messages/123')).to.deep.equal({path: '/users/:userId/messages/:messageId', params: {userId: 'testuser', messageId: '123'}})
})

it('should return the catchall param', function () {
let instance = new Tree()

instance.add('/')
instance.add('/:id')

expect(instance.find('/')).to.deep.equal({path: '/'})
expect(instance.find('/some')).to.deep.equal({path: '/:id', params: {id: 'some'}})
})


it('should return the catchall param', function () {
let instance = new Tree()

Expand All @@ -146,6 +157,16 @@ describe('Tree', function() {
expect(instance.find('/users/some/path')).to.deep.equal({path: '/users/*api', params: {api: 'some/path'}})
})

it('should return the catchall param', function () {
let instance = new Tree()

instance.add('/')
instance.add('/*api')

expect(instance.find('/')).to.deep.equal({path: '/'})
expect(instance.find('/some/path')).to.deep.equal({path: '/*api', params: {api: 'some/path'}})
})

it('should be possible to mix both types', function () {
let instance = new Tree()

Expand Down

0 comments on commit d2f0a36

Please sign in to comment.