From b0f291973b579a63252779f72e004e663fbba8ab Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Sat, 7 Oct 2017 10:04:19 -0600 Subject: [PATCH] Method for peeking at the newest cache entry key --- src/ol/structs/lrucache.js | 9 +++++++++ test/spec/ol/structs/lrucache.test.js | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/ol/structs/lrucache.js b/src/ol/structs/lrucache.js index e0f74ec850..046e8f5adf 100644 --- a/src/ol/structs/lrucache.js +++ b/src/ol/structs/lrucache.js @@ -196,6 +196,15 @@ ol.structs.LRUCache.prototype.peekLastKey = function() { }; +/** + * Get the key of the newest item in the cache. Throws if the cache is empty. + * @return {string} The newest key. + */ +ol.structs.LRUCache.prototype.peekFirstKey = function() { + return this.newest_.key_; +}; + + /** * @return {T} value Value. */ diff --git a/test/spec/ol/structs/lrucache.test.js b/test/spec/ol/structs/lrucache.test.js index fd25890692..3b4453bee3 100644 --- a/test/spec/ol/structs/lrucache.test.js +++ b/test/spec/ol/structs/lrucache.test.js @@ -164,6 +164,30 @@ describe('ol.structs.LRUCache', function() { }); }); + describe('#peekFirstKey()', function() { + it('returns the newest key in the cache', function() { + var cache = new ol.structs.LRUCache(); + cache.set('oldest', 'oldest'); + cache.set('oldish', 'oldish'); + cache.set('newish', 'newish'); + cache.set('newest', 'newest'); + expect(cache.peekFirstKey()).to.eql('newest'); + }); + + it('works if the cache has one item', function() { + var cache = new ol.structs.LRUCache(); + cache.set('key', 'value'); + expect(cache.peekFirstKey()).to.eql('key'); + }); + + it('throws if the cache is empty', function() { + var cache = new ol.structs.LRUCache(); + expect(function() { + cache.peekFirstKey(); + }).to.throwException(); + }); + }); + describe('peeking at the last value', function() { it('returns the last key', function() { fillLRUCache(lruCache);