You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.0 KiB

2 years ago
  1. declare namespace QuickLRU {
  2. interface Options<KeyType, ValueType> {
  3. /**
  4. The maximum number of items before evicting the least recently used items.
  5. */
  6. readonly maxSize: number;
  7. /**
  8. Called right before an item is evicted from the cache.
  9. Useful for side effects or for items like object URLs that need explicit cleanup (`revokeObjectURL`).
  10. */
  11. onEviction?: (key: KeyType, value: ValueType) => void;
  12. }
  13. }
  14. declare class QuickLRU<KeyType, ValueType>
  15. implements Iterable<[KeyType, ValueType]> {
  16. /**
  17. The stored item count.
  18. */
  19. readonly size: number;
  20. /**
  21. Simple ["Least Recently Used" (LRU) cache](https://en.m.wikipedia.org/wiki/Cache_replacement_policies#Least_Recently_Used_.28LRU.29).
  22. 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.
  23. @example
  24. ```
  25. import QuickLRU = require('quick-lru');
  26. const lru = new QuickLRU({maxSize: 1000});
  27. lru.set('🦄', '🌈');
  28. lru.has('🦄');
  29. //=> true
  30. lru.get('🦄');
  31. //=> '🌈'
  32. ```
  33. */
  34. constructor(options: QuickLRU.Options<KeyType, ValueType>);
  35. [Symbol.iterator](): IterableIterator<[KeyType, ValueType]>;
  36. /**
  37. Set an item.
  38. @returns The list instance.
  39. */
  40. set(key: KeyType, value: ValueType): this;
  41. /**
  42. Get an item.
  43. @returns The stored item or `undefined`.
  44. */
  45. get(key: KeyType): ValueType | undefined;
  46. /**
  47. Check if an item exists.
  48. */
  49. has(key: KeyType): boolean;
  50. /**
  51. Get an item without marking it as recently used.
  52. @returns The stored item or `undefined`.
  53. */
  54. peek(key: KeyType): ValueType | undefined;
  55. /**
  56. Delete an item.
  57. @returns `true` if the item is removed or `false` if the item doesn't exist.
  58. */
  59. delete(key: KeyType): boolean;
  60. /**
  61. Delete all items.
  62. */
  63. clear(): void;
  64. /**
  65. Iterable for all the keys.
  66. */
  67. keys(): IterableIterator<KeyType>;
  68. /**
  69. Iterable for all the values.
  70. */
  71. values(): IterableIterator<ValueType>;
  72. }
  73. export = QuickLRU;