Skip to content

Commit

Permalink
drop #each API
Browse files Browse the repository at this point in the history
When `#get` or `#set` are called during iteration, it possibly ends up with an
invalid state. And it's hard to support stable `#each` with current
implementation.
  • Loading branch information
smagch committed Sep 26, 2013
1 parent ef99286 commit bd4b767
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 37 deletions.
26 changes: 7 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,33 +205,21 @@
},

/**
* return array of keys
* return array of keys in least recently used order
* @return {Array}
*/
keys: function () {
var i = 0
var count = 0
, tail = this._tail
, head = this._head
, keys = new Array(this._len);

this.each(function (val, key) {
keys[i++] = key;
});

return keys;
},

/**
* call function in least recently used order
* the most recently used cache will be executed last
* @param {Function}
* @param {Object}
*/
each: function (fn, context) {
var tail = this._tail
, head = this._head;
for (var i = tail; i < head; i++) {
var entry = this._byOrder[i];
if (entry) fn.call(context, entry.val, entry.key);
if (entry) keys[count++] = entry.key;
}

return keys;
},

/**
Expand Down
18 changes: 0 additions & 18 deletions test/simple-lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,24 +127,6 @@ describe('SimpleLRU', function () {
})
})

describe('#each', function () {
it('should loop through in least recently used order', function () {
var cache = new SimpleLRU(3)
cache.set('b', 'B')
cache.set('a', 'A')
cache.get('b')
cache.set('c', 'C')
var keys = ['a', 'b', 'c']
var values = ['A', 'B', 'C']
var i = 0
cache.each(function (val, key) {
expect(val).to.be(values[i])
expect(key).to.be(keys[i])
i++
})
})
})

describe('#length', function () {
it('should return total number of cache', function () {
var cache = new SimpleLRU(3)
Expand Down

0 comments on commit bd4b767

Please sign in to comment.