rmapi-js 6.0.0 → 8.0.0

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/dist/lru.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ export declare class LruCache extends Map<string, string | null> {
2
+ #private;
3
+ constructor(maxSize: number, entries?: Iterable<[string, string | null]>);
4
+ get(key: string): string | null | undefined;
5
+ set(key: string, value: string | null): this;
6
+ delete(key: string): boolean;
7
+ clear(): void;
8
+ }
package/dist/lru.js ADDED
@@ -0,0 +1,64 @@
1
+ export class LruCache extends Map {
2
+ #maxSize;
3
+ #currentSize = 0;
4
+ constructor(maxSize, entries = []) {
5
+ super();
6
+ this.#maxSize = maxSize;
7
+ for (const [key, value] of entries) {
8
+ this.set(key, value);
9
+ }
10
+ }
11
+ get(key) {
12
+ const res = super.get(key);
13
+ if (res !== undefined) {
14
+ // update order so key is most recent
15
+ super.delete(key);
16
+ super.set(key, res);
17
+ }
18
+ return res;
19
+ }
20
+ set(key, value) {
21
+ const existing = super.get(key);
22
+ if (existing === undefined) {
23
+ this.#currentSize += key.length; // adding a new key
24
+ }
25
+ else if (existing !== null) {
26
+ this.#currentSize -= existing.length; // removing old value
27
+ }
28
+ if (value !== null) {
29
+ this.#currentSize += value.length;
30
+ }
31
+ // delete existing value
32
+ super.delete(key);
33
+ // evict down to desired size
34
+ let entry;
35
+ while (this.#currentSize > this.#maxSize &&
36
+ (entry = this.entries().next().value)) {
37
+ const [oldestKey, oldestValue] = entry;
38
+ super.delete(oldestKey);
39
+ this.#currentSize -= oldestKey.length;
40
+ if (oldestValue !== null) {
41
+ this.#currentSize -= oldestValue.length;
42
+ }
43
+ }
44
+ // finally insert new key and return
45
+ super.set(key, value);
46
+ return this;
47
+ }
48
+ delete(key) {
49
+ const value = super.get(key);
50
+ if (value === undefined) {
51
+ return false;
52
+ }
53
+ super.delete(key);
54
+ if (value !== null) {
55
+ this.#currentSize -= value.length;
56
+ }
57
+ this.#currentSize -= key.length;
58
+ return true;
59
+ }
60
+ clear() {
61
+ super.clear();
62
+ this.#currentSize = 0;
63
+ }
64
+ }