UNPKG

@visulima/string

Version:

Functions for manipulating strings.

54 lines (51 loc) 1.21 kB
'use strict'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); class LRUCache { static { __name(this, "LRUCache"); } capacity; cache; keyOrder; constructor(capacity) { this.capacity = capacity; this.cache = /* @__PURE__ */ new Map(); this.keyOrder = []; } get(key) { if (!this.cache.has(key)) { return void 0; } this.keyOrder = this.keyOrder.filter((k) => k !== key); this.keyOrder.push(key); return this.cache.get(key); } has(key) { return this.cache.has(key); } set(key, value) { if (this.cache.has(key)) { this.keyOrder = this.keyOrder.filter((k) => k !== key); } else if (this.cache.size >= this.capacity) { const lruKey = this.keyOrder.shift(); if (lruKey !== void 0) { this.cache.delete(lruKey); } } this.cache.set(key, value); this.keyOrder.push(key); } delete(key) { this.cache.delete(key); this.keyOrder = this.keyOrder.filter((k) => k !== key); } clear() { this.cache.clear(); this.keyOrder = []; } size() { return this.cache.size; } } module.exports = LRUCache;