lru-cache
Advanced tools
Comparing version 2.7.2 to 2.7.3
@@ -16,5 +16,8 @@ ;(function () { // closure for web browsers | ||
var didTypeWarning = false | ||
function typeCheckKey(key) { | ||
if (typeof key !== 'string' && typeof key !== 'number') | ||
throw new TypeError("key must be a string or number. " + typeof key) | ||
if (!didTypeWarning && typeof key !== 'string' && typeof key !== 'number') { | ||
didTypeWarning = true | ||
console.error(new TypeError("LRU: key must be a string or number. Almost certainly a bug! " + typeof key).stack) | ||
} | ||
} | ||
@@ -218,5 +221,3 @@ | ||
LRUCache.prototype.has = function (key) { | ||
if (typeof key !== 'string' && typeof key !== 'number') | ||
return false | ||
typeCheckKey(key) | ||
if (!hOP(this._cache, key)) return false | ||
@@ -231,4 +232,3 @@ var hit = this._cache[key] | ||
LRUCache.prototype.get = function (key) { | ||
if (typeof key !== 'string' && typeof key !== 'number') | ||
return | ||
typeCheckKey(key) | ||
return get(this, key, true) | ||
@@ -238,4 +238,3 @@ } | ||
LRUCache.prototype.peek = function (key) { | ||
if (typeof key !== 'string' && typeof key !== 'number') | ||
return | ||
typeCheckKey(key) | ||
return get(this, key, false) | ||
@@ -251,4 +250,3 @@ } | ||
LRUCache.prototype.del = function (key) { | ||
if (typeof key !== 'string' && typeof key !== 'number') | ||
return | ||
typeCheckKey(key) | ||
del(this, this._cache[key]) | ||
@@ -255,0 +253,0 @@ } |
{ | ||
"name": "lru-cache", | ||
"description": "A cache object that deletes the least-recently-used items.", | ||
"version": "2.7.2", | ||
"version": "2.7.3", | ||
"author": "Isaac Z. Schlueter <i@izs.me>", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -27,2 +27,20 @@ # lru cache | ||
## Keys should always be Strings or Numbers | ||
Note: this module will print warnings to `console.error` if you use a | ||
key that is not a String or Number. Because items are stored in an | ||
object, which coerces keys to a string, it won't go well for you if | ||
you try to use a key that is not a unique string, it'll cause surprise | ||
collisions. For example: | ||
```JavaScript | ||
// Bad Example! Dont' do this! | ||
var cache = LRU() | ||
var a = {} | ||
var b = {} | ||
cache.set(a, 'this is a') | ||
cache.set(b, 'this is b') | ||
console.log(cache.get(a)) // prints: 'this is b' | ||
``` | ||
## Options | ||
@@ -29,0 +47,0 @@ |
@@ -232,3 +232,3 @@ var test = require("tap").test | ||
test("individual item can have its own maxAge", function(t) { | ||
test("individual item can have it's own maxAge", function(t) { | ||
var cache = new LRU({ | ||
@@ -246,3 +246,3 @@ max: 5, | ||
test("individual item can have its own maxAge > cache's", function(t) { | ||
test("individual item can have it's own maxAge > cache's", function(t) { | ||
var cache = new LRU({ | ||
@@ -399,61 +399,1 @@ 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.set([1,2,3], "b") | ||
}, "set 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.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)) | ||
cache.set('[object Object]', 123) | ||
t.assertNot(cache.has({})) | ||
t.assert(cache.has(String({}))) | ||
t.end() | ||
}) | ||
test("has only accepts strings and numbers as keys", function(t) { | ||
var cache = new LRU() | ||
cache.has("key") | ||
cache.has(123) | ||
t.end() | ||
}) |
@@ -16,10 +16,2 @@ 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"); | ||
@@ -100,3 +92,2 @@ t.deepEqual(cache.dump(), [ | ||
cache.set("b", "B") | ||
cache.set(123, 456) | ||
@@ -228,22 +219,1 @@ 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() | ||
}) |
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
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
138
30557
936