index.d.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. declare namespace QuickLRU {
  2. interface Options {
  3. /**
  4. The maximum number of items before evicting the least recently used items.
  5. */
  6. readonly maxSize: number;
  7. }
  8. }
  9. declare class QuickLRU<KeyType extends unknown, ValueType extends unknown>
  10. implements Iterable<[KeyType, ValueType]> {
  11. /**
  12. The stored item count.
  13. */
  14. readonly size: number;
  15. /**
  16. Simple ["Least Recently Used" (LRU) cache](https://en.m.wikipedia.org/wiki/Cache_replacement_policies#Least_Recently_Used_.28LRU.29).
  17. The instance is [`iterable`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Iteration_protocols) so you can use it directly in a [`for…of`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/for...of) loop.
  18. @example
  19. ```
  20. import QuickLRU = require('quick-lru');
  21. const lru = new QuickLRU({maxSize: 1000});
  22. lru.set('🦄', '🌈');
  23. lru.has('🦄');
  24. //=> true
  25. lru.get('🦄');
  26. //=> '🌈'
  27. ```
  28. */
  29. constructor(options: QuickLRU.Options);
  30. [Symbol.iterator](): IterableIterator<[KeyType, ValueType]>;
  31. /**
  32. Set an item.
  33. @returns The list instance.
  34. */
  35. set(key: KeyType, value: ValueType): this;
  36. /**
  37. Get an item.
  38. @returns The stored item or `undefined`.
  39. */
  40. get(key: KeyType): ValueType | undefined;
  41. /**
  42. Check if an item exists.
  43. */
  44. has(key: KeyType): boolean;
  45. /**
  46. Get an item without marking it as recently used.
  47. @returns The stored item or `undefined`.
  48. */
  49. peek(key: KeyType): ValueType | undefined;
  50. /**
  51. Delete an item.
  52. @returns `true` if the item is removed or `false` if the item doesn't exist.
  53. */
  54. delete(key: KeyType): boolean;
  55. /**
  56. Delete all items.
  57. */
  58. clear(): void;
  59. /**
  60. Iterable for all the keys.
  61. */
  62. keys(): IterableIterator<KeyType>;
  63. /**
  64. Iterable for all the values.
  65. */
  66. values(): IterableIterator<ValueType>;
  67. }
  68. export = QuickLRU;