Comparing version 6.1.0 to 7.0.0
@@ -1,169 +0,1 @@ | ||
/** | ||
* Tiny LRU cache for Client or Server | ||
* | ||
* @author Jason Mulligan <jason.mulligan@avoidwork.com> | ||
* @copyright 2019 | ||
* @license BSD-3-Clause | ||
* @version 6.1.0 | ||
*/ | ||
"use strict"; | ||
(function (global) { | ||
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); | ||
} | ||
// Node, AMD & window supported | ||
if (typeof exports !== "undefined") { | ||
module.exports = factory; | ||
} else if (typeof define === "function" && define.amd !== void 0) { | ||
define(() => factory); | ||
} else { | ||
global.lru = factory; | ||
} | ||
}(typeof window !== "undefined" ? window : global)); | ||
!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):(t=t||self).tinyLru=i()}(this,(function(){"use strict";class t{constructor(t=0,i=0){this.first=null,this.items={},this.last=null,this.max=t,this.size=0,this.ttl=i}has(t){return t in this.items}clear(){return this.first=null,this.items={},this.last=null,this.size=0,this}delete(t){if(this.has(t)){const i=this.items[t];delete this.items[t],this.size--,null!==i.prev&&(i.prev.next=i.next),null!==i.next&&(i.next.prev=i.prev),this.first===i&&(this.first=i.next),this.last===i&&(this.last=i.prev)}return this}evict(){const t=this.first;return delete this.items[t.key],this.first=t.next,this.first.prev=null,this.size--,this}get(t){let i;if(this.has(t)){const s=this.items[t];this.ttl>0&&s.expiry<=(new Date).getTime()?this.delete(t):(i=s.value,this.set(t,i,!0))}return i}keys(){return Object.keys(this.items)}set(t,i,s=!1){let e;if(s||this.has(t)){if((e=this.items[t]).value=i,this.last!==e){const t=this.last,i=e.next,s=e.prev;this.first===e&&(this.first=e.next),e.next=null,e.prev=this.last,t.next=e,null!==s&&(s.next=i),null!==i&&(i.prev=s)}}else this.max>0&&this.size===this.max&&this.evict(),e=this.items[t]={expiry:this.ttl>0?(new Date).getTime()+this.ttl:this.ttl,key:t,prev:this.last,next:null,value:i},1==++this.size?this.first=e:this.last.next=e;return this.last=e,this}}return function(i=1e3,s=0){if(isNaN(i)||i<0)throw new TypeError("Invalid max value");if(isNaN(s)||s<0)throw new TypeError("Invalid ttl value");return new t(i,s)}})); |
{ | ||
"name": "tiny-lru", | ||
"description": "Tiny LRU cache for Client or Server", | ||
"version": "6.1.0", | ||
"version": "7.0.0", | ||
"homepage": "https://github.com/avoidwork/tiny-lru", | ||
@@ -14,6 +14,12 @@ "author": "Jason Mulligan <jason.mulligan@avoidwork.com>", | ||
}, | ||
"files": [ | ||
"lib", | ||
"*.d.ts" | ||
], | ||
"license": "BSD-3-Clause", | ||
"main": "lib/tiny-lru.js", | ||
"module": "lib/tiny-lru.es6.js", | ||
"types": "lib/tiny-lru.d.ts", | ||
"source": "src/lru.js", | ||
"browser": "lib/tiny-lru.js", | ||
"main": "lib/tiny-lru.cjs.js", | ||
"module": "lib/tiny-lru.esm.js", | ||
"types": "tiny-lru.d.ts", | ||
"engines": { | ||
@@ -25,17 +31,9 @@ "node": ">=6" | ||
"benchmark": "node benchmark.js", | ||
"test": "grunt test", | ||
"build": "grunt build" | ||
"test": "nodeunit test/lru.js", | ||
"build": "tslib build" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"babel-core": "^6.26.3", | ||
"babel-minify": "^0.5.1", | ||
"babel-preset-env": "^1.7.0", | ||
"grunt": "^1.0.4", | ||
"grunt-cli": "^1.3.2", | ||
"grunt-contrib-concat": "^1.0.1", | ||
"grunt-contrib-copy": "^1.0.0", | ||
"grunt-contrib-nodeunit": "^2.0.0", | ||
"grunt-contrib-watch": "^1.1.0", | ||
"grunt-eslint": "^22.0.0", | ||
"tslib-cli": "^5.0.17", | ||
"nodeunit": "^0.11.3", | ||
"precise": "^1.1.0" | ||
@@ -42,0 +40,0 @@ }, |
@@ -21,15 +21,2 @@ # Tiny LRU | ||
For usage with webpack, you can `import lru from 'tiny-lru/lib/tiny-lru.es5'` or create an [alias](https://webpack.js.org/configuration/resolve/#resolve-alias): | ||
```javascript | ||
resolve: { | ||
alias: { | ||
'tiny-lru': 'tiny-lru/lib/tiny-lru.es5.js' | ||
} | ||
} | ||
// This should work now | ||
import lru from 'tiny-lru'; | ||
``` | ||
## clear | ||
@@ -36,0 +23,0 @@ ### Method |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3
12555
7
253
185
1