-
Notifications
You must be signed in to change notification settings - Fork 2
/
mod.ts
200 lines (182 loc) · 5.23 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* LRU main class
*/
export class LRU<T> {
/**
* Capacity limit
*/
#max: number;
/**
* LRU map String:any
*/
#cache: Map<string, T>;
/**
* Constructor method for a new LRU
* @param max Cache capacity
*/
constructor(max: number) {
this.#max = max;
this.#cache = new Map();
}
/**
* Use it for..of method
*/
public [Symbol.iterator]() {
const iterator = this.#cache[Symbol.iterator]();
return {
next: () => iterator.next(),
};
}
/**
* Amount of entries in the cache
* @returns current size of cache
*/
public get size(): number {
return this.#cache.size;
}
/**
* Get entries as object
* @returns Object with all entries
*/
public get object(): { [key: string]: T } {
return Object.fromEntries(this.#cache);
}
/**
* Get all values from cache
* @returns Array of all values
*/
public get values(): Array<T> {
return Array.from(this.#cache.values());
}
/**
* Get all keys from cache
* @returns Array of all keys
*/
public get keys(): Array<string> {
return Array.from(this.#cache.keys());
}
/**
* Executes a provided function once for each array element
* @param callbackfn Function to execute on each element. It accepts between one and three arguments
* @param thisArg Value to use as this when executing callback
*/
public forEach(
callbackfn: (
value: T,
key: string,
map: Map<string, T>,
) => void,
thisArg?: unknown,
): void {
return this.#cache.forEach(callbackfn, thisArg);
}
/**
* Method creates a new array populated with the results of calling a provided function on every element in the calling array
* @param callbackfn Function that is called for every element of arr. Each time callback executes, the returned value is added to new_array
* @param thisArg Value to use as this when executing callback
*/
public map(
callbackfn: (
value: [string, T],
index: number,
array: [string, T][],
) => T,
thisArg?: unknown,
): T[] {
return Array.from(this.#cache).map(callbackfn, thisArg);
}
/**
* Creates a new array with all elements that pass the test implemented by the provided function
* @param callbackfn Function is a predicate, to test each element of the array. Return true to keep the element, false otherwise
* @param thisArg Value to use as this when executing callback.
*/
public filter(
callbackfn: (
value: [string, T],
index: number,
array: [string, T][],
) => T,
thisArg?: unknown,
): [string, T][] {
return Array.from(this.#cache).filter(callbackfn, thisArg);
}
/**
* Executes a reducer function (that you provide) on each element of the array, resulting in single output value
* @param callbackfn A function to execute on each element in the array (except for the first, if no initialValue is supplied)
* @param initialValue A value to use as the first argument to the first call of the callback. If no initialValue is supplied, the first element in the array will be used as the initial accumulator value and skipped as currentValue. Calling reduce() on an empty array without an initialValue will throw a TypeError.
*/
public reduce(
callbackfn: (
previousValue: [string, T],
currentValue: [string, T],
currentIndex: number,
array: [string, T][],
) => [string, T],
initialValue?: [string, T],
): [string, T];
public reduce<U>(
callbackfn: (
previousValue: U,
currentValue: [string, T],
currentIndex: number,
array: [string, T][],
) => U,
initialValue: U,
): U;
// deno-lint-ignore no-explicit-any
public reduce(callbackfn: any, initialValue?: any): any {
return Array.from(this.#cache).reduce(callbackfn, initialValue);
}
/**
* Removes all entries in the cache
*/
public clear(): void {
this.#cache.clear();
}
/**
* Returns a boolean indicating whether an element with the specified key exists or not
* @param key The key of the element to test for presence in the cache
* @returns true if an element with the specified key exists in the cache otherwise false
*/
public has(key: string): boolean {
return this.#cache.has(key);
}
/**
* Attemps to retrieve a value from the cache
* @param key The key of the entry to return from the cache
* @returns the value associated to the input key or undefined
*/
public get(key: string): T | undefined {
const item = this.#cache.get(key);
if (!item) return undefined;
this.remove(key);
this.#cache.set(key, item);
return item;
}
/**
* Removes the entry
* @param key key is used to remove from the cache
*/
public remove(key: string): void {
this.#cache.delete(key);
}
/**
* Adds or updates an entry in the cache
*
* @param key new entry key
* @param value new entry value
*/
public set(key: string, value: T): void {
if (this.has(key)) this.remove(key);
else if (this.size === this.#max) this.remove(this._first());
this.#cache.set(key, value);
}
/**
* Get the oldest entry in the cache
* @returns value of the oldest entry
*/
private _first() {
return this.#cache.keys().next().value;
}
}
export default LRU;