From 3d94506408c3965d91a96980e7b02c556a0283eb Mon Sep 17 00:00:00 2001 From: Michael Iwersen Date: Thu, 21 Jan 2016 22:46:02 +0100 Subject: [PATCH] Fixed an issue with exact match and similar paths in find and remove --- package.json | 2 +- src/tree.js | 4 ++-- test/tree.js | 9 +++++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 0bc992b..cd14e1b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "radix-tree", - "version": "0.3.1", + "version": "0.3.2", "description": "Node.js version of a radix tree usable for routers or url path based storage.", "main": "index.js", "engines": { diff --git a/src/tree.js b/src/tree.js index 0073376..0580a89 100644 --- a/src/tree.js +++ b/src/tree.js @@ -173,7 +173,7 @@ export class Tree { let child = node.children[index] if (child.type === Node.DEFAULT) { - if ( path[offset] === child.path[0] ) { + if ( path[offset] === child.path[0] && path.indexOf(child.path, offset) === offset ) { node = child offset += node.path.length @@ -291,7 +291,7 @@ export class Tree { let child = node.children[index] if (child.type === Node.DEFAULT) { - if ( path[offset] === child.path[0] ) { + if ( path[offset] === child.path[0] && path.indexOf(child.path, offset) === offset ) { node = child offset += node.path.length diff --git a/test/tree.js b/test/tree.js index c3498e9..32ecbc9 100644 --- a/test/tree.js +++ b/test/tree.js @@ -89,6 +89,15 @@ describe('Tree', function() { }) + it('should also work with very similar paths', function () { + let instance = new Tree() + + instance.add('/test1') + + expect(instance.find('/test1')).to.be.ok + expect(instance.find('/test2')).to.be.not.ok + }) + it('should return the node on exact match of the path', function () { let instance = new Tree()