Skip to content

Commit e52764b

Browse files
authored
Merge pull request #93 from minrk/no-delete-if-children
Deleting a node shouldn't delete all children
2 parents 7224fac + 4a518a6 commit e52764b

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

lib/trie.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ 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;
77+
var child = this.branches[part];
78+
if (child === undefined) {
79+
// Requested node doesn't exist,
80+
// consider it already removed.
8081
return;
8182
}
82-
var child = this.branches[part];
8383
child.remove(path);
8484
if (child.size === 0 && child.data === undefined) {
8585
// child has no branches and is not a leaf

test/jasmine.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"spec_dir": "test",
33
"stopSpecOnExpectationFailure": false,
4-
"spec_files": ["*spec.js"]
4+
"spec_files": ["store_spec.js"]
55
}

test/store_spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe("MemoryStore", function () {
102102

103103
it("doesn't explode when route is not defined", function (done) {
104104
// would blow up if an error was thrown
105-
this.subject.remove("/my_route", done);
105+
this.subject.remove("/my_route/foo/bar", done);
106106
});
107107
});
108108

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)