react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / fileset / node_modules / minimatch / node_modules / lru-cache / lib / lru-cache.js
80742 views;(function () { // closure for web browsers12if (typeof module === 'object' && module.exports) {3module.exports = LRUCache4} else {5// just set the global for non-node platforms.6this.LRUCache = LRUCache7}89function hOP (obj, key) {10return Object.prototype.hasOwnProperty.call(obj, key)11}1213function naiveLength () { return 1 }1415function LRUCache (options) {16if (!(this instanceof LRUCache))17return new LRUCache(options)1819if (typeof options === 'number')20options = { max: options }2122if (!options)23options = {}2425this._max = options.max26// Kind of weird to have a default max of Infinity, but oh well.27if (!this._max || !(typeof this._max === "number") || this._max <= 0 )28this._max = Infinity2930this._lengthCalculator = options.length || naiveLength31if (typeof this._lengthCalculator !== "function")32this._lengthCalculator = naiveLength3334this._allowStale = options.stale || false35this._maxAge = options.maxAge || null36this._dispose = options.dispose37this.reset()38}3940// resize the cache when the max changes.41Object.defineProperty(LRUCache.prototype, "max",42{ set : function (mL) {43if (!mL || !(typeof mL === "number") || mL <= 0 ) mL = Infinity44this._max = mL45if (this._length > this._max) trim(this)46}47, get : function () { return this._max }48, enumerable : true49})5051// resize the cache when the lengthCalculator changes.52Object.defineProperty(LRUCache.prototype, "lengthCalculator",53{ set : function (lC) {54if (typeof lC !== "function") {55this._lengthCalculator = naiveLength56this._length = this._itemCount57for (var key in this._cache) {58this._cache[key].length = 159}60} else {61this._lengthCalculator = lC62this._length = 063for (var key in this._cache) {64this._cache[key].length = this._lengthCalculator(this._cache[key].value)65this._length += this._cache[key].length66}67}6869if (this._length > this._max) trim(this)70}71, get : function () { return this._lengthCalculator }72, enumerable : true73})7475Object.defineProperty(LRUCache.prototype, "length",76{ get : function () { return this._length }77, enumerable : true78})798081Object.defineProperty(LRUCache.prototype, "itemCount",82{ get : function () { return this._itemCount }83, enumerable : true84})8586LRUCache.prototype.forEach = function (fn, thisp) {87thisp = thisp || this88var i = 089var itemCount = this._itemCount9091for (var k = this._mru - 1; k >= 0 && i < itemCount; k--) if (this._lruList[k]) {92i++93var hit = this._lruList[k]94if (isStale(this, hit)) {95del(this, hit)96if (!this._allowStale) hit = undefined97}98if (hit) {99fn.call(thisp, hit.value, hit.key, this)100}101}102}103104LRUCache.prototype.keys = function () {105var keys = new Array(this._itemCount)106var i = 0107for (var k = this._mru - 1; k >= 0 && i < this._itemCount; k--) if (this._lruList[k]) {108var hit = this._lruList[k]109keys[i++] = hit.key110}111return keys112}113114LRUCache.prototype.values = function () {115var values = new Array(this._itemCount)116var i = 0117for (var k = this._mru - 1; k >= 0 && i < this._itemCount; k--) if (this._lruList[k]) {118var hit = this._lruList[k]119values[i++] = hit.value120}121return values122}123124LRUCache.prototype.reset = function () {125if (this._dispose && this._cache) {126for (var k in this._cache) {127this._dispose(k, this._cache[k].value)128}129}130131this._cache = Object.create(null) // hash of items by key132this._lruList = Object.create(null) // list of items in order of use recency133this._mru = 0 // most recently used134this._lru = 0 // least recently used135this._length = 0 // number of items in the list136this._itemCount = 0137}138139// Provided for debugging/dev purposes only. No promises whatsoever that140// this API stays stable.141LRUCache.prototype.dump = function () {142return this._cache143}144145LRUCache.prototype.dumpLru = function () {146return this._lruList147}148149LRUCache.prototype.set = function (key, value, maxAge) {150maxAge = maxAge || this._maxAge151var now = maxAge ? Date.now() : 0152153if (hOP(this._cache, key)) {154// dispose of the old one before overwriting155if (this._dispose)156this._dispose(key, this._cache[key].value)157158this._cache[key].now = now159this._cache[key].maxAge = maxAge160this._cache[key].value = value161this.get(key)162return true163}164165var len = this._lengthCalculator(value)166var hit = new Entry(key, value, this._mru++, len, now, maxAge)167168// oversized objects fall out of cache automatically.169if (hit.length > this._max) {170if (this._dispose) this._dispose(key, value)171return false172}173174this._length += hit.length175this._lruList[hit.lu] = this._cache[key] = hit176this._itemCount ++177178if (this._length > this._max)179trim(this)180181return true182}183184LRUCache.prototype.has = function (key) {185if (!hOP(this._cache, key)) return false186var hit = this._cache[key]187if (isStale(this, hit)) {188return false189}190return true191}192193LRUCache.prototype.get = function (key) {194return get(this, key, true)195}196197LRUCache.prototype.peek = function (key) {198return get(this, key, false)199}200201LRUCache.prototype.pop = function () {202var hit = this._lruList[this._lru]203del(this, hit)204return hit || null205}206207LRUCache.prototype.del = function (key) {208del(this, this._cache[key])209}210211function get (self, key, doUse) {212var hit = self._cache[key]213if (hit) {214if (isStale(self, hit)) {215del(self, hit)216if (!self._allowStale) hit = undefined217} else {218if (doUse) use(self, hit)219}220if (hit) hit = hit.value221}222return hit223}224225function isStale(self, hit) {226if (!hit || (!hit.maxAge && !self._maxAge)) return false227var stale = false;228var diff = Date.now() - hit.now229if (hit.maxAge) {230stale = diff > hit.maxAge231} else {232stale = self._maxAge && (diff > self._maxAge)233}234return stale;235}236237function use (self, hit) {238shiftLU(self, hit)239hit.lu = self._mru ++240self._lruList[hit.lu] = hit241}242243function trim (self) {244while (self._lru < self._mru && self._length > self._max)245del(self, self._lruList[self._lru])246}247248function shiftLU (self, hit) {249delete self._lruList[ hit.lu ]250while (self._lru < self._mru && !self._lruList[self._lru]) self._lru ++251}252253function del (self, hit) {254if (hit) {255if (self._dispose) self._dispose(hit.key, hit.value)256self._length -= hit.length257self._itemCount --258delete self._cache[ hit.key ]259shiftLU(self, hit)260}261}262263// classy, since V8 prefers predictable objects.264function Entry (key, value, lu, length, now, maxAge) {265this.key = key266this.value = value267this.lu = lu268this.length = length269this.now = now270if (maxAge) this.maxAge = maxAge271}272273})()274275276