Socket
Socket
Sign inDemoInstall

tiny-lru

Package Overview
Dependencies
0
Maintainers
1
Versions
97
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.0 to 1.3.0

bower.json

197

lib/tiny-lru.es6.js

@@ -8,122 +8,145 @@ /**

* @link https://github.com/avoidwork/tiny-lru
* @version 1.1.0
* @version 1.3.0
*/
"use strict";
(function (global) {
let next = typeof process !== "undefined" ? process.nextTick : arg => setTimeout(arg, 1);
class LRUItem {
constructor (value) {
this.next = null;
this.previous = null;
this.value = value;
}
}
class LRU {
constructor (max) {
this.cache = {};
this.max = max;
this.first = null;
this.last = null;
this.length = 0;
}
class LRU {
constructor (max) {
this.cache = {};
this.first = null;
this.last = null;
this.length = 0;
this.max = max;
this.notify = false;
this.onchange = () => {};
}
delete (key) {
return this.remove(key);
}
delete (key) {
return this.remove(key);
}
evict () {
if (this.last !== null) {
this.remove(this.last);
dump () {
return JSON.stringify(this, null, 0);
}
return this;
}
evict () {
if (this.last !== null) {
this.remove(this.last, true);
}
get (key) {
let cached = this.cache[key],
output;
if (this.notify) {
next(this.onchange("evict", this.dump()));
}
if (cached) {
output = cached.value;
this.set(key, cached.value);
return this;
}
return output;
}
get (key) {
let cached = this.cache[key],
output;
has (key) {
return this.cache[key] !== undefined;
}
if (cached) {
output = cached.value;
this.set(key, cached.value);
}
remove (key) {
let cached = this.cache[key];
if (this.notify) {
next(this.onchange("get", this.dump()));
}
if (cached) {
delete this.cache[key];
this.length--;
return output;
}
if (cached.previous !== null) {
this.cache[cached.previous].next = cached.next;
}
has (key) {
return this.cache[key] !== undefined;
}
if (cached.next !== null) {
this.cache[cached.next].previous = cached.previous;
remove (key, silent = false) {
let cached = this.cache[key];
if (cached) {
delete this.cache[key];
this.length--;
if (cached.previous !== null) {
this.cache[cached.previous].next = cached.next;
}
if (cached.next !== null) {
this.cache[cached.next].previous = cached.previous;
}
if (this.first === key) {
this.first = cached.previous;
}
if (this.last === key) {
this.last = cached.next;
}
}
if (this.first === key) {
this.first = cached.previous;
if (!silent && this.notify) {
next(this.onchange("remove", this.dump()));
}
if (this.last === key) {
this.last = cached.next;
}
return cached;
}
return cached;
}
set (key, value) {
let obj = this.remove(key, true);
set (key, value) {
let obj = this.remove(key);
if (!obj) {
obj = {
next: null,
previous: null,
value: value
};
} else {
obj.value = value;
}
if (!obj) {
obj = new LRUItem(value);
} else {
obj.value = value;
}
obj.next = null;
obj.previous = this.first;
this.cache[key] = obj;
obj.next = null;
obj.previous = this.first;
this.cache[key] = obj;
if (this.first) {
this.cache[this.first].next = key;
}
if (this.first) {
this.cache[this.first].next = key;
}
this.first = key;
this.first = key;
if (!this.last) {
this.last = key;
}
if (!this.last) {
this.last = key;
}
if (++this.length > this.max) {
this.evict();
}
if (++this.length > this.max) {
this.evict();
if (this.notify) {
next(this.onchange("set", this.dump()));
}
return this;
}
}
return this;
function factory (max = 1000) {
return new LRU(max);
}
}
function factory (max = 1000) {
return new LRU(max);
}
// Node, AMD & window supported
if (typeof exports !== "undefined") {
module.exports = factory;
} else if (typeof define === "function" && define.amd) {
define(function () {
return factory;
});
} else {
global.lru = factory;
}}(typeof window !== "undefined" ? window : global));
// Node, AMD & window supported
if (typeof exports !== "undefined") {
module.exports = factory;
} else if (typeof define === "function" && define.amd) {
define(function () {
return factory;
});
} else {
global.lru = factory;
}
}(typeof window !== "undefined" ? window : global));

@@ -1,7 +0,1 @@

"use strict";
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**

@@ -14,11 +8,13 @@ * Tiny LRU cache for Client or Server

* @link https://github.com/avoidwork/tiny-lru
* @version 1.1.0
* @version 1.3.0
*/
"use strict";
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
(function (global) {
var LRUItem = function LRUItem(value) {
_classCallCheck(this, LRUItem);
this.next = null;
this.previous = null;
this.value = value;
var next = typeof process !== "undefined" ? process.nextTick : function (arg) {
return setTimeout(arg, 1);
};

@@ -31,6 +27,8 @@

this.cache = {};
this.max = max;
this.first = null;
this.last = null;
this.length = 0;
this.max = max;
this.notify = false;
this.onchange = function () {};
}

@@ -44,8 +42,17 @@

}, {
key: "dump",
value: function dump() {
return JSON.stringify(this, null, 0);
}
}, {
key: "evict",
value: function evict() {
if (this.last !== null) {
this.remove(this.last);
this.remove(this.last, true);
}
if (this.notify) {
next(this.onchange("evict", this.dump()));
}
return this;

@@ -57,3 +64,3 @@ }

var cached = this.cache[key],
output = undefined;
output = void 0;

@@ -65,2 +72,6 @@ if (cached) {

if (this.notify) {
next(this.onchange("get", this.dump()));
}
return output;

@@ -76,2 +87,4 @@ }

value: function remove(key) {
var silent = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var cached = this.cache[key];

@@ -100,2 +113,6 @@

if (!silent && this.notify) {
next(this.onchange("remove", this.dump()));
}
return cached;

@@ -106,6 +123,10 @@ }

value: function set(key, value) {
var obj = this.remove(key);
var obj = this.remove(key, true);
if (!obj) {
obj = new LRUItem(value);
obj = {
next: null,
previous: null,
value: value
};
} else {

@@ -133,2 +154,6 @@ obj.value = value;

if (this.notify) {
next(this.onchange("set", this.dump()));
}
return this;

@@ -142,3 +167,3 @@ }

function factory() {
var max = arguments.length <= 0 || arguments[0] === undefined ? 1000 : arguments[0];
var max = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1000;

@@ -145,0 +170,0 @@ return new LRU(max);

{
"name": "tiny-lru",
"description": "Tiny LRU cache for Client or Server",
"version": "1.1.0",
"version": "1.3.0",
"homepage": "https://github.com/avoidwork/tiny-lru",

@@ -24,3 +24,2 @@ "author": "Jason Mulligan <jason.mulligan@avoidwork.com>",

"devDependencies": {
"babel-eslint": "^4.1.4",
"babel-preset-es2015": "^6.1.2",

@@ -34,3 +33,3 @@ "grunt": "^0.4.5",

"grunt-contrib-watch": "^0.2.0",
"grunt-eslint": "^17.3.1",
"grunt-eslint": "~19.0.0",
"grunt-sed": "^0.1.1"

@@ -37,0 +36,0 @@ },

@@ -8,5 +8,15 @@ # Tiny LRU

```javascript
var cache = lru(500);
const cache = lru(500);
```
Lodash provides a `memoize` function with a cache that can be swapped out as long as it implements the right interface.
See the [lodash docs](https://lodash.com/docs#memoize) for more on `memoize`.
#### Example
```javascript
_.memoize.Cache = lru().constructor;
const memoized = _.memoize(myFunc);
memoized.cache.max = 10;
```
## evict

@@ -33,3 +43,3 @@ ### Method

```javascript
var cache = lru();
const cache = lru();

@@ -50,3 +60,3 @@ cache.first; // null - it's a new cache!

```javascript
var item = cache.get("myKey");
const item = cache.get("myKey");
```

@@ -62,3 +72,3 @@

```javascript
var cache = lru();
const cache = lru();

@@ -76,3 +86,3 @@ cache.items; // {}

```javascript
var cache = lru(500);
const cache = lru(500);

@@ -82,2 +92,34 @@ cache.max; // 500

## notify
### Property
Executes `onchange(eventName, serializedCache)` on the next tick when the cache changes
**Example**
```javascript
const cache = lru();
cache.notify = true;
cache.onchange = (event, serializedCache) => {
console.log(event, serializedCache);
};
```
## onchange
### Method
Accepts `eventName` & `serializedCache` arguments
**Example**
```javascript
const cache = lru();
cache.notify = true;
cache.onchange = (event, serializedCache) => {
console.log(event, serializedCache);
};
````
## last

@@ -91,3 +133,3 @@ ### Property

```javascript
var cache = lru();
const cache = lru();

@@ -105,3 +147,3 @@ cache.last; // null - it's a new cache!

```javascript
var cache = lru();
const cache = lru();

@@ -122,3 +164,3 @@ cache.length; // 0 - it's a new cache!

```javascript
var staleItem = cache.remove("myKey");
const staleItem = cache.remove("myKey");
```

@@ -141,12 +183,4 @@

Lodash provides a `memoize` function with a cache that can be swapped out as long as it implements the right interface. Sample usage with lodash:
```javascript
_.memoize.Cache = lru().constructor;
var memoized = _.memoize(myFunc);
memoized.cache.max = 10;
```
See the [lodash docs](https://lodash.com/docs#memoize) for more on `memoize`.
## License
Copyright (c) 2016 Jason Mulligan
Licensed under the BSD-3 license.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc