Skip to content

Commit f90ec99

Browse files
committed
Deleting a node shouldn't delete all children
1 parent 7224fac commit f90ec99

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

lib/trie.js

-5
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@ URLTrie.prototype.remove = function (path) {
7474
return;
7575
}
7676
var part = path.shift();
77-
if (path.length === 0) {
78-
delete this.branches[part];
79-
this.size -= 1;
80-
return;
81-
}
8277
var child = this.branches[part];
8378
child.remove(path);
8479
if (child.size === 0 && child.data === undefined) {

test/trie_spec.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe("URLTrie", function () {
1313
'/a/b/c/d',
1414
'/a/b/d',
1515
'/a/b/e',
16+
'/b',
1617
'/b/c',
1718
'/b/c/d',
1819
];
@@ -137,11 +138,18 @@ describe("URLTrie", function () {
137138
it("trie_remove", function (done) {
138139
var trie = full_trie();
139140
var size = trie.size;
141+
var node;
142+
node = trie.get('/b/just-b');
143+
expect(node.prefix).toEqual('/b');
144+
140145
trie.remove('/b');
141-
expect(trie.size).toEqual(size - 1);
142-
expect(trie.get('/b/c/dword')).toBe(undefined);
146+
// deleting a node doesn't change size if no children
147+
expect(trie.size).toEqual(size);
148+
expect(trie.get('/b/just-b')).toBe(undefined);
149+
node = trie.get('/b/c/sub-still-here');
150+
expect(node.prefix).toEqual('/b/c');
143151

144-
var node = trie.get('/a/b/c/d/word');
152+
node = trie.get('/a/b/c/d/word');
145153
expect(node.prefix).toEqual('/a/b/c/d');
146154
var b = trie.branches.a.branches.b;
147155
expect(b.size).toEqual(3);

0 commit comments

Comments
 (0)