Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tiny-lru

Package Overview
Dependencies
Maintainers
1
Versions
102
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tiny-lru - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

241

lib/tiny-lru.js

@@ -9,3 +9,3 @@ /**

* @module tiny-lru
* @version 1.0.1
* @version 1.0.2
*/

@@ -16,153 +16,144 @@ ( function ( global ) {

/**
* Least Recently Used cache
* LRU factory
*
* @class lru
* @method lru
* @param {Number} max [Optional] Max size of cache, default is 1000
* @return {Object} LRU instance
*/
var lru = {
/**
* LRU cache factory
*
* @method factory
* @param {Number} max [Optional] Max size of cache, default is 1000
* @return {Object} LRU instance
*/
factory : function ( max ) {
var self = new LRU();
var lru = function ( max ) {
var self = new LRU();
if ( !isNaN( max ) ) {
self.max = max;
}
if ( !isNaN( max ) ) {
self.max = max;
}
return self;
},
return self;
};
// Inherited by LRUs
methods : {
/**
* Evicts the least recently used item from cache
*
* @method evict
* @return {Object} LRU instance
*/
evict : function () {
if ( this.last !== null ) {
this.remove( this.last );
}
/**
* Least Recently Used cache
*
* @method LRU
* @constructor
* @private
*/
function LRU () {
this.cache = {};
this.max = 1000;
this.first = null;
this.last = null;
this.length = 0;
}
return this;
},
// Setting constructor loop
LRU.prototype.constructor = LRU;
/**
* Gets cached item and moves it to the front
*
* @method get
* @param {String} key Item key
* @return {Mixed} Undefined or Item value
*/
get : function ( key ) {
var item = this.cache[key];
/**
* 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 ( item === undefined ) {
return;
}
return this;
};
this.set( key, item.value );
/**
* 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];
return item.value;
},
if ( item === undefined ) {
return;
}
/**
* Removes item from cache
*
* @method remove
* @param {String} key Item key
* @return {Object} Item
*/
remove : function ( key ) {
var item = this.cache[ key ];
this.set( key, item.value );
if ( item !== undefined ) {
delete this.cache[key];
return item.value;
};
this.length--;
/**
* 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 ( item.previous !== null ) {
this.cache[item.previous].next = item.next;
}
if ( item !== undefined ) {
delete this.cache[key];
if ( item.next !== null ) {
this.cache[item.next].previous = item.previous;
}
this.length--;
if ( this.first === key ) {
this.first = item.previous;
}
if ( item.previous !== null ) {
this.cache[item.previous].next = item.next;
}
if ( this.last === key ) {
this.last = item.next;
}
}
if ( item.next !== null ) {
this.cache[item.next].previous = item.previous;
}
return item;
},
if ( this.first === key ) {
this.first = item.previous;
}
/**
* Sets item in cache as `first`
*
* @method set
* @param {String} key Item key
* @param {Mixed} value Item value
* @return {Object} LRU instance
*/
set : function ( key, value ) {
var item = this.remove( key );
if ( this.last === key ) {
this.last = item.next;
}
}
if ( item === undefined ) {
item = new LRUItem( value );
}
else {
item.value = value;
}
return item;
};
item.next = null;
item.previous = this.first;
this.cache[key] = item;
/**
* 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 );
if ( this.first !== null ) {
this.cache[this.first].next = key;
}
if ( item === undefined ) {
item = new LRUItem( value );
}
else {
item.value = value;
}
this.first = key;
item.next = null;
item.previous = this.first;
this.cache[key] = item;
if ( this.last === null ) {
this.last = key;
}
if ( this.first !== null ) {
this.cache[this.first].next = key;
}
if ( ++this.length > this.max ) {
this.evict();
}
this.first = key;
return this;
}
if ( this.last === null ) {
this.last = key;
}
if ( ++this.length > this.max ) {
this.evict();
}
return this;
};
/**
* LRU factory
*
* @method LRU
* @constructor
*/
function LRU () {
this.cache = {};
this.max = 1000;
this.first = null;
this.last = null;
this.length = 0;
}
// Setting prototype & constructor loop
LRU.prototype = lru.methods;
LRU.prototype.constructor = LRU;
/**
* LRU Item factory

@@ -184,12 +175,12 @@ *

if ( typeof exports !== "undefined" ) {
module.exports = lru.factory;
module.exports = lru;
}
else if ( typeof define === "function" ) {
define( function () {
return lru.factory;
return lru;
});
}
else {
global.lru = lru.factory;
global.lru = lru;
}
})( this );
{
"name": "tiny-lru",
"description": "Tiny LRU cache for Client or Server",
"version": "1.0.1",
"version": "1.0.2",
"homepage": "https://github.com/avoidwork/tiny-lru",

@@ -6,0 +6,0 @@ "author": {

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

[![build status](https://secure.travis-ci.org/avoidwork/tiny-lru.png)](http://travis-ci.org/avoidwork/tiny-lru)
# Tiny LRU
Least Recently Used cache for Client or Server.
[![build status](https://secure.travis-ci.org/avoidwork/tiny-lru.png)](http://travis-ci.org/avoidwork/tiny-lru)
```javascript

@@ -14,3 +16,3 @@ var cache = lru(500);

@return {Object} LRU instance
return {Object} LRU instance

@@ -41,4 +43,4 @@ **Example**

@param {String} key Item key
@return {Mixed} Undefined or Item value
param {String} key Item key
return {Mixed} Undefined or Item value

@@ -108,4 +110,4 @@ **Example**

@param {String} key Item key
@return {Object} Item
param {String} key Item key
return {Object} Item

@@ -123,5 +125,5 @@ **Example**

@param {String} key Item key
@param {Mixed} value Item value
@return {Object} LRU instance
param {String} key Item key
param {Mixed} value Item value
return {Object} LRU instance

@@ -135,7 +137,3 @@ **Example**

## License
abaaso is licensed under BSD-3 https://raw.github.com/avoidwork/tiny-lru/master/LICENSE
### Copyright
Copyright (c) 2013, Jason Mulligan <jason.mulligan@avoidwork.com>
Copyright (c) 2013 Jason Mulligan
Licensed under the BSD-3 license.

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc