Skip to content

Commit

Permalink
Reducing code
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Dec 9, 2018
1 parent 9576b00 commit 31bc3eb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 64 deletions.
45 changes: 14 additions & 31 deletions lib/tiny-lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,29 @@
(function (global) {
const empty = null;

function link (item, key, a, b) {
item[a] = key;
function link (item, key) {
item.next = key;

if (item[b] === key) {
item[b] = empty;
if (item.previous === key) {
item.previous = empty;
}
}

function reset () {
this.cache = {};
this.first = empty;
this.last = empty;
this.length = 0;

return this;
}

class LRU {
constructor (max, ttl) {
this.cache = {};
this.first = empty;
this.last = empty;
this.length = 0;
this.max = max;
this.ttl = ttl;
reset.call(this);
}

clear () {
reset.call(this);
this.cache = {};
this.first = empty;
this.last = empty;
this.length = 0;

return this;
}
Expand All @@ -46,18 +43,6 @@
return this.remove(key);
}

dump () {
return JSON.stringify({
cache: this.cache,
first: this.first,
last: this.last,
length: this.length,
max: this.max,
notify: this.notify,
ttl: this.ttl
});
}

evict () {
this.remove(this.last, true);

Expand Down Expand Up @@ -85,8 +70,6 @@
return key in this.cache;
}

onchange () {}

remove (key, bypass = false) {
let result;

Expand Down Expand Up @@ -128,7 +111,7 @@

item.next = empty;
item.previous = this.first;
link(this.cache[this.first], key, "next", "previous");
link(this.cache[this.first], key);

if (n !== empty && n !== this.first) {
if (p !== empty) {
Expand Down Expand Up @@ -160,7 +143,7 @@
}

if (this.first !== empty && this.first !== key) {
link(this.cache[this.first], key, "next", "previous");
link(this.cache[this.first], key);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/tiny-lru.min.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.min.js.map

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

17 changes: 4 additions & 13 deletions src/intro.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,10 @@
(function (global) {
const empty = null;

function link (item, key, a, b) {
item[a] = key;
function link (item, key) {
item.next = key;

if (item[b] === key) {
item[b] = empty;
if (item.previous === key) {
item.previous = empty;
}
}

function reset () {
this.cache = {};
this.first = empty;
this.last = empty;
this.length = 0;

return this;
}

0 comments on commit 31bc3eb

Please sign in to comment.