From fbe4ea60cb20675a0c087c57ee22fb6e9a20919e Mon Sep 17 00:00:00 2001 From: Okiki Ojo Date: Mon, 26 Jan 2026 16:30:43 -0500 Subject: [PATCH] chore: remove old lru code Signed-off-by: Okiki Ojo --- utils/lru.ts | 205 --------------------------------------------------- 1 file changed, 205 deletions(-) diff --git a/utils/lru.ts b/utils/lru.ts index ba23e3e..e038c24 100644 --- a/utils/lru.ts +++ b/utils/lru.ts @@ -1,206 +1 @@ -/** - * Based on https://deno.land/x/lru@1.0.2 - */ - -/** - * LRU main class - */ -export class LRU { - /** - * Capacity limit - */ - #max: number; - - /** - * LRU map String:any - */ - #cache: Map; - - /** - * 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 - */ - [Symbol.iterator]() { - const iterator = this.#cache[Symbol.iterator](); - return { - next: () => iterator.next(), - }; - } - - /** - * Amount of entries in the cache - * @returns current size of cache - */ - get size(): number { - return this.#cache.size; - } - - /** - * Get entries as object - * @returns Object with all entries - */ - get object(): { [key: string]: T } { - return Object.fromEntries(this.#cache); - } - - /** - * Get all values from cache - * @returns Array of all values - */ - get values(): Array { - return Array.from(this.#cache.values()); - } - - /** - * Get all keys from cache - * @returns Array of all keys - */ - get keys(): Array { - 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 - */ - forEach( - callbackfn: ( - value: T, - key: string, - map: Map, - ) => void, - thisArg?: unknown, - ): void { - 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 - */ - 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. - */ - 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. - */ - reduce( - callbackfn: ( - previousValue: [string, T], - currentValue: [string, T], - currentIndex: number, - array: [string, T][], - ) => [string, T], - initialValue?: [string, T], - ): [string, T]; - reduce( - callbackfn: ( - previousValue: U, - currentValue: [string, T], - currentIndex: number, - array: [string, T][], - ) => U, - initialValue: U, - ): U; - // deno-lint-ignore no-explicit-any - reduce(callbackfn: any, initialValue?: any): any { - return Array.from(this.#cache).reduce(callbackfn, initialValue); - } - - /** - * Removes all entries in the cache - */ - 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 - */ - 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 - */ - 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 - */ - 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 - */ - 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 - */ - #first() { - return this.#cache.keys().next().value; - } -} - -export default LRU; - export * from "@std/cache" -- knotmirror 0.0.1