Skip to content

Commit

Permalink
ES6 module
Browse files Browse the repository at this point in the history
  • Loading branch information
stereobooster committed Sep 19, 2019
1 parent b8dbd71 commit d84aead
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 3 deletions.
18 changes: 15 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,21 @@ module.exports = function (grunt) {
},
copy: {
main: {
expand: true,
src: "tiny-lru.d.ts",
dest: "lib/"
files: [
{
expand: true,
src: "tiny-lru.d.ts",
dest: "lib/"
},
{
expand: true,
src: "src/lru.js",
dest: "lib/",
rename: function (dest) {
return dest + "tiny-lru.es6.js";
}
}
]
}
},
eslint: {
Expand Down
135 changes: 135 additions & 0 deletions lib/tiny-lru.es6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
class LRU {
constructor (max = 0, ttl = 0) {
this.first = null;
this.items = {};
this.last = null;
this.max = max;
this.size = 0;
this.ttl = ttl;
}

has (key) {
return key in this.items;
}

clear () {
this.first = null;
this.items = {};
this.last = null;
this.size = 0;

return this;
}

delete (key) {
if (this.has(key)) {
const item = this.items[key];

delete this.items[key];
this.size--;

if (item.prev !== null) {
item.prev.next = item.next;
}

if (item.next !== null) {
item.next.prev = item.prev;
}

if (this.first === item) {
this.first = item.next;
}

if (this.last === item) {
this.last = item.prev;
}
}

return this;
}

evict () {
const item = this.first;

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

return this;
}

get (key) {
let result;

if (this.has(key)) {
const item = this.items[key];

if (this.ttl > 0 && item.expiry <= new Date().getTime()) {
this.delete(key);
} else {
result = item.value;
this.set(key, result, true);
}
}

return result;
}

keys () {
return Object.keys(this.items);
}

set (key, value, bypass = false) {
let item;

if (bypass || this.has(key)) {
item = this.items[key];
item.value = value;

if (this.last !== item) {
const last = this.last,
next = item.next,
prev = item.prev;

if (this.first === item) {
this.first = item.next;
}

item.next = null;
item.prev = this.last;
last.next = item;

if (prev !== null) {
prev.next = next;
}

if (next !== null) {
next.prev = prev;
}
}
} else {
if (this.max > 0 && this.size === this.max) {
this.evict();
}

item = this.items[key] = {
expiry: this.ttl > 0 ? new Date().getTime() + this.ttl : this.ttl,
key: key,
prev: this.last,
next: null,
value
};

if (++this.size === 1) {
this.first = item;
} else {
this.last.next = item;
}
}

this.last = item;

return this;
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"license": "BSD-3-Clause",
"main": "lib/tiny-lru.js",
"module": "lib/tiny-lru.es6.js",
"types": "lib/tiny-lru.d.ts",
"engines": {
"node": ">=6"
Expand Down

0 comments on commit d84aead

Please sign in to comment.