Skip to content

Commit

Permalink
Type check less aggressively
Browse files Browse the repository at this point in the history
Fix #60
  • Loading branch information
isaacs committed Nov 24, 2015
1 parent 7414f61 commit 10f5b01
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 36 deletions.
13 changes: 9 additions & 4 deletions lib/lru-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ LRUCache.prototype.set = function (key, value, maxAge) {
}

LRUCache.prototype.has = function (key) {
typeCheckKey(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 @@ -224,12 +226,14 @@ LRUCache.prototype.has = function (key) {
}

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

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

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

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

Expand Down
35 changes: 3 additions & 32 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,18 +408,10 @@ test("get and set only accepts strings and numbers as keys", function(t) {
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()
})

Expand All @@ -431,15 +423,6 @@ test("peek only accepts strings and numbers as keys", function(t) {

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

Expand All @@ -455,13 +438,9 @@ test("del only accepts strings and numbers as keys", function(t) {
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")
cache.set('[object Object]', 123)
t.assertNot(cache.has({}))
t.assert(cache.has(String({})))

t.end()
})
Expand All @@ -473,13 +452,5 @@ test("has only accepts strings and numbers as keys", function(t) {
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()
})

0 comments on commit 10f5b01

Please sign in to comment.