Skip to content

Commit

Permalink
roll back key type checking
Browse files Browse the repository at this point in the history
Too aggressive, breaks backwards compatibility
  • Loading branch information
isaacs committed Nov 25, 2015
1 parent c70ccfd commit 2e2d3d4
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 110 deletions.
18 changes: 0 additions & 18 deletions lib/lru-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ function hOP (obj, key) {

function naiveLength () { return 1 }

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) {
if (!(this instanceof LRUCache))
return new LRUCache(options)
Expand Down Expand Up @@ -168,8 +163,6 @@ LRUCache.prototype.dumpLru = function () {

LRUCache.prototype.set = function (key, value, maxAge) {
maxAge = maxAge || this._maxAge
typeCheckKey(key)

var now = maxAge ? Date.now() : 0
var len = this._lengthCalculator(value)

Expand Down Expand Up @@ -214,9 +207,6 @@ LRUCache.prototype.set = function (key, value, maxAge) {
}

LRUCache.prototype.has = function (key) {
if (typeof key !== 'string' && typeof key !== 'number')
return false

if (!hOP(this._cache, key)) return false
var hit = this._cache[key]
if (isStale(this, hit)) {
Expand All @@ -226,14 +216,10 @@ LRUCache.prototype.has = function (key) {
}

LRUCache.prototype.get = function (key) {
if (typeof key !== 'string' && typeof key !== 'number')
return
return get(this, key, true)
}

LRUCache.prototype.peek = function (key) {
if (typeof key !== 'string' && typeof key !== 'number')
return
return get(this, key, false)
}

Expand All @@ -244,8 +230,6 @@ LRUCache.prototype.pop = function () {
}

LRUCache.prototype.del = function (key) {
if (typeof key !== 'string' && typeof key !== 'number')
return
del(this, this._cache[key])
}

Expand All @@ -257,7 +241,6 @@ LRUCache.prototype.load = function (arr) {
//A previous serialized cache has the most recent items first
for (var l = arr.length - 1; l >= 0; l-- ) {
var hit = arr[l]
typeCheckKey(hit.k)
var expiresAt = hit.e || 0
if (expiresAt === 0) {
//the item was created without expiration in a non aged cache
Expand All @@ -271,7 +254,6 @@ LRUCache.prototype.load = function (arr) {
}

function get (self, key, doUse) {
typeCheckKey(key)
var hit = self._cache[key]
if (hit) {
if (isStale(self, hit)) {
Expand Down
64 changes: 2 additions & 62 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ test("drop the old items", function(t) {
}, 155)
})

test("individual item can have its own maxAge", function(t) {
test("individual item can have it's own maxAge", function(t) {

This comment has been minimized.

Copy link
@ronkorving

ronkorving Nov 26, 2015

actually, these changes were good :)

This comment has been minimized.

Copy link
@tedpennings

tedpennings Nov 26, 2015

Contributor

👓

var cache = new LRU({
max: 5,
maxAge: 50
Expand All @@ -242,7 +242,7 @@ test("individual item can have its own maxAge", function(t) {
}, 25)
})

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({
max: 5,
maxAge: 20
Expand Down Expand Up @@ -394,63 +394,3 @@ test("pop the least used item", function (t) {

t.end()
})

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()
})
30 changes: 0 additions & 30 deletions test/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ test('dump', function (t) {
{ k: "a", v: "A", e: 0 }
])

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");
t.deepEqual(cache.dump(), [
{ k: "a", v: "A", e: 0 },
Expand Down Expand Up @@ -96,7 +88,6 @@ test("load basic cache", function(t) {

cache.set("a", "A")
cache.set("b", "B")
cache.set(123, 456)

copy.load(cache.dump())
t.deepEquals(cache.dump(), copy.dump())
Expand Down Expand Up @@ -223,24 +214,3 @@ test("load to other age cache", function(t) {

})

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()
})

0 comments on commit 2e2d3d4

Please sign in to comment.