From bd4b7670aa10b33822f6e03d9f1b89ccec67b00d Mon Sep 17 00:00:00 2001 From: smagch Date: Thu, 26 Sep 2013 21:45:49 +0900 Subject: [PATCH] drop `#each` API 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. --- index.js | 26 +++++++------------------- test/simple-lru.js | 18 ------------------ 2 files changed, 7 insertions(+), 37 deletions(-) diff --git a/index.js b/index.js index 46b3839..bb2aa38 100644 --- a/index.js +++ b/index.js @@ -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; }, /** diff --git a/test/simple-lru.js b/test/simple-lru.js index d53e453..1b6bf8e 100644 --- a/test/simple-lru.js +++ b/test/simple-lru.js @@ -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)