Skip to content

Commit c00f2cb

Browse files
committed
Fixing evict() such that it handles a size of 0 and 1 properly
(redo) Fixing `evict()` such that it handles a size of 0 and 1 properly Adding an empty eviction test Minor tweak to new test need more coffee need more coffee part 2
1 parent 39a2161 commit c00f2cb

File tree

5 files changed

+56
-22
lines changed

5 files changed

+56
-22
lines changed

lib/tiny-lru.cjs.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,21 @@ class LRU {
5050
return this;
5151
}
5252

53-
evict () {
54-
const item = this.first;
53+
evict (bypass = false) {
54+
if (bypass || this.size > 0) {
55+
const item = this.first;
5556

56-
delete this.items[item.key];
57-
this.first = item.next;
58-
this.first.prev = null;
59-
this.size--;
57+
delete this.items[item.key];
58+
this.size--;
59+
60+
if (this.size === 0) {
61+
this.first = null;
62+
this.last = null;
63+
} else {
64+
this.first = item.next;
65+
this.first.prev = null;
66+
}
67+
}
6068

6169
return this;
6270
}
@@ -112,7 +120,7 @@ class LRU {
112120
}
113121
} else {
114122
if (this.max > 0 && this.size === this.max) {
115-
this.evict();
123+
this.evict(true);
116124
}
117125

118126
item = this.items[key] = {

lib/tiny-lru.esm.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/tiny-lru.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lru.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,21 @@
4848
return this;
4949
}
5050

51-
evict () {
52-
const item = this.first;
51+
evict (bypass = false) {
52+
if (bypass || this.size > 0) {
53+
const item = this.first;
5354

54-
delete this.items[item.key];
55-
this.first = item.next;
56-
this.first.prev = null;
57-
this.size--;
55+
delete this.items[item.key];
56+
this.size--;
57+
58+
if (this.size === 0) {
59+
this.first = null;
60+
this.last = null;
61+
} else {
62+
this.first = item.next;
63+
this.first.prev = null;
64+
}
65+
}
5866

5967
return this;
6068
}
@@ -110,7 +118,7 @@
110118
}
111119
} else {
112120
if (this.max > 0 && this.size === this.max) {
113-
this.evict();
121+
this.evict(true);
114122
}
115123

116124
item = this.items[key] = {

test/lru.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,38 @@ exports.deletion = {
7171

7272
exports.smallEvict = {
7373
setUp: function (done) {
74-
this.cache = lru(4);
75-
this.items = ["a"];
74+
this.cache = lru(1);
75+
this.items = ["a", "b"];
7676
done();
7777
},
7878
test: function (test) {
7979
this.items.forEach(i => this.cache.set(i, false));
8080
test.expect(6);
81-
test.equal(this.cache.first.key, "a", "Should be 'a'");
82-
test.equal(this.cache.last.key, "a", "Should be 'a'");
81+
test.equal(this.cache.first.key, "b", "Should be 'b'");
82+
test.equal(this.cache.last.key, "b", "Should be 'b'");
8383
test.equal(this.cache.size, 1, "Should be '1'");
8484
this.cache.evict();
85-
test.equal(this.cache.first.key, null, "Should be 'null'");
86-
test.equal(this.cache.last.key, null, "Should be 'null'");
85+
test.equal(this.cache.first, null, "Should be 'null'");
86+
test.equal(this.cache.last, null, "Should be 'null'");
8787
test.equal(this.cache.size, 0, "Should be 'null'");
8888
test.done();
8989
}
9090
};
91+
92+
exports.emptyEvict = {
93+
setUp: function (done) {
94+
this.cache = lru(1);
95+
done();
96+
},
97+
test: function (test) {
98+
test.expect(6);
99+
test.equal(this.cache.first, null, "Should be 'null'");
100+
test.equal(this.cache.last, null, "Should be 'null'");
101+
test.equal(this.cache.size, 0, "Should be 'null'");
102+
this.cache.evict();
103+
test.equal(this.cache.first, null, "Should be 'null'");
104+
test.equal(this.cache.last, null, "Should be 'null'");
105+
test.equal(this.cache.size, 0, "Should be 'null'");
106+
test.done();
107+
}
108+
};

0 commit comments

Comments
 (0)