Skip to content

Commit 6b7b715

Browse files
committed
docs: added js docs
1 parent b7c961c commit 6b7b715

1 file changed

Lines changed: 33 additions & 7 deletions

File tree

Cache/LRUCache.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ class LRUCache {
22
// LRU Cache to store a given capacity of data
33
#capacity
44

5+
/**
6+
* @param {number} capacity - the capacity of LRUCache
7+
* @returns {LRUCache} - sealed
8+
*/
59
constructor (capacity) {
610
if (!Number.isInteger(capacity) || capacity < 0) {
711
throw new TypeError('Invalid capacity')
@@ -11,6 +15,8 @@ class LRUCache {
1115
this.misses = 0
1216
this.hits = 0
1317
this.cache = new Map()
18+
19+
return Object.seal(this)
1420
}
1521

1622
get info () {
@@ -30,13 +36,6 @@ class LRUCache {
3036
return this.#capacity
3137
}
3238

33-
/**
34-
* delete oldest key existing in map by the help of iterator
35-
*/
36-
#removeLeastRecentlyUsed () {
37-
this.cache.delete(this.cache.keys().next().value)
38-
}
39-
4039
set capacity (newCapacity) {
4140
if (newCapacity < 0) {
4241
throw new RangeError('Capacity should be greater than 0')
@@ -53,12 +52,27 @@ class LRUCache {
5352
this.#capacity = newCapacity
5453
}
5554

55+
/**
56+
* delete oldest key existing in map by the help of iterator
57+
*/
58+
#removeLeastRecentlyUsed () {
59+
this.cache.delete(this.cache.keys().next().value)
60+
}
61+
62+
/**
63+
* @param {string} key
64+
* @returns {*}
65+
*/
5666
has (key) {
5767
key = String(key)
5868

5969
return this.cache.has(key)
6070
}
6171

72+
/**
73+
* @param {string} key
74+
* @param {*} value
75+
*/
6276
set (key, value) {
6377
key = String(key)
6478
// Sets the value for the input key and if the key exists it updates the existing key
@@ -69,6 +83,10 @@ class LRUCache {
6983
this.cache.set(key, value)
7084
}
7185

86+
/**
87+
* @param {string} key
88+
* @returns {*}
89+
*/
7290
get (key) {
7391
key = String(key)
7492
// Returns the value for the input key. Returns null if key is not present in cache
@@ -87,6 +105,10 @@ class LRUCache {
87105
return null
88106
}
89107

108+
/**
109+
* @param {JSON} json
110+
* @returns {LRUCache}
111+
*/
90112
parse (json) {
91113
const { misses, hits, cache } = JSON.parse(json)
92114

@@ -100,6 +122,10 @@ class LRUCache {
100122
return this
101123
}
102124

125+
/**
126+
* @param {number} indent
127+
* @returns {JSON} - string
128+
*/
103129
toString (indent) {
104130
const replacer = (_, value) => {
105131
if (value instanceof Set) {

0 commit comments

Comments
 (0)