index.d.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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;