diff --git a/lib/lru.d.ts b/lib/lru.d.ts deleted file mode 100644 index 204d0ee..0000000 --- a/lib/lru.d.ts +++ /dev/null @@ -1,18 +0,0 @@ -declare class LRU { - private first; - private items; - private last; - private max; - private size; - private ttl; - constructor(max?: number, ttl?: number); - has(key: string): boolean; - clear(): this; - delete(key: string): this; - evict(): this; - get(key: string): T; - keys(): string[]; - set(key: string, value: T, bypass?: boolean): this; -} -export default function factory(max?: number, ttl?: number): LRU; -export {}; diff --git a/lib/tiny-lru.cjs.js b/lib/tiny-lru.cjs.js index 50be73a..712da54 100644 --- a/lib/tiny-lru.cjs.js +++ b/lib/tiny-lru.cjs.js @@ -1,120 +1,151 @@ 'use strict'; -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; - } -} -function factory(max = 1000, ttl = 0) { - if (isNaN(max) || max < 0) { - throw new TypeError("Invalid max value"); - } - if (isNaN(ttl) || ttl < 0) { - throw new TypeError("Invalid ttl value"); - } - return new LRU(max, ttl); -} +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; + } + } + + function factory (max = 1000, ttl = 0) { + if (isNaN(max) || max < 0) { + throw new TypeError("Invalid max value"); + } + + if (isNaN(ttl) || ttl < 0) { + throw new TypeError("Invalid ttl value"); + } + + return new LRU(max, ttl); + } module.exports = factory; diff --git a/lib/tiny-lru.esm.js b/lib/tiny-lru.esm.js index b37987e..c14a83f 100644 --- a/lib/tiny-lru.esm.js +++ b/lib/tiny-lru.esm.js @@ -1,118 +1,149 @@ -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; - } -} -function factory(max = 1000, ttl = 0) { - if (isNaN(max) || max < 0) { - throw new TypeError("Invalid max value"); - } - if (isNaN(ttl) || ttl < 0) { - throw new TypeError("Invalid ttl value"); - } - return new LRU(max, ttl); -} +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; + } + } + + function factory (max = 1000, ttl = 0) { + if (isNaN(max) || max < 0) { + throw new TypeError("Invalid max value"); + } + + if (isNaN(ttl) || ttl < 0) { + throw new TypeError("Invalid ttl value"); + } + + return new LRU(max, ttl); + } export default factory; diff --git a/package-lock.json b/package-lock.json index d4d145c..df4fee9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,18 +14,18 @@ } }, "@babel/core": { - "version": "7.6.4", - "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.6.4.tgz", - "integrity": "sha512-Rm0HGw101GY8FTzpWSyRbki/jzq+/PkNQJ+nSulrdY6gFGOsNseCqD6KHRYe2E+EdzuBdr2pxCp6s4Uk6eJ+XQ==", + "version": "7.6.2", + "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.6.2.tgz", + "integrity": "sha512-l8zto/fuoZIbncm+01p8zPSDZu/VuuJhAfA7d/AbzM09WR7iVhavvfNDYCNpo1VvLk6E6xgAoP9P+/EMJHuRkQ==", "dev": true, "requires": { "@babel/code-frame": "^7.5.5", - "@babel/generator": "^7.6.4", + "@babel/generator": "^7.6.2", "@babel/helpers": "^7.6.2", - "@babel/parser": "^7.6.4", + "@babel/parser": "^7.6.2", "@babel/template": "^7.6.0", - "@babel/traverse": "^7.6.3", - "@babel/types": "^7.6.3", + "@babel/traverse": "^7.6.2", + "@babel/types": "^7.6.0", "convert-source-map": "^1.1.0", "debug": "^4.1.0", "json5": "^2.1.0", @@ -33,54 +33,6 @@ "resolve": "^1.3.2", "semver": "^5.4.1", "source-map": "^0.5.0" - }, - "dependencies": { - "@babel/generator": { - "version": "7.6.4", - "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.6.4.tgz", - "integrity": "sha512-jsBuXkFoZxk0yWLyGI9llT9oiQ2FeTASmRFE32U+aaDTfoE92t78eroO7PTpU/OrYq38hlcDM6vbfLDaOLy+7w==", - "dev": true, - "requires": { - "@babel/types": "^7.6.3", - "jsesc": "^2.5.1", - "lodash": "^4.17.13", - "source-map": "^0.5.0" - } - }, - "@babel/parser": { - "version": "7.6.4", - "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.6.4.tgz", - "integrity": "sha512-D8RHPW5qd0Vbyo3qb+YjO5nvUVRTXFLQ/FsDxJU2Nqz4uB5EnUN0ZQSEYpvTIbRuttig1XbHWU5oMeQwQSAA+A==", - "dev": true - }, - "@babel/traverse": { - "version": "7.6.3", - "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.6.3.tgz", - "integrity": "sha512-unn7P4LGsijIxaAJo/wpoU11zN+2IaClkQAxcJWBNCMS6cmVh802IyLHNkAjQ0iYnRS3nnxk5O3fuXW28IMxTw==", - "dev": true, - "requires": { - "@babel/code-frame": "^7.5.5", - "@babel/generator": "^7.6.3", - "@babel/helper-function-name": "^7.1.0", - "@babel/helper-split-export-declaration": "^7.4.4", - "@babel/parser": "^7.6.3", - "@babel/types": "^7.6.3", - "debug": "^4.1.0", - "globals": "^11.1.0", - "lodash": "^4.17.13" - } - }, - "@babel/types": { - "version": "7.6.3", - "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.6.3.tgz", - "integrity": "sha512-CqbcpTxMcpuQTMhjI37ZHVgjBkysg5icREQIEZ0eG1yCNwg3oy+5AaLiOKmjsCj6nqOsa6Hf0ObjRVwokb7srA==", - "dev": true, - "requires": { - "esutils": "^2.0.2", - "lodash": "^4.17.13", - "to-fast-properties": "^2.0.0" - } - } } }, "@babel/generator": { @@ -168,9 +120,9 @@ } }, "@babel/runtime": { - "version": "7.6.3", - "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.6.3.tgz", - "integrity": "sha512-kq6anf9JGjW8Nt5rYfEuGRaEAaH1mkv3Bbu6rYvLOpPh/RusSJXuKPEAoZ7L7gybZkchE8+NV5g9vKF4AGAtsA==", + "version": "7.6.2", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.6.2.tgz", + "integrity": "sha512-EXxN64agfUqqIGeEjI5dL5z0Sw0ZwWo1mLTi4mQowCZ42O59b7DRpZAnTC6OqdF28wMBMFKNb/4uFGrVaigSpg==", "dev": true, "requires": { "regenerator-runtime": "^0.13.2" @@ -663,28 +615,28 @@ } }, "@nodelib/fs.scandir": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.3.tgz", - "integrity": "sha512-eGmwYQn3gxo4r7jdQnkrrN6bY478C3P+a/y72IJukF8LjB6ZHeB3c+Ehacj3sYeSmUXGlnA67/PmbM9CVwL7Dw==", + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.2.tgz", + "integrity": "sha512-wrIBsjA5pl13f0RN4Zx4FNWmU71lv03meGKnqRUoCyan17s4V3WL92f3w3AIuWbNnpcrQyFBU5qMavJoB8d27w==", "dev": true, "requires": { - "@nodelib/fs.stat": "2.0.3", + "@nodelib/fs.stat": "2.0.2", "run-parallel": "^1.1.9" } }, "@nodelib/fs.stat": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/@nodelib/fs.stat/-/fs.stat-2.0.3.tgz", - "integrity": "sha512-bQBFruR2TAwoevBEd/NWMoAAtNGzTRgdrqnYCc7dhzfoNvqPzLyqlEQnzZ3kVnNrSp25iyxE00/3h2fqGAGArA==", + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/@nodelib/fs.stat/-/fs.stat-2.0.2.tgz", + "integrity": "sha512-z8+wGWV2dgUhLqrtRYa03yDx4HWMvXKi1z8g3m2JyxAx8F7xk74asqPk5LAETjqDSGLFML/6CDl0+yFunSYicw==", "dev": true }, "@nodelib/fs.walk": { - "version": "1.2.4", - "resolved": "https://registry.npmjs.org/@nodelib/fs.walk/-/fs.walk-1.2.4.tgz", - "integrity": "sha512-1V9XOY4rDW0rehzbrcqAmHnz8e7SKvX27gh8Gt2WgB0+pdzdiLV83p72kZPU+jvMbS1qU5mauP2iOvO8rhmurQ==", + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/@nodelib/fs.walk/-/fs.walk-1.2.3.tgz", + "integrity": "sha512-l6t8xEhfK9Sa4YO5mIRdau7XSOADfmh3jCr0evNHdY+HNkW6xuQhgMH7D73VV6WpZOagrW0UludvMTiifiwTfA==", "dev": true, "requires": { - "@nodelib/fs.scandir": "2.1.3", + "@nodelib/fs.scandir": "2.1.2", "fastq": "^1.6.0" } }, @@ -784,9 +736,9 @@ "dev": true }, "@types/node": { - "version": "12.7.12", - "resolved": "https://registry.npmjs.org/@types/node/-/node-12.7.12.tgz", - "integrity": "sha512-KPYGmfD0/b1eXurQ59fXD1GBzhSQfz6/lKBxkaHX9dKTzjXbK68Zt7yGUxUsCS1jeTy/8aL+d9JEr+S54mpkWQ==", + "version": "12.7.8", + "resolved": "https://registry.npmjs.org/@types/node/-/node-12.7.8.tgz", + "integrity": "sha512-FMdVn84tJJdV+xe+53sYiZS4R5yn1mAIxfj+DVoNiQjTYz1+OYmjwEZr1ev9nU0axXwda0QDbYl06QHanRVH3A==", "dev": true }, "@types/resolve": { @@ -856,13 +808,13 @@ "dev": true }, "aggregate-error": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/aggregate-error/-/aggregate-error-3.0.1.tgz", - "integrity": "sha512-quoaXsZ9/BLNae5yiNoUz+Nhkwz83GhWwtYFglcjEQB2NDHCIpApbqXxIFnm4Pq/Nvhrsq5sYJFyohrrxnTGAA==", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/aggregate-error/-/aggregate-error-3.0.0.tgz", + "integrity": "sha512-yKD9kEoJIR+2IFqhMwayIBgheLYbB3PS2OBhWae1L/ODTd/JF/30cW0bc9TqzRL3k4U41Dieu3BF4I29p8xesA==", "dev": true, "requires": { "clean-stack": "^2.0.0", - "indent-string": "^4.0.0" + "indent-string": "^3.2.0" } }, "ajv": { @@ -1714,12 +1666,6 @@ "delayed-stream": "~1.0.0" } }, - "command-exists": { - "version": "1.2.8", - "resolved": "https://registry.npmjs.org/command-exists/-/command-exists-1.2.8.tgz", - "integrity": "sha512-PM54PkseWbiiD/mMsbvW351/u+dafwTJ0ye2qB60G1aGQP9j3xK2gmMDc+R34L3nDtx4qMCitXT75mkbkGJDLw==", - "dev": true - }, "commander": { "version": "2.20.0", "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.0.tgz", @@ -1895,9 +1841,9 @@ "dev": true }, "deepmerge": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.1.1.tgz", - "integrity": "sha512-+qO5WbNBKBaZez95TffdUDnGIo4+r5kmsX8aOb7PDHvXsTbghAmleuxjs6ytNaf5Eg4FGBXDS5vqO61TRi6BMg==", + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.0.0.tgz", + "integrity": "sha512-YZ1rOP5+kHor4hMAH+HRQnBQHg+wvS1un1hAOuIcxcBy0hzcUf6Jg2a1w65kpoOUnurOfZbERwjI1TfZxNjcww==", "dev": true }, "default-require-extensions": { @@ -2089,9 +2035,9 @@ } }, "es-abstract": { - "version": "1.15.0", - "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.15.0.tgz", - "integrity": "sha512-bhkEqWJ2t2lMeaJDuk7okMkJWI/yqgH/EoGwpcvv0XW9RWQsRspI4wt6xuyuvMvvQE3gg/D9HXppgk21w78GyQ==", + "version": "1.14.2", + "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.14.2.tgz", + "integrity": "sha512-DgoQmbpFNOofkjJtKwr87Ma5EW4Dc8fWhD0R+ndq7Oc456ivUfGOOP6oAZTTKl5/CcNMP+EN+e3/iUzgE0veZg==", "dev": true, "requires": { "es-to-primitive": "^1.2.0", @@ -2102,8 +2048,8 @@ "is-regex": "^1.0.4", "object-inspect": "^1.6.0", "object-keys": "^1.1.1", - "string.prototype.trimleft": "^2.1.0", - "string.prototype.trimright": "^2.1.0" + "string.prototype.trimleft": "^2.0.0", + "string.prototype.trimright": "^2.0.0" } }, "es-to-primitive": { @@ -2404,15 +2350,16 @@ "dev": true }, "fast-glob": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.1.0.tgz", - "integrity": "sha512-TrUz3THiq2Vy3bjfQUB2wNyPdGBeGmdjbzzBLhfHN4YFurYptCKwGq/TfiRavbGywFRzY6U2CdmQ1zmsY5yYaw==", + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.0.4.tgz", + "integrity": "sha512-wkIbV6qg37xTJwqSsdnIphL1e+LaGz4AIQqr00mIubMaEhv1/HEmJ0uuCGZRNRUkZZmOB5mJKO0ZUTVq+SxMQg==", "dev": true, "requires": { - "@nodelib/fs.stat": "^2.0.2", - "@nodelib/fs.walk": "^1.2.3", - "glob-parent": "^5.1.0", - "merge2": "^1.3.0", + "@nodelib/fs.stat": "^2.0.1", + "@nodelib/fs.walk": "^1.2.1", + "glob-parent": "^5.0.0", + "is-glob": "^4.0.1", + "merge2": "^1.2.3", "micromatch": "^4.0.2" } }, @@ -3385,9 +3332,9 @@ "dev": true }, "indent-string": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/indent-string/-/indent-string-4.0.0.tgz", - "integrity": "sha512-EdDDZu4A2OyIK7Lr/2zG+w5jmbuk1DVBnEwREQvBzspBJkCEbRa8GxU1lghYcaGJCnRWibjDXlq779X1/y5xwg==", + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/indent-string/-/indent-string-3.2.0.tgz", + "integrity": "sha1-Sl/W0nzDMvN+VBmlBNu4NxBckok=", "dev": true }, "inflight": { @@ -3574,9 +3521,9 @@ "dev": true }, "is-path-inside": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/is-path-inside/-/is-path-inside-3.0.2.tgz", - "integrity": "sha512-/2UGPSgmtqwo1ktx8NDHjuPwZWmHhO+gj0f93EkhLB5RgW9RZevWYYlIkS6zePc6U2WpOdQYIwHe9YC4DWEBVg==", + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/is-path-inside/-/is-path-inside-3.0.1.tgz", + "integrity": "sha512-CKstxrctq1kUesU6WhtZDbYKzzYBuRH0UYInAVrkc/EYdB9ltbfE0gOoayG9nhohG6447sOOVGhHqsdmBvkbNg==", "dev": true }, "is-plain-object": { @@ -4609,9 +4556,9 @@ "dev": true }, "json5": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/json5/-/json5-2.1.1.tgz", - "integrity": "sha512-l+3HXD0GEI3huGq1njuqtzYK8OYJyXMkOLtQ53pjWh89tvWS2h6l+1zMkYWqlb57+SiQodKZyvMEFb2X+KrFhQ==", + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.1.0.tgz", + "integrity": "sha512-8Mh9h6xViijj36g7Dxi+Y4S6hNGV96vcJZr/SrlHh1LR/pEn/8j/+qIBbs44YKl69Lrfctp4QD+AdWLTMqEZAQ==", "dev": true, "requires": { "minimist": "^1.2.0" @@ -4679,23 +4626,24 @@ } }, "livereload": { - "version": "0.8.2", - "resolved": "https://registry.npmjs.org/livereload/-/livereload-0.8.2.tgz", - "integrity": "sha512-8wCvhiCL4cGVoT3U5xoe+UjpiiVZLrlOvr6dbhb1VlyC5QarhrlyRRt4z7EMGO4KSgXj+tKF/dr284F28/wI+g==", + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/livereload/-/livereload-0.8.0.tgz", + "integrity": "sha512-Hi5Na6VIK3e8zlgOS50fu+iOTKWj5hM0BE7NKpZkwnfWTnktTjA38ZUXa2NlJww8/GrdVhpnxdqlLad5fkO27g==", "dev": true, "requires": { "chokidar": "^2.1.5", "opts": ">= 1.2.0", - "ws": "^6.2.1" + "ws": "^1.1.5" }, "dependencies": { "ws": { - "version": "6.2.1", - "resolved": "https://registry.npmjs.org/ws/-/ws-6.2.1.tgz", - "integrity": "sha512-GIyAXC2cB7LjvpgMt9EKS2ldqr0MTrORaleiOno6TweZ6r3TKtoFQWay/2PceJ3RuBasOHzXNn5Lrw1X0bEjqA==", + "version": "1.1.5", + "resolved": "https://registry.npmjs.org/ws/-/ws-1.1.5.tgz", + "integrity": "sha512-o3KqipXNUdS7wpQzBHSe180lBGO60SoK0yVo3CYJgb2MkobuWuBX6dhkYP5ORCLd55y+SaflMOV5fqAB53ux4w==", "dev": true, "requires": { - "async-limiter": "~1.0.0" + "options": ">=0.0.5", + "ultron": "1.0.x" } } } @@ -4780,9 +4728,9 @@ } }, "magic-string": { - "version": "0.25.4", - "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.4.tgz", - "integrity": "sha512-oycWO9nEVAP2RVPbIoDoA4Y7LFIJ3xRYov93gAyJhZkET1tNuB0u7uWkZS2LpBWTJUWnmau/To8ECWRC+jKNfw==", + "version": "0.25.3", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.3.tgz", + "integrity": "sha512-6QK0OpF/phMz0Q2AxILkX2mFhi7m+WMwTRg0LQKq/WBB0cDP4rYH3Wp4/d3OTXlrPLVJT/RFqj8tFeAR4nk8AA==", "dev": true, "requires": { "sourcemap-codec": "^1.4.4" @@ -5263,6 +5211,12 @@ } } }, + "options": { + "version": "0.0.6", + "resolved": "https://registry.npmjs.org/options/-/options-0.0.6.tgz", + "integrity": "sha1-7CLTEoBrtT5zF3Pnza788cZDEo8=", + "dev": true + }, "opts": { "version": "1.2.6", "resolved": "https://registry.npmjs.org/opts/-/opts-1.2.6.tgz", @@ -5560,9 +5514,9 @@ "dev": true }, "react-is": { - "version": "16.10.2", - "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.10.2.tgz", - "integrity": "sha512-INBT1QEgtcCCgvccr5/86CfD71fw9EPmDxgiJX4I2Ddr6ZsV6iFXsuby+qWJPtmNuMY0zByTsG4468P7nHuNWA==", + "version": "16.10.0", + "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.10.0.tgz", + "integrity": "sha512-WRki2sBb7MTpYp7FtDEmSeGKX2vamYyq3rc9o7fKUG+/DHVyJu69NnvJsiSwwhh2Tt8XN40MQHkDBEXwyfxncQ==", "dev": true }, "read-pkg": { @@ -5914,14 +5868,14 @@ } }, "rollup": { - "version": "1.24.0", - "resolved": "https://registry.npmjs.org/rollup/-/rollup-1.24.0.tgz", - "integrity": "sha512-PiFETY/rPwodQ8TTC52Nz2DSCYUATznGh/ChnxActCr8rV5FIk3afBUb3uxNritQW/Jpbdn3kq1Rwh1HHYMwdQ==", + "version": "1.21.4", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-1.21.4.tgz", + "integrity": "sha512-Pl512XVCmVzgcBz5h/3Li4oTaoDcmpuFZ+kdhS/wLreALz//WuDAMfomD3QEYl84NkDu6Z6wV9twlcREb4qQsw==", "dev": true, "requires": { - "@types/estree": "*", - "@types/node": "*", - "acorn": "^7.1.0" + "@types/estree": "0.0.39", + "@types/node": "^12.7.5", + "acorn": "^7.0.0" }, "dependencies": { "acorn": { @@ -5956,12 +5910,12 @@ } }, "rollup-plugin-executable": { - "version": "1.5.1", - "resolved": "https://registry.npmjs.org/rollup-plugin-executable/-/rollup-plugin-executable-1.5.1.tgz", - "integrity": "sha512-+UAapfjBIJP24/x2qqxQTCNOZq2fRdLOpeXlIaGwOYTvjafBkc35LQrApr3Lg+2izJTUJsQdiW38vaoArS0n+A==", + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/rollup-plugin-executable/-/rollup-plugin-executable-1.5.0.tgz", + "integrity": "sha512-jbzScgip5whAWSLWY/OlgBxM6fiwQt7ix6UpsAJJ3gA+LLIOE6qWqVxnlLNMDgn84m1H2z25fqQRTl9yl9kAbQ==", "dev": true, "requires": { - "@babel/runtime": "^7.6.2" + "@babel/runtime": "^7.4.5" } }, "rollup-plugin-filesize": { @@ -5989,12 +5943,12 @@ } }, "rollup-plugin-livereload": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/rollup-plugin-livereload/-/rollup-plugin-livereload-1.0.4.tgz", - "integrity": "sha512-nbnSP8Mj2mmLZkrf080z3PrdacmpAW6UkmgM+BWClcJ8MSsruPONGTwirhZaNNHjUYvkJ+iF5/pSk4g0KV2uVQ==", + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/rollup-plugin-livereload/-/rollup-plugin-livereload-1.0.3.tgz", + "integrity": "sha512-K626KL5JAF3PGHdcVFHaR+d1w3c4BOzdcJkAnXHCKbL41byLEux/bjaqy1/uNVrpXlxeRPJu4fJfMTl5LHbEpA==", "dev": true, "requires": { - "livereload": "0.8.0 || ^0.8.2" + "livereload": "0.8.0" } }, "rollup-plugin-node-globals": { @@ -6915,9 +6869,9 @@ "dev": true }, "terser": { - "version": "4.3.9", - "resolved": "https://registry.npmjs.org/terser/-/terser-4.3.9.tgz", - "integrity": "sha512-NFGMpHjlzmyOtPL+fDw3G7+6Ueh/sz4mkaUYa4lJCxOPTNzd0Uj0aZJOmsDYoSQyfuVoWDMSWTPU3huyOm2zdA==", + "version": "4.3.3", + "resolved": "https://registry.npmjs.org/terser/-/terser-4.3.3.tgz", + "integrity": "sha512-Nzr7dpRjSzMEUS+z2UYQBtzE0LDm5k0Yy8RgLRPy85QUo1TjU5lIOBwzS5/FVAMaVyHZ3WTTU2BuQcMn8KXnNQ==", "dev": true, "requires": { "commander": "^2.20.0", @@ -7112,63 +7066,40 @@ "dev": true }, "tslib-cli": { - "version": "5.2.3", - "resolved": "https://registry.npmjs.org/tslib-cli/-/tslib-cli-5.2.3.tgz", - "integrity": "sha512-jsRI8l5yvfeK9uqW33hVvcwp2RqQ6ArScl5cWZonaAKD25roEkyLzLb2Yy9DLgJSSatSWqo/7chYb+m3YY9Fzg==", - "dev": true, - "requires": { - "builtin-modules": "3.1.0", - "chalk": "2.4.2", - "command-exists": "1.2.8", - "coveralls": "3.0.7", - "deepmerge": "4.1.1", - "del": "5.1.0", - "is-ci": "2.0.0", - "jest": "24.9.0", + "version": "5.0.18", + "resolved": "https://registry.npmjs.org/tslib-cli/-/tslib-cli-5.0.18.tgz", + "integrity": "sha512-9gYtN0M5vxLddWDHUj9hIriXhnAC81uY4BXHXCnYdxtB54fFOdmgDBalOl/QxoAHkkHv+ZqntjLtvvNS7XKSaQ==", + "dev": true, + "requires": { + "builtin-modules": "^3.1.0", + "chalk": "^2.4.2", + "coveralls": "^3.0.6", + "deepmerge": "^4.0.0", + "del": "^5.1.0", + "is-ci": "^2.0.0", + "jest": "^24.9.0", "ncp": "2.0.0", - "prettier": "1.18.2", - "readline-sync": "1.4.10", + "prettier": "^1.18.2", + "readline-sync": "^1.4.10", "replacestream": "4.0.3", - "rollup": "1.24.0", - "rollup-plugin-add-shebang": "0.3.1", - "rollup-plugin-commonjs": "10.1.0", - "rollup-plugin-executable": "1.5.1", - "rollup-plugin-filesize": "6.2.0", - "rollup-plugin-json": "4.0.0", - "rollup-plugin-livereload": "1.0.4", - "rollup-plugin-node-globals": "1.4.0", - "rollup-plugin-node-resolve": "5.2.0", - "rollup-plugin-replace": "2.2.0", - "rollup-plugin-serve": "1.0.1", - "rollup-plugin-terser": "5.1.2", - "rollup-plugin-typescript2": "0.24.3", - "ts-jest": "24.1.0", - "tslib": "1.10.0", - "tslint": "5.20.0", - "tslint-config-prettier": "1.18.0", - "typescript": "3.6.4" - }, - "dependencies": { - "coveralls": { - "version": "3.0.7", - "resolved": "https://registry.npmjs.org/coveralls/-/coveralls-3.0.7.tgz", - "integrity": "sha512-mUuH2MFOYB2oBaA4D4Ykqi9LaEYpMMlsiOMJOrv358yAjP6enPIk55fod2fNJ8AvwoYXStWQls37rA+s5e7boA==", - "dev": true, - "requires": { - "growl": "~> 1.10.0", - "js-yaml": "^3.13.1", - "lcov-parse": "^0.0.10", - "log-driver": "^1.2.7", - "minimist": "^1.2.0", - "request": "^2.86.0" - } - }, - "typescript": { - "version": "3.6.4", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.6.4.tgz", - "integrity": "sha512-unoCll1+l+YK4i4F8f22TaNVPRHcD9PA3yCuZ8g5e0qGqlVlJ/8FSateOLLSagn+Yg5+ZwuPkL8LFUc0Jcvksg==", - "dev": true - } + "rollup": "^1.21.4", + "rollup-plugin-add-shebang": "^0.3.1", + "rollup-plugin-commonjs": "^10.1.0", + "rollup-plugin-executable": "^1.5.0", + "rollup-plugin-filesize": "^6.2.0", + "rollup-plugin-json": "^4.0.0", + "rollup-plugin-livereload": "^1.0.3", + "rollup-plugin-node-globals": "^1.4.0", + "rollup-plugin-node-resolve": "^5.2.0", + "rollup-plugin-replace": "^2.2.0", + "rollup-plugin-serve": "^1.0.1", + "rollup-plugin-terser": "^5.1.2", + "rollup-plugin-typescript2": "^0.24.3", + "ts-jest": "^24.1.0", + "tslib": "^1.10.0", + "tslint": "^5.20.0", + "tslint-config-prettier": "^1.18.0", + "typescript": "^3.6.3" } }, "tslint": { @@ -7277,6 +7208,12 @@ } } }, + "ultron": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/ultron/-/ultron-1.0.2.tgz", + "integrity": "sha1-rOEWq1V80Zc4ak6I9GhTeMiy5Po=", + "dev": true + }, "unicode-length": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/unicode-length/-/unicode-length-1.0.3.tgz", diff --git a/package.json b/package.json index bb6e887..ba49842 100644 --- a/package.json +++ b/package.json @@ -16,11 +16,11 @@ "*.d.ts" ], "license": "BSD-3-Clause", - "source": "src/lru.ts", + "source": "src/lru.js", "browser": "lib/tiny-lru.js", "main": "lib/tiny-lru.cjs.js", "module": "lib/tiny-lru.esm.js", - "types": "lib/tiny-lru.d.ts", + "types": "tiny-lru.d.ts", "engines": { "node": ">=6" }, @@ -33,7 +33,7 @@ }, "dependencies": {}, "devDependencies": { - "tslib-cli": "^5.2.3", + "tslib-cli": "^5.0.18", "nodeunit": "^0.11.3", "precise": "^1.1.0" }, diff --git a/src/lru.ts b/src/lru.js similarity index 90% rename from src/lru.ts rename to src/lru.js index 5616f39..62d8367 100644 --- a/src/lru.ts +++ b/src/lru.js @@ -1,12 +1,4 @@ - class LRU { - - private first; - private items; - private last; - private max; - private size; - private ttl; - + class LRU { constructor (max = 0, ttl = 0) { this.first = null; this.items = {}; @@ -16,7 +8,7 @@ this.ttl = ttl; } - has (key: string) { + has (key) { return key in this.items; } @@ -29,7 +21,7 @@ return this; } - delete (key: string) { + delete (key) { if (this.has(key)) { const item = this.items[key]; @@ -67,7 +59,7 @@ return this; } - get (key: string): T { + get (key) { let result; if (this.has(key)) { @@ -88,7 +80,7 @@ return Object.keys(this.items); } - set (key: string, value: T, bypass = false) { + set (key, value, bypass = false) { let item; if (bypass || this.has(key)) { diff --git a/tiny-lru.d.ts b/tiny-lru.d.ts new file mode 100644 index 0000000..f4b2f34 --- /dev/null +++ b/tiny-lru.d.ts @@ -0,0 +1,14 @@ +declare module "tiny-lru" { + export class Lru { + constructor(max?: number, ttl?: number); + + public has(key: string): boolean; + public get(key: string): T; + public set(key: string, value: T, bypass?: boolean): this; + public clear(): this; + public delete(key: string): this; + public evict(): this; + public keys(): string[]; + } + export default Lru; +} diff --git a/tsconfig.json b/tsconfig.json index 2b5a596..f38ddc4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,14 +1,13 @@ { "exclude": ["node_modules", "lib"], "compilerOptions": { - "declaration": true, - "declarationDir": "lib", "lib": ["dom", "esnext"], "module": "esnext", "moduleResolution": "node", "outDir": "lib", "strict": false, "target": "esnext", + "allowJs": true, "sourceMap": true } } \ No newline at end of file