diff --git a/lib/lru.d.ts b/lib/lru.d.ts new file mode 100644 index 0000000..204d0ee --- /dev/null +++ b/lib/lru.d.ts @@ -0,0 +1,18 @@ +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 712da54..50be73a 100644 --- a/lib/tiny-lru.cjs.js +++ b/lib/tiny-lru.cjs.js @@ -1,151 +1,120 @@ '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 c14a83f..b37987e 100644 --- a/lib/tiny-lru.esm.js +++ b/lib/tiny-lru.esm.js @@ -1,149 +1,118 @@ -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 df4fee9..bbd48cd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,18 +14,18 @@ } }, "@babel/core": { - "version": "7.6.2", - "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.6.2.tgz", - "integrity": "sha512-l8zto/fuoZIbncm+01p8zPSDZu/VuuJhAfA7d/AbzM09WR7iVhavvfNDYCNpo1VvLk6E6xgAoP9P+/EMJHuRkQ==", + "version": "7.6.4", + "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.6.4.tgz", + "integrity": "sha512-Rm0HGw101GY8FTzpWSyRbki/jzq+/PkNQJ+nSulrdY6gFGOsNseCqD6KHRYe2E+EdzuBdr2pxCp6s4Uk6eJ+XQ==", "dev": true, "requires": { "@babel/code-frame": "^7.5.5", - "@babel/generator": "^7.6.2", + "@babel/generator": "^7.6.4", "@babel/helpers": "^7.6.2", - "@babel/parser": "^7.6.2", + "@babel/parser": "^7.6.4", "@babel/template": "^7.6.0", - "@babel/traverse": "^7.6.2", - "@babel/types": "^7.6.0", + "@babel/traverse": "^7.6.3", + "@babel/types": "^7.6.3", "convert-source-map": "^1.1.0", "debug": "^4.1.0", "json5": "^2.1.0", @@ -33,6 +33,54 @@ "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": { @@ -120,9 +168,9 @@ } }, "@babel/runtime": { - "version": "7.6.2", - "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.6.2.tgz", - "integrity": "sha512-EXxN64agfUqqIGeEjI5dL5z0Sw0ZwWo1mLTi4mQowCZ42O59b7DRpZAnTC6OqdF28wMBMFKNb/4uFGrVaigSpg==", + "version": "7.6.3", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.6.3.tgz", + "integrity": "sha512-kq6anf9JGjW8Nt5rYfEuGRaEAaH1mkv3Bbu6rYvLOpPh/RusSJXuKPEAoZ7L7gybZkchE8+NV5g9vKF4AGAtsA==", "dev": true, "requires": { "regenerator-runtime": "^0.13.2" @@ -615,28 +663,28 @@ } }, "@nodelib/fs.scandir": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.2.tgz", - "integrity": "sha512-wrIBsjA5pl13f0RN4Zx4FNWmU71lv03meGKnqRUoCyan17s4V3WL92f3w3AIuWbNnpcrQyFBU5qMavJoB8d27w==", + "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==", "dev": true, "requires": { - "@nodelib/fs.stat": "2.0.2", + "@nodelib/fs.stat": "2.0.3", "run-parallel": "^1.1.9" } }, "@nodelib/fs.stat": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/@nodelib/fs.stat/-/fs.stat-2.0.2.tgz", - "integrity": "sha512-z8+wGWV2dgUhLqrtRYa03yDx4HWMvXKi1z8g3m2JyxAx8F7xk74asqPk5LAETjqDSGLFML/6CDl0+yFunSYicw==", + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/@nodelib/fs.stat/-/fs.stat-2.0.3.tgz", + "integrity": "sha512-bQBFruR2TAwoevBEd/NWMoAAtNGzTRgdrqnYCc7dhzfoNvqPzLyqlEQnzZ3kVnNrSp25iyxE00/3h2fqGAGArA==", "dev": true }, "@nodelib/fs.walk": { - "version": "1.2.3", - "resolved": "https://registry.npmjs.org/@nodelib/fs.walk/-/fs.walk-1.2.3.tgz", - "integrity": "sha512-l6t8xEhfK9Sa4YO5mIRdau7XSOADfmh3jCr0evNHdY+HNkW6xuQhgMH7D73VV6WpZOagrW0UludvMTiifiwTfA==", + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@nodelib/fs.walk/-/fs.walk-1.2.4.tgz", + "integrity": "sha512-1V9XOY4rDW0rehzbrcqAmHnz8e7SKvX27gh8Gt2WgB0+pdzdiLV83p72kZPU+jvMbS1qU5mauP2iOvO8rhmurQ==", "dev": true, "requires": { - "@nodelib/fs.scandir": "2.1.2", + "@nodelib/fs.scandir": "2.1.3", "fastq": "^1.6.0" } }, @@ -736,9 +784,9 @@ "dev": true }, "@types/node": { - "version": "12.7.8", - "resolved": "https://registry.npmjs.org/@types/node/-/node-12.7.8.tgz", - "integrity": "sha512-FMdVn84tJJdV+xe+53sYiZS4R5yn1mAIxfj+DVoNiQjTYz1+OYmjwEZr1ev9nU0axXwda0QDbYl06QHanRVH3A==", + "version": "12.7.12", + "resolved": "https://registry.npmjs.org/@types/node/-/node-12.7.12.tgz", + "integrity": "sha512-KPYGmfD0/b1eXurQ59fXD1GBzhSQfz6/lKBxkaHX9dKTzjXbK68Zt7yGUxUsCS1jeTy/8aL+d9JEr+S54mpkWQ==", "dev": true }, "@types/resolve": { @@ -808,13 +856,13 @@ "dev": true }, "aggregate-error": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/aggregate-error/-/aggregate-error-3.0.0.tgz", - "integrity": "sha512-yKD9kEoJIR+2IFqhMwayIBgheLYbB3PS2OBhWae1L/ODTd/JF/30cW0bc9TqzRL3k4U41Dieu3BF4I29p8xesA==", + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/aggregate-error/-/aggregate-error-3.0.1.tgz", + "integrity": "sha512-quoaXsZ9/BLNae5yiNoUz+Nhkwz83GhWwtYFglcjEQB2NDHCIpApbqXxIFnm4Pq/Nvhrsq5sYJFyohrrxnTGAA==", "dev": true, "requires": { "clean-stack": "^2.0.0", - "indent-string": "^3.2.0" + "indent-string": "^4.0.0" } }, "ajv": { @@ -1666,6 +1714,12 @@ "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", @@ -1841,9 +1895,9 @@ "dev": true }, "deepmerge": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.0.0.tgz", - "integrity": "sha512-YZ1rOP5+kHor4hMAH+HRQnBQHg+wvS1un1hAOuIcxcBy0hzcUf6Jg2a1w65kpoOUnurOfZbERwjI1TfZxNjcww==", + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.1.1.tgz", + "integrity": "sha512-+qO5WbNBKBaZez95TffdUDnGIo4+r5kmsX8aOb7PDHvXsTbghAmleuxjs6ytNaf5Eg4FGBXDS5vqO61TRi6BMg==", "dev": true }, "default-require-extensions": { @@ -2035,9 +2089,9 @@ } }, "es-abstract": { - "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==", + "version": "1.15.0", + "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.15.0.tgz", + "integrity": "sha512-bhkEqWJ2t2lMeaJDuk7okMkJWI/yqgH/EoGwpcvv0XW9RWQsRspI4wt6xuyuvMvvQE3gg/D9HXppgk21w78GyQ==", "dev": true, "requires": { "es-to-primitive": "^1.2.0", @@ -2048,8 +2102,8 @@ "is-regex": "^1.0.4", "object-inspect": "^1.6.0", "object-keys": "^1.1.1", - "string.prototype.trimleft": "^2.0.0", - "string.prototype.trimright": "^2.0.0" + "string.prototype.trimleft": "^2.1.0", + "string.prototype.trimright": "^2.1.0" } }, "es-to-primitive": { @@ -2350,16 +2404,15 @@ "dev": true }, "fast-glob": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.0.4.tgz", - "integrity": "sha512-wkIbV6qg37xTJwqSsdnIphL1e+LaGz4AIQqr00mIubMaEhv1/HEmJ0uuCGZRNRUkZZmOB5mJKO0ZUTVq+SxMQg==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.1.0.tgz", + "integrity": "sha512-TrUz3THiq2Vy3bjfQUB2wNyPdGBeGmdjbzzBLhfHN4YFurYptCKwGq/TfiRavbGywFRzY6U2CdmQ1zmsY5yYaw==", "dev": true, "requires": { - "@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", + "@nodelib/fs.stat": "^2.0.2", + "@nodelib/fs.walk": "^1.2.3", + "glob-parent": "^5.1.0", + "merge2": "^1.3.0", "micromatch": "^4.0.2" } }, @@ -3332,9 +3385,9 @@ "dev": true }, "indent-string": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/indent-string/-/indent-string-3.2.0.tgz", - "integrity": "sha1-Sl/W0nzDMvN+VBmlBNu4NxBckok=", + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/indent-string/-/indent-string-4.0.0.tgz", + "integrity": "sha512-EdDDZu4A2OyIK7Lr/2zG+w5jmbuk1DVBnEwREQvBzspBJkCEbRa8GxU1lghYcaGJCnRWibjDXlq779X1/y5xwg==", "dev": true }, "inflight": { @@ -3521,9 +3574,9 @@ "dev": true }, "is-path-inside": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/is-path-inside/-/is-path-inside-3.0.1.tgz", - "integrity": "sha512-CKstxrctq1kUesU6WhtZDbYKzzYBuRH0UYInAVrkc/EYdB9ltbfE0gOoayG9nhohG6447sOOVGhHqsdmBvkbNg==", + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/is-path-inside/-/is-path-inside-3.0.2.tgz", + "integrity": "sha512-/2UGPSgmtqwo1ktx8NDHjuPwZWmHhO+gj0f93EkhLB5RgW9RZevWYYlIkS6zePc6U2WpOdQYIwHe9YC4DWEBVg==", "dev": true }, "is-plain-object": { @@ -4556,9 +4609,9 @@ "dev": true }, "json5": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/json5/-/json5-2.1.0.tgz", - "integrity": "sha512-8Mh9h6xViijj36g7Dxi+Y4S6hNGV96vcJZr/SrlHh1LR/pEn/8j/+qIBbs44YKl69Lrfctp4QD+AdWLTMqEZAQ==", + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.1.1.tgz", + "integrity": "sha512-l+3HXD0GEI3huGq1njuqtzYK8OYJyXMkOLtQ53pjWh89tvWS2h6l+1zMkYWqlb57+SiQodKZyvMEFb2X+KrFhQ==", "dev": true, "requires": { "minimist": "^1.2.0" @@ -4626,24 +4679,23 @@ } }, "livereload": { - "version": "0.8.0", - "resolved": "https://registry.npmjs.org/livereload/-/livereload-0.8.0.tgz", - "integrity": "sha512-Hi5Na6VIK3e8zlgOS50fu+iOTKWj5hM0BE7NKpZkwnfWTnktTjA38ZUXa2NlJww8/GrdVhpnxdqlLad5fkO27g==", + "version": "0.8.2", + "resolved": "https://registry.npmjs.org/livereload/-/livereload-0.8.2.tgz", + "integrity": "sha512-8wCvhiCL4cGVoT3U5xoe+UjpiiVZLrlOvr6dbhb1VlyC5QarhrlyRRt4z7EMGO4KSgXj+tKF/dr284F28/wI+g==", "dev": true, "requires": { "chokidar": "^2.1.5", "opts": ">= 1.2.0", - "ws": "^1.1.5" + "ws": "^6.2.1" }, "dependencies": { "ws": { - "version": "1.1.5", - "resolved": "https://registry.npmjs.org/ws/-/ws-1.1.5.tgz", - "integrity": "sha512-o3KqipXNUdS7wpQzBHSe180lBGO60SoK0yVo3CYJgb2MkobuWuBX6dhkYP5ORCLd55y+SaflMOV5fqAB53ux4w==", + "version": "6.2.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-6.2.1.tgz", + "integrity": "sha512-GIyAXC2cB7LjvpgMt9EKS2ldqr0MTrORaleiOno6TweZ6r3TKtoFQWay/2PceJ3RuBasOHzXNn5Lrw1X0bEjqA==", "dev": true, "requires": { - "options": ">=0.0.5", - "ultron": "1.0.x" + "async-limiter": "~1.0.0" } } } @@ -4728,9 +4780,9 @@ } }, "magic-string": { - "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==", + "version": "0.25.4", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.4.tgz", + "integrity": "sha512-oycWO9nEVAP2RVPbIoDoA4Y7LFIJ3xRYov93gAyJhZkET1tNuB0u7uWkZS2LpBWTJUWnmau/To8ECWRC+jKNfw==", "dev": true, "requires": { "sourcemap-codec": "^1.4.4" @@ -5211,12 +5263,6 @@ } } }, - "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", @@ -5514,9 +5560,9 @@ "dev": true }, "react-is": { - "version": "16.10.0", - "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.10.0.tgz", - "integrity": "sha512-WRki2sBb7MTpYp7FtDEmSeGKX2vamYyq3rc9o7fKUG+/DHVyJu69NnvJsiSwwhh2Tt8XN40MQHkDBEXwyfxncQ==", + "version": "16.10.2", + "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.10.2.tgz", + "integrity": "sha512-INBT1QEgtcCCgvccr5/86CfD71fw9EPmDxgiJX4I2Ddr6ZsV6iFXsuby+qWJPtmNuMY0zByTsG4468P7nHuNWA==", "dev": true }, "read-pkg": { @@ -5868,14 +5914,14 @@ } }, "rollup": { - "version": "1.21.4", - "resolved": "https://registry.npmjs.org/rollup/-/rollup-1.21.4.tgz", - "integrity": "sha512-Pl512XVCmVzgcBz5h/3Li4oTaoDcmpuFZ+kdhS/wLreALz//WuDAMfomD3QEYl84NkDu6Z6wV9twlcREb4qQsw==", + "version": "1.23.1", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-1.23.1.tgz", + "integrity": "sha512-95C1GZQpr/NIA0kMUQmSjuMDQ45oZfPgDBcN0yZwBG7Kee//m7H68vgIyg+SPuyrTZ5PrXfyLK80OzXeKG5dAA==", "dev": true, "requires": { - "@types/estree": "0.0.39", - "@types/node": "^12.7.5", - "acorn": "^7.0.0" + "@types/estree": "*", + "@types/node": "*", + "acorn": "^7.1.0" }, "dependencies": { "acorn": { @@ -5910,12 +5956,12 @@ } }, "rollup-plugin-executable": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/rollup-plugin-executable/-/rollup-plugin-executable-1.5.0.tgz", - "integrity": "sha512-jbzScgip5whAWSLWY/OlgBxM6fiwQt7ix6UpsAJJ3gA+LLIOE6qWqVxnlLNMDgn84m1H2z25fqQRTl9yl9kAbQ==", + "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==", "dev": true, "requires": { - "@babel/runtime": "^7.4.5" + "@babel/runtime": "^7.6.2" } }, "rollup-plugin-filesize": { @@ -5943,12 +5989,12 @@ } }, "rollup-plugin-livereload": { - "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==", + "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==", "dev": true, "requires": { - "livereload": "0.8.0" + "livereload": "0.8.0 || ^0.8.2" } }, "rollup-plugin-node-globals": { @@ -6869,9 +6915,9 @@ "dev": true }, "terser": { - "version": "4.3.3", - "resolved": "https://registry.npmjs.org/terser/-/terser-4.3.3.tgz", - "integrity": "sha512-Nzr7dpRjSzMEUS+z2UYQBtzE0LDm5k0Yy8RgLRPy85QUo1TjU5lIOBwzS5/FVAMaVyHZ3WTTU2BuQcMn8KXnNQ==", + "version": "4.3.8", + "resolved": "https://registry.npmjs.org/terser/-/terser-4.3.8.tgz", + "integrity": "sha512-otmIRlRVmLChAWsnSFNO0Bfk6YySuBp6G9qrHiJwlLDd4mxe2ta4sjI7TzIR+W1nBMjilzrMcPOz9pSusgx3hQ==", "dev": true, "requires": { "commander": "^2.20.0", @@ -7066,40 +7112,63 @@ "dev": true }, "tslib-cli": { - "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", + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/tslib-cli/-/tslib-cli-5.2.2.tgz", + "integrity": "sha512-kflIqGBaRhgRTfZips4jOABuQCYDpPBw5B4L0NYGMU4aVTQ3xiLowxwnwzeV5zfsT4Alx+vL2+NKbsNWFXunOg==", + "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", "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.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" + "rollup": "1.23.1", + "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 + } } }, "tslint": { @@ -7208,12 +7277,6 @@ } } }, - "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 ba49842..0c081e9 100644 --- a/package.json +++ b/package.json @@ -16,11 +16,11 @@ "*.d.ts" ], "license": "BSD-3-Clause", - "source": "src/lru.js", + "source": "src/lru.ts", "browser": "lib/tiny-lru.js", "main": "lib/tiny-lru.cjs.js", "module": "lib/tiny-lru.esm.js", - "types": "tiny-lru.d.ts", + "types": "lib/tiny-lru.d.ts", "engines": { "node": ">=6" }, @@ -33,7 +33,7 @@ }, "dependencies": {}, "devDependencies": { - "tslib-cli": "^5.0.18", + "tslib-cli": "^5.2.2", "nodeunit": "^0.11.3", "precise": "^1.1.0" }, diff --git a/src/lru.js b/src/lru.ts similarity index 90% rename from src/lru.js rename to src/lru.ts index 62d8367..5616f39 100644 --- a/src/lru.js +++ b/src/lru.ts @@ -1,4 +1,12 @@ - class LRU { + class LRU { + + private first; + private items; + private last; + private max; + private size; + private ttl; + constructor (max = 0, ttl = 0) { this.first = null; this.items = {}; @@ -8,7 +16,7 @@ this.ttl = ttl; } - has (key) { + has (key: string) { return key in this.items; } @@ -21,7 +29,7 @@ return this; } - delete (key) { + delete (key: string) { if (this.has(key)) { const item = this.items[key]; @@ -59,7 +67,7 @@ return this; } - get (key) { + get (key: string): T { let result; if (this.has(key)) { @@ -80,7 +88,7 @@ return Object.keys(this.items); } - set (key, value, bypass = false) { + set (key: string, value: T, bypass = false) { let item; if (bypass || this.has(key)) { diff --git a/tiny-lru.d.ts b/tiny-lru.d.ts deleted file mode 100644 index f4b2f34..0000000 --- a/tiny-lru.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -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 f38ddc4..2b5a596 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,13 +1,14 @@ { "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