Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions lib/simple_lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,10 @@ Cache.prototype.detach = function(element){
this.size--
}

Cache.prototype.forEach = function(callback){
var self = this
Cache.prototype.forEach = function(callback, thisArg){
Object.keys(this.cache).forEach(function(key){
var val = self.cache[key]
callback(val.value,key)
})
var val = this.cache[key]
callback.call(thisArg, val.value, key, this)
}, this)
}
module.exports=Cache
38 changes: 37 additions & 1 deletion test/simple_lru_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,41 @@ describe("BigCache Config",function(){

for(var i = 0; i < 100; i++)
cache.get(i).should.equal("value_"+i+"_modif")
})
})
it("Should have forEach with the same interface as Array#forEach", function() {
var cache = new SimpleCache({maxSize:10})
for(var i = 1; i <= 10; i++)
cache.set(i,"value_"+i)

var testContext1 = { test:1 }
var testContext2 = { other: 2 }

cache.forEach(function(value,key,passedCache){
should.exist(this)
should.exist(this.test)
should.exist(value)
should.exist(key)
should.exist(passedCache)
should(key).be.a.String();
should(passedCache).be.instanceof(SimpleCache);
this.should.equal(testContext1)
this.test.should.equal(1)
value.should.equal("value_" + key)
should(+key).be.greaterThanOrEqual(1)
should(+key).be.lessThanOrEqual(10)
passedCache.should.equal(cache)
}, testContext1);

cache.forEach(function(value,key,passedCache){
should.exist(this)
should.exist(this.other)
this.other.should.be.equal(2)
this.should.equal(testContext2)
}, testContext2);

var returnValue = cache.forEach(function(value, key) {
return 42;
});
should(returnValue).be.undefined();
})
})