Skip to content

Commit

Permalink
move getter/setters onto prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Nov 21, 2013
1 parent 8c2725a commit 806a067
Showing 1 changed file with 41 additions and 41 deletions.
82 changes: 41 additions & 41 deletions lib/lru-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,53 +49,53 @@ function LRUCache (options) {
this._lru = 0 // least recently used
this._length = 0 // number of items in the list
this._itemCount = 0
}

// resize the cache when the max changes.
Object.defineProperty(this, "max",
{ set : function (mL) {
if (!mL || !(typeof mL === "number") || mL <= 0 ) mL = Infinity
this._max = mL
if (this._length > this._max) trim(this)
}
, get : function () { return this._max }
, enumerable : true
})

// resize the cache when the lengthCalculator changes.
Object.defineProperty(this, "lengthCalculator",
{ set : function (lC) {
if (typeof lC !== "function") {
this._lengthCalculator = naiveLength
this._length = this._itemCount
for (var key in this._cache) {
this._cache[key].length = 1
}
} else {
this._lengthCalculator = lC
this._length = 0
for (var key in this._cache) {
this._cache[key].length = this._lengthCalculator(this._cache[key].value)
this._length += this._cache[key].length
}
// resize the cache when the max changes.
Object.defineProperty(LRUCache.prototype, "max",
{ set : function (mL) {
if (!mL || !(typeof mL === "number") || mL <= 0 ) mL = Infinity
this._max = mL
if (this._length > this._max) trim(this)
}
, get : function () { return this._max }
, enumerable : true
})

// resize the cache when the lengthCalculator changes.
Object.defineProperty(LRUCache.prototype, "lengthCalculator",
{ set : function (lC) {
if (typeof lC !== "function") {
this._lengthCalculator = naiveLength
this._length = this._itemCount
for (var key in this._cache) {
this._cache[key].length = 1
}
} else {
this._lengthCalculator = lC
this._length = 0
for (var key in this._cache) {
this._cache[key].length = this._lengthCalculator(this._cache[key].value)
this._length += this._cache[key].length
}

if (this._length > this._max) trim(this)
}
, get : function () { return this._lengthCalculator }
, enumerable : true
})

Object.defineProperty(this, "length",
{ get : function () { return this._length }
, enumerable : true
})
if (this._length > this._max) trim(this)
}
, get : function () { return this._lengthCalculator }
, enumerable : true
})

Object.defineProperty(LRUCache.prototype, "length",
{ get : function () { return this._length }
, enumerable : true
})


Object.defineProperty(this, "itemCount",
{ get : function () { return this._itemCount }
, enumerable : true
})
}
Object.defineProperty(LRUCache.prototype, "itemCount",
{ get : function () { return this._itemCount }
, enumerable : true
})

LRUCache.prototype.forEach = function (fn, thisp) {
thisp = thisp || this
Expand Down

0 comments on commit 806a067

Please sign in to comment.