From 1a0c3b293713e137c850ed0657e79ccaa047fe74 Mon Sep 17 00:00:00 2001 From: Jason Mulligan Date: Wed, 5 Dec 2018 07:39:28 -0500 Subject: [PATCH] Undoing API change as it's breaking things --- README.md | 13 ------------- lib/tiny-lru.js | 25 ++++++++++++------------- lib/tiny-lru.min.js | 4 ++-- lib/tiny-lru.min.js.map | 2 +- package-lock.json | 2 +- package.json | 2 +- src/intro.js | 9 +++++++++ src/lru.js | 14 ++------------ 8 files changed, 28 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index b34ac9b..7a4d677 100644 --- a/README.md +++ b/README.md @@ -156,19 +156,6 @@ Removes item from cache const staleItem = cache.remove("myKey"); ``` -## reset -### Method - -Resets the cache to it's original state - - return {Object} LRU instance - -**Example** - -```javascript -cache.reset(); -``` - ## set ### Method diff --git a/lib/tiny-lru.js b/lib/tiny-lru.js index 9d4550c..8395f35 100644 --- a/lib/tiny-lru.js +++ b/lib/tiny-lru.js @@ -5,7 +5,7 @@ * @copyright 2018 * @license BSD-3-Clause * @link https://github.com/avoidwork/tiny-lru - * @version 3.0.7 + * @version 4.0.0 */ "use strict"; @@ -13,17 +13,25 @@ const next = typeof process !== "undefined" ? process.nextTick : arg => setTimeout(arg, 1), empty = null; + function reset () { + this.cache = {}; + this.first = empty; + this.last = empty; + this.length = 0; + + return this; + } + class LRU { constructor (max, notify, ttl) { this.max = max; this.notify = notify; this.ttl = ttl; - - return this.reset(); + reset.call(this); } clear (silent = false) { - this.reset(); + reset.call(this); if (silent === false && this.notify === true) { next(this.onchange("clear", this.dump())); @@ -118,15 +126,6 @@ return result; } - reset () { - this.cache = {}; - this.first = empty; - this.last = empty; - this.length = 0; - - return this; - } - set (key, value, silent = false, bypass = false) { if (bypass === true || this.has(key) === true) { const item = this.cache[key]; diff --git a/lib/tiny-lru.min.js b/lib/tiny-lru.min.js index 4654fa8..094b98b 100644 --- a/lib/tiny-lru.min.js +++ b/lib/tiny-lru.min.js @@ -1,6 +1,6 @@ /* 2018 Jason Mulligan - @version 3.0.7 + @version 4.0.0 */ -"use strict";(function(a){function b(a=1e3,b=!1,c=0){return new e(a,b,c)}const c="undefined"==typeof process?(a)=>setTimeout(a,1):process.nextTick,d=null;class e{constructor(a,b,c){return this.max=a,this.notify=b,this.ttl=c,this.reset()}clear(a=!1){return this.reset(),!1===a&&!0===this.notify&&c(this.onchange("clear",this.dump())),this}delete(a,b=!1){return this.remove(a,b)}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(){return this.remove(this.last,!0,!0),!0===this.notify&&c(this.onchange("evict",this.dump())),this}get(a,b=!1){let d;if(!0===this.has(a)){const e=this.cache[a];-1===e.expiry||e.expiry>Date.now()?(d=e.value,this.set(a,d,!0,!0),!1===b&&!0===this.notify&&c(this.onchange("get",this.dump()))):this.remove(a)}return d}has(a){return a in this.cache}onchange(){}remove(a,b=!1,e=!1){let f;return(!0===e||!0===this.has(a))&&(f=this.cache[a],delete this.cache[a],this.length--,f.previous!==d&&(this.cache[f.previous].next=f.next),f.next!==d&&(this.cache[f.next].previous=f.previous),this.first===a&&(this.first=f.previous),this.last===a&&(this.last=this.first),!1===b&&!0===this.notify&&c(this.onchange("remove",this.dump()))),f}reset(){return this.cache={},this.first=d,this.last=d,this.length=0,this}set(a,b,e=!1,f=!1){if(!0===f||!0===this.has(a)){const c=this.cache[a];c.value=b,c.next=d,this.first!==a&&(c.previous=this.first),this.last===a&&c.previous!==d&&(this.last=c.previous)}else this.length===this.max&&this.evict(),this.length++,this.cache[a]={expiry:0{this[a]=b[a]})}}"undefined"==typeof exports?"function"==typeof define&&void 0!==define.amd?define(()=>b):a.lru=b:module.exports=b})("undefined"==typeof window?global:window); +"use strict";(function(a){function b(){return this.cache={},this.first=e,this.last=e,this.length=0,this}function c(a=1e3,b=!1,c=0){return new f(a,b,c)}const d="undefined"==typeof process?(a)=>setTimeout(a,1):process.nextTick,e=null;class f{constructor(a,c,d){this.max=a,this.notify=c,this.ttl=d,b.call(this)}clear(a=!1){return b.call(this),!1===a&&!0===this.notify&&d(this.onchange("clear",this.dump())),this}delete(a,b=!1){return this.remove(a,b)}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(){return this.remove(this.last,!0,!0),!0===this.notify&&d(this.onchange("evict",this.dump())),this}get(a,b=!1){let c;if(!0===this.has(a)){const e=this.cache[a];-1===e.expiry||e.expiry>Date.now()?(c=e.value,this.set(a,c,!0,!0),!1===b&&!0===this.notify&&d(this.onchange("get",this.dump()))):this.remove(a)}return c}has(a){return a in this.cache}onchange(){}remove(a,b=!1,c=!1){let f;return(!0===c||!0===this.has(a))&&(f=this.cache[a],delete this.cache[a],this.length--,f.previous!==e&&(this.cache[f.previous].next=f.next),f.next!==e&&(this.cache[f.next].previous=f.previous),this.first===a&&(this.first=f.previous),this.last===a&&(this.last=this.first),!1===b&&!0===this.notify&&d(this.onchange("remove",this.dump()))),f}set(a,b,c=!1,f=!1){if(!0===f||!0===this.has(a)){const c=this.cache[a];c.value=b,c.next=e,this.first!==a&&(c.previous=this.first),this.last===a&&c.previous!==e&&(this.last=c.previous)}else this.length===this.max&&this.evict(),this.length++,this.cache[a]={expiry:0{this[a]=b[a]})}}"undefined"==typeof exports?"function"==typeof define&&void 0!==define.amd?define(()=>c):a.lru=c:module.exports=c})("undefined"==typeof window?global:window); //# sourceMappingURL=tiny-lru.min.js.map \ No newline at end of file diff --git a/lib/tiny-lru.min.js.map b/lib/tiny-lru.min.js.map index 21e8a6a..1c3e2be 100644 --- a/lib/tiny-lru.min.js.map +++ b/lib/tiny-lru.min.js.map @@ -1 +1 @@ -{"version":3,"sources":["tiny-lru.js"],"names":["process","setTimeout","nextTick","constructor","max","notify","ttl","reset","clear","onchange","dump","delete","remove","JSON","stringify","cache","first","last","length","evict","get","has","expiry","Date","now","value","set","previous","next","getTime","update","parse","Object","keys","forEach","exports","define","amd","lru","module","window","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OAAP,CAAyCC,MAAzC,CAAgCD,MA9LjC,C","file":"unknown","sourcesContent":["/**\r\n * Tiny LRU cache for Client or Server\r\n *\r\n * @author Jason Mulligan \r\n * @copyright 2018\r\n * @license BSD-3-Clause\r\n * @link https://github.com/avoidwork/tiny-lru\r\n * @version 3.0.7\r\n */\r\n\r\n\r\n(function (global) {\r\n\tconst next = typeof process !== \"undefined\" ? process.nextTick : arg => setTimeout(arg, 1),\r\n\t\tempty = null;\r\n\r\n\tclass LRU {\r\n\t\tconstructor (max, notify, ttl) {\r\n\t\t\tthis.max = max;\r\n\t\t\tthis.notify = notify;\r\n\t\t\tthis.ttl = ttl;\r\n\r\n\t\t\treturn this.reset();\r\n\t\t}\r\n\r\n\t\tclear (silent = false) {\r\n\t\t\tthis.reset();\r\n\r\n\t\t\tif (silent === false && this.notify === true) {\r\n\t\t\t\tnext(this.onchange(\"clear\", this.dump()));\r\n\t\t\t}\r\n\r\n\t\t\treturn this;\r\n\t\t}\r\n\r\n\t\tdelete (key, silent = false) {\r\n\t\t\treturn this.remove(key, silent);\r\n\t\t}\r\n\r\n\t\tdump () {\r\n\t\t\treturn JSON.stringify({\r\n\t\t\t\tcache: this.cache,\r\n\t\t\t\tfirst: this.first,\r\n\t\t\t\tlast: this.last,\r\n\t\t\t\tlength: this.length,\r\n\t\t\t\tmax: this.max,\r\n\t\t\t\tnotify: this.notify,\r\n\t\t\t\tttl: this.ttl\r\n\t\t\t});\r\n\t\t}\r\n\r\n\t\tevict () {\r\n\t\t\tthis.remove(this.last, true, true);\r\n\r\n\t\t\tif (this.notify === true) {\r\n\t\t\t\tnext(this.onchange(\"evict\", this.dump()));\r\n\t\t\t}\r\n\r\n\t\t\treturn this;\r\n\t\t}\r\n\r\n\t\tget (key, silent = false) {\r\n\t\t\tlet result;\r\n\r\n\t\t\tif (this.has(key) === true) {\r\n\t\t\t\tconst item = this.cache[key];\r\n\r\n\t\t\t\tif (item.expiry === -1 || item.expiry > Date.now()) {\r\n\t\t\t\t\tresult = item.value;\r\n\t\t\t\t\tthis.set(key, result, true, true);\r\n\r\n\t\t\t\t\tif (silent === false && this.notify === true) {\r\n\t\t\t\t\t\tnext(this.onchange(\"get\", this.dump()));\r\n\t\t\t\t\t}\r\n\t\t\t\t} else {\r\n\t\t\t\t\tthis.remove(key);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\treturn result;\r\n\t\t}\r\n\r\n\t\thas (key) {\r\n\t\t\treturn key in this.cache;\r\n\t\t}\r\n\r\n\t\tonchange () {}\r\n\r\n\t\tremove (key, silent = false, bypass = false) {\r\n\t\t\tlet result;\r\n\r\n\t\t\tif (bypass === true || this.has(key) === true) {\r\n\t\t\t\tresult = this.cache[key];\r\n\r\n\t\t\t\tdelete this.cache[key];\r\n\t\t\t\tthis.length--;\r\n\r\n\t\t\t\tif (result.previous !== empty) {\r\n\t\t\t\t\tthis.cache[result.previous].next = result.next;\r\n\t\t\t\t}\r\n\r\n\t\t\t\tif (result.next !== empty) {\r\n\t\t\t\t\tthis.cache[result.next].previous = result.previous;\r\n\t\t\t\t}\r\n\r\n\t\t\t\tif (this.first === key) {\r\n\t\t\t\t\tthis.first = result.previous;\r\n\t\t\t\t}\r\n\r\n\t\t\t\tif (this.last === key) {\r\n\t\t\t\t\tthis.last = this.first;\r\n\t\t\t\t}\r\n\r\n\t\t\t\tif (silent === false && this.notify === true) {\r\n\t\t\t\t\tnext(this.onchange(\"remove\", this.dump()));\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\treturn result;\r\n\t\t}\r\n\r\n\t\treset () {\r\n\t\t\tthis.cache = {};\r\n\t\t\tthis.first = empty;\r\n\t\t\tthis.last = empty;\r\n\t\t\tthis.length = 0;\r\n\r\n\t\t\treturn this;\r\n\t\t}\r\n\r\n\t\tset (key, value, silent = false, bypass = false) {\r\n\t\t\tif (bypass === true || this.has(key) === true) {\r\n\t\t\t\tconst item = this.cache[key];\r\n\r\n\t\t\t\titem.value = value;\r\n\t\t\t\titem.next = empty;\r\n\r\n\t\t\t\tif (this.first !== key) {\r\n\t\t\t\t\titem.previous = this.first;\r\n\t\t\t\t}\r\n\r\n\t\t\t\tif (this.last === key && item.previous !== empty) {\r\n\t\t\t\t\tthis.last = item.previous;\r\n\t\t\t\t}\r\n\t\t\t} else {\r\n\t\t\t\tif (this.length === this.max) {\r\n\t\t\t\t\tthis.evict();\r\n\t\t\t\t}\r\n\r\n\t\t\t\tthis.length++;\r\n\t\t\t\tthis.cache[key] = {\r\n\t\t\t\t\texpiry: this.ttl > 0 ? new Date().getTime() + this.ttl : -1,\r\n\t\t\t\t\tnext: empty,\r\n\t\t\t\t\tprevious: this.first,\r\n\t\t\t\t\tvalue: value\r\n\t\t\t\t};\r\n\r\n\t\t\t\tif (this.length === 1) {\r\n\t\t\t\t\tthis.last = key;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\tif (this.first !== empty && this.first !== key) {\r\n\t\t\t\tconst first = this.cache[this.first];\r\n\r\n\t\t\t\tfirst.next = key;\r\n\r\n\t\t\t\tif (first.previous === key) {\r\n\t\t\t\t\tfirst.previous = empty;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\tthis.first = key;\r\n\r\n\t\t\tif (silent === false && this.notify === true) {\r\n\t\t\t\tnext(this.onchange(\"set\", this.dump()));\r\n\t\t\t}\r\n\r\n\t\t\treturn this;\r\n\t\t}\r\n\r\n\t\tupdate (arg) {\r\n\t\t\tconst obj = JSON.parse(arg);\r\n\r\n\t\t\tObject.keys(obj).forEach(i => {\r\n\t\t\t\tthis[i] = obj[i];\r\n\t\t\t});\r\n\t\t}\r\n\t}\r\n\r\n\tfunction factory (max = 1000, notify = false, ttl = 0) {\r\n\t\treturn new LRU(max, notify, ttl);\r\n\t}\r\n\r\n\t// Node, AMD & window supported\r\n\tif (typeof exports !== \"undefined\") {\r\n\t\tmodule.exports = factory;\r\n\t} else if (typeof define === \"function\" && define.amd !== void 0) {\r\n\t\tdefine(() => factory);\r\n\t} else {\r\n\t\tglobal.lru = factory;\r\n\t}\r\n}(typeof window !== \"undefined\" ? window : global));\r\n"]} \ No newline at end of file +{"version":3,"sources":["tiny-lru.js"],"names":["cache","first","last","length","process","setTimeout","nextTick","constructor","max","notify","ttl","call","clear","onchange","dump","delete","remove","JSON","stringify","evict","get","has","expiry","Date","now","value","set","previous","next","getTime","update","parse","Object","keys","forEach","exports","define","amd","lru","module","window","global"],"mappings":"AAWC,YAAkB,CAIlB,YAAkB,CAMjB,MALA,MAAKA,KAAL,CAAa,EAKb,CAJA,KAAKC,KAAL,EAIA,CAHA,KAAKC,IAAL,EAGA,CAFA,KAAKC,MAAL,CAAc,CAEd,CAAO,IACP,CAsKD,WAAkB,EAAM,GAAxB,CAA8B,IAA9B,CAA8C,EAAM,CAApD,CAAuD,CACtD,MAAO,aACP,CAlLD,KAAM,GAA0B,WAAnB,QAAOC,QAAP,CAAoD,KAAOC,aAAgB,CAAhB,CAA3D,CAAiCD,QAAQE,QAAtD,CACC,EAAQ,IADT,CAYA,OAAU,CACTC,kBAA+B,CAC9B,KAAKC,GAAL,EAD8B,CAE9B,KAAKC,MAAL,EAF8B,CAG9B,KAAKC,GAAL,EAH8B,CAI9B,EAAMC,IAAN,CAAW,IAAX,CACA,CAEDC,MAAO,IAAP,CAAuB,CAOtB,MANA,GAAMD,IAAN,CAAW,IAAX,CAMA,CAJI,QAAoB,UAAKF,MAI7B,EAHC,EAAK,KAAKI,QAAL,CAAc,OAAd,CAAuB,KAAKC,IAAL,EAAvB,CAAL,CAGD,CAAO,IACP,CAEDC,SAAa,IAAb,CAA6B,CAC5B,MAAO,MAAKC,MAAL,KACP,CAEDF,MAAQ,CACP,MAAOG,MAAKC,SAAL,CAAe,CACrBlB,MAAO,KAAKA,KADS,CAErBC,MAAO,KAAKA,KAFS,CAGrBC,KAAM,KAAKA,IAHU,CAIrBC,OAAQ,KAAKA,MAJQ,CAKrBK,IAAK,KAAKA,GALW,CAMrBC,OAAQ,KAAKA,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KAAKc,KAAL,GAAZ,CAEAC,OAAOC,IAAP,IAAiBC,OAAjB,CAAyB,KAAK,CAC7B,QAAU,IACV,CAFD,CAGA,CAjKQ,CAyKa,WAAnB,QAAOC,QAtLO,CAwLW,UAAlB,QAAOC,OAAP,EAA+C,IAAK,EAApB,UAAOC,GAxLhC,CAyLjBD,OAAO,KAAP,CAzLiB,CA2LjB,EAAOE,GAAP,EA3LiB,CAuLjBC,OAAOJ,OAAP,EAMD,CA7LA,EA6LmB,WAAlB,QAAOK,OAAP,CAAyCC,MAAzC,CAAgCD,MA7LjC,C","file":"unknown","sourcesContent":["/**\r\n * Tiny LRU cache for Client or Server\r\n *\r\n * @author Jason Mulligan \r\n * @copyright 2018\r\n * @license BSD-3-Clause\r\n * @link https://github.com/avoidwork/tiny-lru\r\n * @version 4.0.0\r\n */\r\n\r\n\r\n(function (global) {\r\n\tconst next = typeof process !== \"undefined\" ? process.nextTick : arg => setTimeout(arg, 1),\r\n\t\tempty = null;\r\n\r\n\tfunction reset () {\r\n\t\tthis.cache = {};\r\n\t\tthis.first = empty;\r\n\t\tthis.last = empty;\r\n\t\tthis.length = 0;\r\n\r\n\t\treturn this;\r\n\t}\r\n\r\n\tclass LRU {\r\n\t\tconstructor (max, notify, ttl) {\r\n\t\t\tthis.max = max;\r\n\t\t\tthis.notify = notify;\r\n\t\t\tthis.ttl = ttl;\r\n\t\t\treset.call(this);\r\n\t\t}\r\n\r\n\t\tclear (silent = false) {\r\n\t\t\treset.call(this);\r\n\r\n\t\t\tif (silent === false && this.notify === true) {\r\n\t\t\t\tnext(this.onchange(\"clear\", this.dump()));\r\n\t\t\t}\r\n\r\n\t\t\treturn this;\r\n\t\t}\r\n\r\n\t\tdelete (key, silent = false) {\r\n\t\t\treturn this.remove(key, silent);\r\n\t\t}\r\n\r\n\t\tdump () {\r\n\t\t\treturn JSON.stringify({\r\n\t\t\t\tcache: this.cache,\r\n\t\t\t\tfirst: this.first,\r\n\t\t\t\tlast: this.last,\r\n\t\t\t\tlength: this.length,\r\n\t\t\t\tmax: this.max,\r\n\t\t\t\tnotify: this.notify,\r\n\t\t\t\tttl: this.ttl\r\n\t\t\t});\r\n\t\t}\r\n\r\n\t\tevict () {\r\n\t\t\tthis.remove(this.last, true, true);\r\n\r\n\t\t\tif (this.notify === true) {\r\n\t\t\t\tnext(this.onchange(\"evict\", this.dump()));\r\n\t\t\t}\r\n\r\n\t\t\treturn this;\r\n\t\t}\r\n\r\n\t\tget (key, silent = false) {\r\n\t\t\tlet result;\r\n\r\n\t\t\tif (this.has(key) === true) {\r\n\t\t\t\tconst item = this.cache[key];\r\n\r\n\t\t\t\tif (item.expiry === -1 || item.expiry > Date.now()) {\r\n\t\t\t\t\tresult = item.value;\r\n\t\t\t\t\tthis.set(key, result, true, true);\r\n\r\n\t\t\t\t\tif (silent === false && this.notify === true) {\r\n\t\t\t\t\t\tnext(this.onchange(\"get\", this.dump()));\r\n\t\t\t\t\t}\r\n\t\t\t\t} else {\r\n\t\t\t\t\tthis.remove(key);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\treturn result;\r\n\t\t}\r\n\r\n\t\thas (key) {\r\n\t\t\treturn key in this.cache;\r\n\t\t}\r\n\r\n\t\tonchange () {}\r\n\r\n\t\tremove (key, silent = false, bypass = false) {\r\n\t\t\tlet result;\r\n\r\n\t\t\tif (bypass === true || this.has(key) === true) {\r\n\t\t\t\tresult = this.cache[key];\r\n\r\n\t\t\t\tdelete this.cache[key];\r\n\t\t\t\tthis.length--;\r\n\r\n\t\t\t\tif (result.previous !== empty) {\r\n\t\t\t\t\tthis.cache[result.previous].next = result.next;\r\n\t\t\t\t}\r\n\r\n\t\t\t\tif (result.next !== empty) {\r\n\t\t\t\t\tthis.cache[result.next].previous = result.previous;\r\n\t\t\t\t}\r\n\r\n\t\t\t\tif (this.first === key) {\r\n\t\t\t\t\tthis.first = result.previous;\r\n\t\t\t\t}\r\n\r\n\t\t\t\tif (this.last === key) {\r\n\t\t\t\t\tthis.last = this.first;\r\n\t\t\t\t}\r\n\r\n\t\t\t\tif (silent === false && this.notify === true) {\r\n\t\t\t\t\tnext(this.onchange(\"remove\", this.dump()));\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\treturn result;\r\n\t\t}\r\n\r\n\t\tset (key, value, silent = false, bypass = false) {\r\n\t\t\tif (bypass === true || this.has(key) === true) {\r\n\t\t\t\tconst item = this.cache[key];\r\n\r\n\t\t\t\titem.value = value;\r\n\t\t\t\titem.next = empty;\r\n\r\n\t\t\t\tif (this.first !== key) {\r\n\t\t\t\t\titem.previous = this.first;\r\n\t\t\t\t}\r\n\r\n\t\t\t\tif (this.last === key && item.previous !== empty) {\r\n\t\t\t\t\tthis.last = item.previous;\r\n\t\t\t\t}\r\n\t\t\t} else {\r\n\t\t\t\tif (this.length === this.max) {\r\n\t\t\t\t\tthis.evict();\r\n\t\t\t\t}\r\n\r\n\t\t\t\tthis.length++;\r\n\t\t\t\tthis.cache[key] = {\r\n\t\t\t\t\texpiry: this.ttl > 0 ? new Date().getTime() + this.ttl : -1,\r\n\t\t\t\t\tnext: empty,\r\n\t\t\t\t\tprevious: this.first,\r\n\t\t\t\t\tvalue: value\r\n\t\t\t\t};\r\n\r\n\t\t\t\tif (this.length === 1) {\r\n\t\t\t\t\tthis.last = key;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\tif (this.first !== empty && this.first !== key) {\r\n\t\t\t\tconst first = this.cache[this.first];\r\n\r\n\t\t\t\tfirst.next = key;\r\n\r\n\t\t\t\tif (first.previous === key) {\r\n\t\t\t\t\tfirst.previous = empty;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\tthis.first = key;\r\n\r\n\t\t\tif (silent === false && this.notify === true) {\r\n\t\t\t\tnext(this.onchange(\"set\", this.dump()));\r\n\t\t\t}\r\n\r\n\t\t\treturn this;\r\n\t\t}\r\n\r\n\t\tupdate (arg) {\r\n\t\t\tconst obj = JSON.parse(arg);\r\n\r\n\t\t\tObject.keys(obj).forEach(i => {\r\n\t\t\t\tthis[i] = obj[i];\r\n\t\t\t});\r\n\t\t}\r\n\t}\r\n\r\n\tfunction factory (max = 1000, notify = false, ttl = 0) {\r\n\t\treturn new LRU(max, notify, ttl);\r\n\t}\r\n\r\n\t// Node, AMD & window supported\r\n\tif (typeof exports !== \"undefined\") {\r\n\t\tmodule.exports = factory;\r\n\t} else if (typeof define === \"function\" && define.amd !== void 0) {\r\n\t\tdefine(() => factory);\r\n\t} else {\r\n\t\tglobal.lru = factory;\r\n\t}\r\n}(typeof window !== \"undefined\" ? window : global));\r\n"]} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 3017960..12f6baa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "tiny-lru", - "version": "3.0.7", + "version": "4.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 728f3c7..5591ad6 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "tiny-lru", "description": "Tiny LRU cache for Client or Server", - "version": "3.0.7", + "version": "4.0.0", "homepage": "https://github.com/avoidwork/tiny-lru", "author": "Jason Mulligan ", "repository": { diff --git a/src/intro.js b/src/intro.js index f0872b2..8b917e1 100644 --- a/src/intro.js +++ b/src/intro.js @@ -3,3 +3,12 @@ (function (global) { const next = typeof process !== "undefined" ? process.nextTick : arg => setTimeout(arg, 1), empty = null; + + function reset () { + this.cache = {}; + this.first = empty; + this.last = empty; + this.length = 0; + + return this; + } diff --git a/src/lru.js b/src/lru.js index 255a519..1cb492d 100644 --- a/src/lru.js +++ b/src/lru.js @@ -3,12 +3,11 @@ this.max = max; this.notify = notify; this.ttl = ttl; - - return this.reset(); + reset.call(this); } clear (silent = false) { - this.reset(); + reset.call(this); if (silent === false && this.notify === true) { next(this.onchange("clear", this.dump())); @@ -103,15 +102,6 @@ return result; } - reset () { - this.cache = {}; - this.first = empty; - this.last = empty; - this.length = 0; - - return this; - } - set (key, value, silent = false, bypass = false) { if (bypass === true || this.has(key) === true) { const item = this.cache[key];