lru-cache
Advanced tools
Comparing version 2.7.0 to 2.7.1
@@ -16,2 +16,7 @@ ;(function () { // closure for web browsers | ||
function typeCheckKey(key) { | ||
if (typeof key !== 'string' && typeof key !== 'number') | ||
throw new TypeError("key must be a string or number. " + typeof key) | ||
} | ||
function LRUCache (options) { | ||
@@ -167,2 +172,4 @@ if (!(this instanceof LRUCache)) | ||
maxAge = maxAge || this._maxAge | ||
typeCheckKey(key) | ||
var now = maxAge ? Date.now() : 0 | ||
@@ -212,2 +219,3 @@ var len = this._lengthCalculator(value) | ||
LRUCache.prototype.has = function (key) { | ||
typeCheckKey(key) | ||
if (!hOP(this._cache, key)) return false | ||
@@ -222,2 +230,3 @@ var hit = this._cache[key] | ||
LRUCache.prototype.get = function (key) { | ||
typeCheckKey(key) | ||
return get(this, key, true) | ||
@@ -227,2 +236,3 @@ } | ||
LRUCache.prototype.peek = function (key) { | ||
typeCheckKey(key) | ||
return get(this, key, false) | ||
@@ -238,2 +248,3 @@ } | ||
LRUCache.prototype.del = function (key) { | ||
typeCheckKey(key) | ||
del(this, this._cache[key]) | ||
@@ -250,2 +261,3 @@ } | ||
var hit = arr[l] | ||
typeCheckKey(hit.k) | ||
var expiresAt = hit.e || 0 | ||
@@ -264,2 +276,3 @@ if (expiresAt === 0) { | ||
function get (self, key, doUse) { | ||
typeCheckKey(key) | ||
var hit = self._cache[key] | ||
@@ -266,0 +279,0 @@ if (hit) { |
{ | ||
"name": "lru-cache", | ||
"description": "A cache object that deletes the least-recently-used items.", | ||
"version": "2.7.0", | ||
"version": "2.7.1", | ||
"author": "Isaac Z. Schlueter <i@izs.me>", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -232,3 +232,3 @@ var test = require("tap").test | ||
test("individual item can have it's own maxAge", function(t) { | ||
test("individual item can have its own maxAge", function(t) { | ||
var cache = new LRU({ | ||
@@ -246,3 +246,3 @@ max: 5, | ||
test("individual item can have it's own maxAge > cache's", function(t) { | ||
test("individual item can have its own maxAge > cache's", function(t) { | ||
var cache = new LRU({ | ||
@@ -399,1 +399,90 @@ max: 5, | ||
}) | ||
test("get and set only accepts strings and numbers as keys", function(t) { | ||
var cache = new LRU() | ||
cache.set("key", "value") | ||
cache.set(123, 456) | ||
t.equal(cache.get("key"), "value") | ||
t.equal(cache.get(123), 456) | ||
t.throws(function() { | ||
cache.set({ someObjectKey: true }, "a") | ||
}, "set should not accept objects as keys") | ||
t.throws(function() { | ||
cache.get({ someObjectKey: true }) | ||
}, "get should not accept objects as keys") | ||
t.throws(function() { | ||
cache.set([1,2,3], "b") | ||
}, "set should not accept arrays as keys") | ||
t.throws(function() { | ||
cache.get([1,2,3]) | ||
}, "get should not accept arrays as keys") | ||
t.end() | ||
}) | ||
test("peek only accepts strings and numbers as keys", function(t) { | ||
var cache = new LRU() | ||
cache.set("key", "value") | ||
cache.set(123, 456) | ||
t.equal(cache.peek("key"), "value") | ||
t.equal(cache.peek(123), 456) | ||
t.throws(function() { | ||
cache.peek({ someObjectKey: true }) | ||
}, "peek should not accept objects as keys") | ||
t.throws(function() { | ||
cache.peek([1,2,3]) | ||
}, "peek should not accept arrays as keys") | ||
t.end() | ||
}) | ||
test("del only accepts strings and numbers as keys", function(t) { | ||
var cache = new LRU() | ||
cache.set("key", "value") | ||
cache.set(123, 456) | ||
cache.del("key") | ||
cache.del(123) | ||
t.assertNot(cache.has("key")) | ||
t.assertNot(cache.has(123)) | ||
t.throws(function() { | ||
cache.del({ someObjectKey: true }) | ||
}, "del should not accept objects as keys") | ||
t.throws(function() { | ||
cache.del([1,2,3]) | ||
}, "del should not accept arrays as keys") | ||
t.end() | ||
}) | ||
test("has only accepts strings and numbers as keys", function(t) { | ||
var cache = new LRU() | ||
cache.has("key") | ||
cache.has(123) | ||
t.throws(function() { | ||
cache.has({ someObjectKey: true }) | ||
}, "has should not accept objects as keys") | ||
t.throws(function() { | ||
cache.has([1,2,3]) | ||
}, "has should not accept arrays as keys") | ||
t.end() | ||
}) |
@@ -16,2 +16,10 @@ var test = require('tap').test | ||
cache.set(123, 456) | ||
t.deepEqual(cache.dump(), [ | ||
{ k: 123, v: 456, e: 0 }, | ||
{ k: "b", v: "B", e: 0 }, | ||
{ k: "a", v: "A", e: 0 }, | ||
]) | ||
cache.del(123) | ||
cache.set("a", "A"); | ||
@@ -92,2 +100,3 @@ t.deepEqual(cache.dump(), [ | ||
cache.set("b", "B") | ||
cache.set(123, 456) | ||
@@ -219,1 +228,22 @@ copy.load(cache.dump()) | ||
test("type checking of keys during load", function(t) { | ||
var cache = new LRU() | ||
t.throws(function() { | ||
cache.load([{ | ||
k: { someObjectKey: true }, | ||
v: "B", | ||
e: 0 | ||
}]) | ||
}, "load should not accept objects as keys") | ||
t.throws(function() { | ||
cache.load([{ | ||
k: [1,2,3], | ||
v: "C", | ||
e: 0 | ||
}]) | ||
}, "load should not accept arrays as keys") | ||
t.end() | ||
}) |
32363
1021