Skip to content

Commit

Permalink
Fixing evict() such that it handles a size of 0 and 1 properly
Browse files Browse the repository at this point in the history
(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
  • Loading branch information
avoidwork committed Feb 13, 2022
1 parent 39a2161 commit c00f2cb
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 22 deletions.
22 changes: 15 additions & 7 deletions lib/tiny-lru.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,21 @@ class LRU {
return this;
}

evict () {
const item = this.first;
evict (bypass = false) {
if (bypass || this.size > 0) {
const item = this.first;

delete this.items[item.key];
this.first = item.next;
this.first.prev = null;
this.size--;
delete this.items[item.key];
this.size--;

if (this.size === 0) {
this.first = null;
this.last = null;
} else {
this.first = item.next;
this.first.prev = null;
}
}

return this;
}
Expand Down Expand Up @@ -112,7 +120,7 @@ class LRU {
}
} else {
if (this.max > 0 && this.size === this.max) {
this.evict();
this.evict(true);
}

item = this.items[key] = {
Expand Down
2 changes: 1 addition & 1 deletion lib/tiny-lru.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/tiny-lru.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions src/lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,21 @@
return this;
}

evict () {
const item = this.first;
evict (bypass = false) {
if (bypass || this.size > 0) {
const item = this.first;

delete this.items[item.key];
this.first = item.next;
this.first.prev = null;
this.size--;
delete this.items[item.key];
this.size--;

if (this.size === 0) {
this.first = null;
this.last = null;
} else {
this.first = item.next;
this.first.prev = null;
}
}

return this;
}
Expand Down Expand Up @@ -110,7 +118,7 @@
}
} else {
if (this.max > 0 && this.size === this.max) {
this.evict();
this.evict(true);
}

item = this.items[key] = {
Expand Down
30 changes: 24 additions & 6 deletions test/lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,38 @@ exports.deletion = {

exports.smallEvict = {
setUp: function (done) {
this.cache = lru(4);
this.items = ["a"];
this.cache = lru(1);
this.items = ["a", "b"];
done();
},
test: function (test) {
this.items.forEach(i => this.cache.set(i, false));
test.expect(6);
test.equal(this.cache.first.key, "a", "Should be 'a'");
test.equal(this.cache.last.key, "a", "Should be 'a'");
test.equal(this.cache.first.key, "b", "Should be 'b'");
test.equal(this.cache.last.key, "b", "Should be 'b'");
test.equal(this.cache.size, 1, "Should be '1'");
this.cache.evict();
test.equal(this.cache.first.key, null, "Should be 'null'");
test.equal(this.cache.last.key, null, "Should be 'null'");
test.equal(this.cache.first, null, "Should be 'null'");
test.equal(this.cache.last, null, "Should be 'null'");
test.equal(this.cache.size, 0, "Should be 'null'");
test.done();
}
};

exports.emptyEvict = {
setUp: function (done) {
this.cache = lru(1);
done();
},
test: function (test) {
test.expect(6);
test.equal(this.cache.first, null, "Should be 'null'");
test.equal(this.cache.last, null, "Should be 'null'");
test.equal(this.cache.size, 0, "Should be 'null'");
this.cache.evict();
test.equal(this.cache.first, null, "Should be 'null'");
test.equal(this.cache.last, null, "Should be 'null'");
test.equal(this.cache.size, 0, "Should be 'null'");
test.done();
}
};

0 comments on commit c00f2cb

Please sign in to comment.