evicting-cache 3.0.3 → 3.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ ## [3.0.4](https://github.com/D1g1talEntr0py/evicting-cache/compare/v3.0.3...v3.0.4) (2026-03-16)
2
+
3
+ ### Bug Fixes
4
+
5
+ * **release:** updated invalid node version (7d2273211f13e5369698bfe2cb8d4455c5798c98)
6
+
7
+ ### Miscellaneous Chores
8
+
9
+ * fix license (62279e557ef3289fcdaf693abc9bcb6d9c9499c0)
10
+ * **publish:** remove /src folder from package (0d5fd5302b50ac7013cd7fee88d311a5dac2e389)
11
+
1
12
  ## [3.0.3](https://github.com/D1g1talEntr0py/evicting-cache/compare/v3.0.2...v3.0.3) (2026-03-16)
2
13
 
3
14
  ### Bug Fixes
package/LICENSE CHANGED
@@ -1,12 +1,7 @@
1
- Copyright (c) 2023, Jason DiMeo
1
+ ISC License
2
2
 
3
- Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided
4
- that the above copyright notice and this permission notice appear in all copies.
3
+ Copyright (c) 2023, Jason DiMeo
5
4
 
6
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
7
- INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
8
- FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
9
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
10
- ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
5
+ Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
11
6
 
12
- Source: http://opensource.org/licenses/ISC
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "evicting-cache",
3
3
  "author": "D1g1talEntr0py",
4
- "version": "3.0.3",
4
+ "version": "3.0.4",
5
5
  "license": "ISC",
6
6
  "description": "Cache implementation with an LRU evicting policy",
7
7
  "homepage": "https://github.com/D1g1talEntr0py/evicting-cache#readme",
@@ -34,7 +34,6 @@
34
34
  },
35
35
  "files": [
36
36
  "dist/",
37
- "src/",
38
37
  "README.md",
39
38
  "CHANGELOG.md",
40
39
  "LICENSE"
@@ -1,292 +0,0 @@
1
- type CacheStats = {
2
- hits: number;
3
- misses: number;
4
- hitRate: number;
5
- };
6
-
7
- /** JavaScript implementation of a Least Recently Used(LRU) Cache using a Map. */
8
- export class EvictingCache<K, V> {
9
- private readonly _capacity: number;
10
- private readonly cache: Map<K, V>;
11
- private hits = 0;
12
- private misses = 0;
13
-
14
- /**
15
- * Creates a new Evicting Cache with the given capacity.
16
- *
17
- * @param {number} [capacity=100] The maximum number of key-value pairs the cache can hold.
18
- */
19
- constructor(capacity: number = 100) {
20
- if (capacity < 1) { throw new RangeError('capacity must be greater than 0') }
21
- if (!Number.isInteger(capacity)) { throw new RangeError('capacity must be an integer') }
22
-
23
- this._capacity = capacity;
24
- this.cache = new Map();
25
- }
26
-
27
- /**
28
- * Returns the value associated with the given key from the cache and updates the LRU order.
29
- *
30
- * @param {K} key The key to get the value for.
31
- * @returns {V | null} The associated value if the key is in the cache, or null otherwise.
32
- */
33
- get(key: K): V | null {
34
- const value = this.cache.get(key);
35
- if (value === undefined) {
36
- this.misses++;
37
- return null;
38
- }
39
-
40
- this.hits++;
41
- this.cache.delete(key);
42
- // Move the accessed item to the end (most recently used)
43
- this.cache.set(key, value);
44
-
45
- return value;
46
- }
47
-
48
- /**
49
- * Returns true if the given key is in the cache, false otherwise.
50
- *
51
- * @param {K} key The key to check.
52
- * @returns {boolean} True if the key is in the cache, false otherwise.
53
- */
54
- has(key: K): boolean {
55
- return this.cache.has(key);
56
- }
57
-
58
- /**
59
- * Adds a new key-value pair to the cache and updates the LRU order.
60
- * If adding the new pair will exceed the capacity, removes the least recently used pair from the cache.
61
- *
62
- * @param {K} key The key to add.
63
- * @param {V} value The value to add.
64
- * @returns {void}
65
- */
66
- put(key: K, value: V): void {
67
- this.putAndEvict(key, value);
68
- }
69
-
70
- /**
71
- * Removes the specified key from the cache.
72
- *
73
- * @param {K} key The key to remove.
74
- * @returns {boolean} True if the key was in the cache and was removed, false otherwise.
75
- */
76
- delete(key: K): boolean {
77
- return this.cache.delete(key);
78
- }
79
-
80
- /**
81
- * Returns the value associated with the given key from the cache without updating the LRU order.
82
- *
83
- * @param {K} key The key to get the value for.
84
- * @returns {V | null} The associated value if the key is in the cache, or null otherwise.
85
- */
86
- peek(key: K): V | null {
87
- return this.cache.get(key) ?? null;
88
- }
89
-
90
- /**
91
- * Returns the value for the key if it exists in the cache. If not, put the key-value pair into the cache and return the value.
92
- * If the producer function throws an error, the cache state is not modified.
93
- *
94
- * @param {K} key The key.
95
- * @param {function(): V} producer The value to put if the key does not exist in the cache.
96
- * @returns {V} The value corresponding to the key.
97
- */
98
- getOrPut(key: K, producer: () => V): V {
99
- const existing = this.get(key);
100
- if (existing !== null) { return existing }
101
-
102
- // If producer throws, cache state remains unchanged
103
- const value = producer();
104
- return this.putAndEvict(key, value);
105
- }
106
-
107
- /**
108
- * Removes the least recently used key-value pair from the cache.
109
- * @returns {boolean} True if an item was removed, false otherwise.
110
- */
111
- evict(): boolean {
112
- const firstEntry = this.cache.keys().next();
113
- if (firstEntry.done) { return false }
114
-
115
- return this.cache.delete(firstEntry.value);
116
- }
117
-
118
- /**
119
- * Clears the cache and the LRU list.
120
- * @returns {void}
121
- */
122
- clear(): void {
123
- this.cache.clear();
124
- }
125
-
126
- /**
127
- * Executes a provided function once per each key/value pair in the cache, in insertion order.
128
- * @param {(value: V, key: K, cache: EvictingCache<K, V>) => void} callbackfn Function to execute for each element.
129
- * @param {unknown} [thisArg] Value to use as `this` when executing callback.
130
- * @returns {void}
131
- */
132
- forEach(callbackfn: (value: V, key: K, cache: EvictingCache<K, V>) => void, thisArg?: unknown): void {
133
- const boundCallback = thisArg !== undefined ? callbackfn.bind(thisArg) : callbackfn;
134
- this.cache.forEach((value, key) => boundCallback(value, key, this));
135
- }
136
-
137
- /**
138
- * Adds multiple key-value pairs to the cache.
139
- * Each pair is added individually, following the same LRU eviction rules as put().
140
- * @param {Iterable<[K, V]>} entries The entries to add.
141
- * @returns {void}
142
- */
143
- putAll(entries: Iterable<[K, V]>): void {
144
- for (const [key, value] of entries) { this.put(key, value) }
145
- }
146
-
147
- /**
148
- * Gets multiple values from the cache.
149
- * Each get updates the LRU order for that key.
150
- *
151
- * @param {Iterable<K>} keys The keys to get values for.
152
- * @returns {Map<K, V>} A map of keys to their values (excludes missing keys).
153
- */
154
- getAll(keys: Iterable<K>): Map<K, V> {
155
- const result = new Map<K, V>();
156
- for (const key of keys) {
157
- const value = this.get(key);
158
- if (value !== null) { result.set(key, value) }
159
- }
160
-
161
- return result;
162
- }
163
-
164
- /**
165
- * Removes multiple keys from the cache.
166
- *
167
- * @param {Iterable<K>} keys The keys to remove.
168
- * @returns {number} The number of keys that were removed.
169
- */
170
- deleteAll(keys: Iterable<K>): number {
171
- let count = 0;
172
- for (const key of keys) {
173
- if (this.cache.delete(key)) { count++ }
174
- }
175
-
176
- return count;
177
- }
178
-
179
- /**
180
- * Gets cache statistics including hit/miss counts and hit rate.
181
- *
182
- * @returns {CacheStats} Cache statistics.
183
- */
184
- getStats(): CacheStats {
185
- const total = this.hits + this.misses;
186
-
187
- return { hits: this.hits, misses: this.misses, hitRate: total === 0 ? 0 : this.hits / total };
188
- }
189
-
190
- /**
191
- * Resets cache statistics to zero.
192
- *
193
- * @returns {void}
194
- */
195
- resetStats(): void {
196
- this.hits = 0;
197
- this.misses = 0;
198
- }
199
-
200
- /**
201
- * Gets the capacity of the cache.
202
- * This is the maximum number of key-value pairs the cache can hold.
203
- * This is not the number of key-value pairs in the cache.
204
- *
205
- * @readonly
206
- * @returns {number} The capacity of the cache.
207
- */
208
- get capacity(): number {
209
- return this._capacity;
210
- }
211
-
212
- /**
213
- * Gets the size of the cache.
214
- * This is the number of key-value pairs in the cache.
215
- * This is not the capacity of the cache.
216
- * The capacity is the maximum number of key-value pairs the cache can hold.
217
- * The size is the number of key-value pairs currently in the cache.
218
- * The size will be less than or equal to the capacity.
219
- *
220
- * @returns {number} The size of the cache.
221
- */
222
- get size(): number {
223
- return this.cache.size;
224
- }
225
-
226
- /**
227
- * Returns an iterator over the keys in the cache.
228
- * The keys are returned in the order of least recently used to most recently used.
229
- *
230
- * @returns {IterableIterator<K>} An iterator over the keys in the cache.
231
- */
232
- keys(): IterableIterator<K> {
233
- return this.cache.keys();
234
- }
235
-
236
- /**
237
- * Returns an iterator over the values in the cache.
238
- * The values are returned in the order of least recently used to most recently used.
239
- *
240
- * @returns {IterableIterator<V>} An iterator over the values in the cache.
241
- */
242
- values(): IterableIterator<V> {
243
- return this.cache.values();
244
- }
245
-
246
- /**
247
- * Returns an iterator over the entries in the cache.
248
- * The entries are returned in the order of least recently used to most recently used.
249
- *
250
- * @returns {IterableIterator<[K, V]>} An iterator over the entries in the cache.
251
- */
252
- entries(): IterableIterator<[K, V]> {
253
- return this.cache.entries();
254
- }
255
-
256
- /**
257
- * Returns an iterator over the entries in the cache.
258
- * The entries are returned in the order of least recently used to most recently used.
259
- *
260
- * @returns {IterableIterator<[K, V]>} An iterator over the entries in the cache.
261
- */
262
- [Symbol.iterator](): IterableIterator<[K, V]> {
263
- return this.entries();
264
- }
265
-
266
- /**
267
- * Gets the description of the object.
268
- *
269
- * @override
270
- * @returns {string} The description of the object.
271
- */
272
- get [Symbol.toStringTag](): string {
273
- return 'EvictingCache';
274
- }
275
-
276
- /**
277
- * Puts a key-value pair into the cache and evicts the least recently used item if necessary.
278
- * If the key already exists, the item is removed and re-added to update its position.
279
- * If the cache is full, the least recently used item is evicted and the new item is added.
280
- * @param {K} key The key to put.
281
- * @param {V} value The value to put.
282
- * @returns {V} The value that was put.
283
- */
284
- private putAndEvict(key: K, value: V): V {
285
- const existed = this.cache.delete(key);
286
- if (!existed && this._capacity <= this.cache.size) { this.evict() }
287
-
288
- this.cache.set(key, value);
289
-
290
- return value;
291
- }
292
- }