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.0.2 to 1.1.0

lib/tiny-lru.es6.js

269

lib/tiny-lru.js

@@ -0,183 +1,148 @@

"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"); } }
/**
* tiny-lru
* Tiny LRU cache for Client or Server
*
* @author Jason Mulligan <jason.mulligan@avoidwork.com>
* @copyright 2013 Jason Mulligan
* @license BSD-3 <https://raw.github.com/avoidwork/tiny-lru/master/LICENSE>
* @copyright 2016
* @license BSD-3-Clause
* @link https://github.com/avoidwork/tiny-lru
* @module tiny-lru
* @version 1.0.2
* @version 1.1.0
*/
( function ( global ) {
"use strict";
(function (global) {
var LRUItem = function LRUItem(value) {
_classCallCheck(this, LRUItem);
/**
* LRU factory
*
* @method lru
* @param {Number} max [Optional] Max size of cache, default is 1000
* @return {Object} LRU instance
*/
var lru = function ( max ) {
var self = new LRU();
this.next = null;
this.previous = null;
this.value = value;
};
if ( !isNaN( max ) ) {
self.max = max;
}
var LRU = function () {
function LRU(max) {
_classCallCheck(this, LRU);
return self;
};
this.cache = {};
this.max = max;
this.first = null;
this.last = null;
this.length = 0;
}
/**
* Least Recently Used cache
*
* @method LRU
* @constructor
* @private
*/
function LRU () {
this.cache = {};
this.max = 1000;
this.first = null;
this.last = null;
this.length = 0;
}
_createClass(LRU, [{
key: "delete",
value: function _delete(key) {
return this.remove(key);
}
}, {
key: "evict",
value: function evict() {
if (this.last !== null) {
this.remove(this.last);
}
// Setting constructor loop
LRU.prototype.constructor = LRU;
return this;
}
}, {
key: "get",
value: function get(key) {
var cached = this.cache[key],
output = undefined;
/**
* Evicts the least recently used item from cache
*
* @method evict
* @return {Object} LRU instance
*/
LRU.prototype.evict = function () {
if ( this.last !== null ) {
this.remove( this.last );
}
if (cached) {
output = cached.value;
this.set(key, cached.value);
}
return this;
};
return output;
}
}, {
key: "has",
value: function has(key) {
return this.cache[key] !== undefined;
}
}, {
key: "remove",
value: function remove(key) {
var cached = this.cache[key];
/**
* Gets cached item and moves it to the front
*
* @method get
* @param {String} key Item key
* @return {Mixed} Undefined or Item value
*/
LRU.prototype.get = function ( key ) {
var item = this.cache[key];
if (cached) {
delete this.cache[key];
this.length--;
if ( item === undefined ) {
return;
}
if (cached.previous !== null) {
this.cache[cached.previous].next = cached.next;
}
this.set( key, item.value );
if (cached.next !== null) {
this.cache[cached.next].previous = cached.previous;
}
return item.value;
};
if (this.first === key) {
this.first = cached.previous;
}
/**
* Removes item from cache
*
* @method remove
* @param {String} key Item key
* @return {Object} Item
*/
LRU.prototype.remove = function ( key ) {
var item = this.cache[ key ];
if (this.last === key) {
this.last = cached.next;
}
}
if ( item !== undefined ) {
delete this.cache[key];
return cached;
}
}, {
key: "set",
value: function set(key, value) {
var obj = this.remove(key);
this.length--;
if (!obj) {
obj = new LRUItem(value);
} else {
obj.value = value;
}
if ( item.previous !== null ) {
this.cache[item.previous].next = item.next;
}
obj.next = null;
obj.previous = this.first;
this.cache[key] = obj;
if ( item.next !== null ) {
this.cache[item.next].previous = item.previous;
}
if (this.first) {
this.cache[this.first].next = key;
}
if ( this.first === key ) {
this.first = item.previous;
}
this.first = key;
if ( this.last === key ) {
this.last = item.next;
}
}
if (!this.last) {
this.last = key;
}
return item;
};
if (++this.length > this.max) {
this.evict();
}
/**
* Sets item in cache as `first`
*
* @method set
* @param {String} key Item key
* @param {Mixed} value Item value
* @return {Object} LRU instance
*/
LRU.prototype.set = function ( key, value ) {
var item = this.remove( key );
return this;
}
}]);
if ( item === undefined ) {
item = new LRUItem( value );
}
else {
item.value = value;
}
return LRU;
}();
item.next = null;
item.previous = this.first;
this.cache[key] = item;
function factory() {
var max = arguments.length <= 0 || arguments[0] === undefined ? 1000 : arguments[0];
if ( this.first !== null ) {
this.cache[this.first].next = key;
return new LRU(max);
}
this.first = key;
if ( this.last === null ) {
this.last = key;
// 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;
}
if ( ++this.length > this.max ) {
this.evict();
}
return this;
};
/**
* LRU Item factory
*
* @param {Mixed} value Item value
* @constructor
*/
function LRUItem ( value ) {
this.next = null;
this.previous = null;
this.value = value;
}
// Setting prototype & constructor loop
LRUItem.prototype.constructor = LRUItem;
// CommonJS, AMD, script tag
if ( typeof exports !== "undefined" ) {
module.exports = lru;
}
else if ( typeof define === "function" ) {
define( function () {
return lru;
});
}
else {
global.lru = lru;
}
})( this );
})(typeof window !== "undefined" ? window : global);
{
"name": "tiny-lru",
"description": "Tiny LRU cache for Client or Server",
"version": "1.0.2",
"version": "1.1.0",
"homepage": "https://github.com/avoidwork/tiny-lru",
"author": {
"name": "Jason Mulligan",
"email": "jason.mulligan@avoidwork.com"
},
"author": "Jason Mulligan <jason.mulligan@avoidwork.com>",
"repository": {

@@ -17,8 +14,3 @@ "type": "git",

},
"licenses": [
{
"type": "BSD-3",
"url": "https://raw.github.com/avoidwork/tiny-lru/master/LICENSE"
}
],
"license": "BSD-3-Clause",
"main": "lib/tiny-lru",

@@ -33,10 +25,13 @@ "engines": {

"devDependencies": {
"grunt": "~0.4.1",
"grunt-cli": "~0.1.6",
"grunt-exec": "~0.4",
"grunt-sed": "~0.1",
"grunt-contrib-concat": "~0.1.3",
"grunt-contrib-jshint": "~0.1",
"grunt-contrib-nodeunit": "~0.1.2",
"grunt-contrib-watch": "~0.2"
"babel-eslint": "^4.1.4",
"babel-preset-es2015": "^6.1.2",
"grunt": "^0.4.5",
"grunt-babel": "^6.0.0",
"grunt-cli": "^0.1.13",
"grunt-contrib-concat": "^0.1.3",
"grunt-contrib-nodeunit": "^0.4.1",
"grunt-contrib-uglify": "^0.9.1",
"grunt-contrib-watch": "^0.2.0",
"grunt-eslint": "^17.3.1",
"grunt-sed": "^0.1.1"
},

@@ -46,4 +41,9 @@ "keywords": [

"cache",
"tiny"
"tiny",
"client",
"server",
"least",
"recently",
"used"
]
}

@@ -5,3 +5,3 @@ # Tiny LRU

[![build status](https://secure.travis-ci.org/avoidwork/tiny-lru.png)](http://travis-ci.org/avoidwork/tiny-lru)
[![build status](https://secure.travis-ci.org/avoidwork/tiny-lru.svg)](http://travis-ci.org/avoidwork/tiny-lru)

@@ -133,4 +133,12 @@ ```javascript

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) 2013 Jason Mulligan
Copyright (c) 2016 Jason Mulligan
Licensed under the BSD-3 license.

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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