@common.js/quick-lru 6.1.2 → 7.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +1 -1
  2. package/index.d.ts +69 -14
  3. package/index.js +428 -289
  4. package/package.json +9 -8
package/README.md CHANGED
@@ -2,4 +2,4 @@
2
2
 
3
3
  The [quick-lru](https://www.npmjs.com/package/quick-lru) package exported as CommonJS modules.
4
4
 
5
- Exported from [quick-lru@6.1.2](https://www.npmjs.com/package/quick-lru/v/6.1.2) using https://github.com/etienne-martin/common.js.
5
+ Exported from [quick-lru@7.3.0](https://www.npmjs.com/package/quick-lru/v/7.3.0) using https://github.com/etienne-martin/common.js.
package/index.d.ts CHANGED
@@ -1,35 +1,35 @@
1
- export interface Options<KeyType, ValueType> {
1
+ export type Options<KeyType, ValueType> = {
2
2
  /**
3
3
  The maximum number of milliseconds an item should remain in the cache.
4
4
 
5
5
  @default Infinity
6
6
 
7
7
  By default, `maxAge` will be `Infinity`, which means that items will never expire.
8
- Lazy expiration upon the next write or read call.
8
+ Lazy expiration occurs upon the next write or read call.
9
9
 
10
- Individual expiration of an item can be specified by the `set(key, value, maxAge)` method.
10
+ Individual expiration of an item can be specified with the `set(key, value, {maxAge})` method.
11
11
  */
12
12
  readonly maxAge?: number;
13
13
 
14
14
  /**
15
- The maximum number of items before evicting the least recently used items.
15
+ The target maximum number of items before evicting the least recently used items.
16
+
17
+ __Note:__ This package uses an [algorithm](https://github.com/sindresorhus/quick-lru#algorithm) which maintains between `maxSize` and `2 × maxSize` items for performance reasons. The cache may temporarily contain up to twice the specified size due to the dual-cache design that avoids expensive delete operations.
16
18
  */
17
19
  readonly maxSize: number;
18
20
 
19
21
  /**
20
- Called right before an item is evicted from the cache.
22
+ Called right before an item is evicted from the cache due to LRU pressure, TTL expiration, or manual eviction via `evict()`.
21
23
 
22
24
  Useful for side effects or for items like object URLs that need explicit cleanup (`revokeObjectURL`).
23
- */
24
- onEviction?: (key: KeyType, value: ValueType) => void;
25
- }
26
25
 
27
- export default class QuickLRU<KeyType, ValueType> extends Map implements Iterable<[KeyType, ValueType]> {
28
- /**
29
- The stored item count.
26
+ __Note:__ This callback is not called for manual removals via `delete()` or `clear()`. It fires for automatic evictions and manual evictions via `evict()`.
30
27
  */
31
- readonly size: number;
28
+ onEviction?: (key: KeyType, value: ValueType) => void;
29
+ };
32
30
 
31
+ // eslint-disable-next-line @typescript-eslint/naming-convention
32
+ export default class QuickLRU<KeyType, ValueType> extends Map<KeyType, ValueType> implements Iterable<[KeyType, ValueType]> {
33
33
  /**
34
34
  Simple ["Least Recently Used" (LRU) cache](https://en.m.wikipedia.org/wiki/Cache_replacement_policies#Least_Recently_Used_.28LRU.29).
35
35
 
@@ -57,9 +57,9 @@ export default class QuickLRU<KeyType, ValueType> extends Map implements Iterabl
57
57
  /**
58
58
  Set an item. Returns the instance.
59
59
 
60
- Individual expiration of an item can be specified with the `maxAge` option. If not specified, the global `maxAge` value will be used in case it is specified in the constructor, otherwise the item will never expire.
60
+ Individual expiration of an item can be specified with the `maxAge` option. If not specified, the global `maxAge` value will be used in case it is specified in the constructor; otherwise the item will never expire.
61
61
 
62
- @returns The list instance.
62
+ @returns The cache instance.
63
63
  */
64
64
  set(key: KeyType, value: ValueType, options?: {maxAge?: number}): this;
65
65
 
@@ -94,6 +94,18 @@ export default class QuickLRU<KeyType, ValueType> extends Map implements Iterabl
94
94
  */
95
95
  clear(): void;
96
96
 
97
+ /**
98
+ Get the remaining time to live (in milliseconds) for the given item, or `undefined` when the item is not in the cache.
99
+
100
+ - Does not mark the item as recently used.
101
+ - Does not trigger lazy expiration or remove the entry when it is expired.
102
+ - Returns `Infinity` if the item has no expiration.
103
+ - May return a negative number if the item is already expired but not yet lazily removed.
104
+
105
+ @returns Remaining time to live in milliseconds when set, `Infinity` when there is no expiration, or `undefined` when the item does not exist.
106
+ */
107
+ expiresIn(key: KeyType): number | undefined;
108
+
97
109
  /**
98
110
  Update the `maxSize` in-place, discarding items as necessary. Insertion order is mostly preserved, though this is not a strong guarantee.
99
111
 
@@ -101,6 +113,21 @@ export default class QuickLRU<KeyType, ValueType> extends Map implements Iterabl
101
113
  */
102
114
  resize(maxSize: number): void;
103
115
 
116
+ /**
117
+ The stored item count.
118
+ */
119
+ get size(): number;
120
+
121
+ /**
122
+ The set max size.
123
+ */
124
+ get maxSize(): number;
125
+
126
+ /**
127
+ The set max age.
128
+ */
129
+ get maxAge(): number;
130
+
104
131
  /**
105
132
  Iterable for all the keys.
106
133
  */
@@ -120,4 +147,32 @@ export default class QuickLRU<KeyType, ValueType> extends Map implements Iterabl
120
147
  Iterable for all entries, starting with the newest (descending in recency).
121
148
  */
122
149
  entriesDescending(): IterableIterator<[KeyType, ValueType]>;
150
+
151
+ /**
152
+ Evict the least recently used items from the cache.
153
+
154
+ @param count - The number of items to evict. Defaults to 1.
155
+
156
+ It will always keep at least one item in the cache.
157
+
158
+ @example
159
+ ```
160
+ import QuickLRU from 'quick-lru';
161
+
162
+ const lru = new QuickLRU({maxSize: 10});
163
+
164
+ lru.set('a', 1);
165
+ lru.set('b', 2);
166
+ lru.set('c', 3);
167
+
168
+ lru.evict(2); // Evicts 'a' and 'b'
169
+
170
+ console.log(lru.has('a'));
171
+ //=> false
172
+
173
+ console.log(lru.has('c'));
174
+ //=> true
175
+ ```
176
+ */
177
+ evict(count?: number): void;
123
178
  }
package/index.js CHANGED
@@ -25,11 +25,85 @@ function _assertThisInitialized(self) {
25
25
  }
26
26
  return self;
27
27
  }
28
+ function _checkPrivateRedeclaration(obj, privateCollection) {
29
+ if (privateCollection.has(obj)) {
30
+ throw new TypeError("Cannot initialize the same private elements twice on an object");
31
+ }
32
+ }
33
+ function _classApplyDescriptorGet(receiver, descriptor) {
34
+ if (descriptor.get) {
35
+ return descriptor.get.call(receiver);
36
+ }
37
+ return descriptor.value;
38
+ }
39
+ function _classApplyDescriptorSet(receiver, descriptor, value) {
40
+ if (descriptor.set) {
41
+ descriptor.set.call(receiver, value);
42
+ } else {
43
+ if (!descriptor.writable) {
44
+ throw new TypeError("attempted to set read only private field");
45
+ }
46
+ descriptor.value = value;
47
+ }
48
+ }
49
+ function _classApplyDescriptorUpdate(receiver, descriptor) {
50
+ if (descriptor.set) {
51
+ if (!("__destrWrapper" in descriptor)) {
52
+ descriptor.__destrWrapper = {
53
+ set value (v){
54
+ descriptor.set.call(receiver, v);
55
+ },
56
+ get value () {
57
+ return descriptor.get.call(receiver);
58
+ }
59
+ };
60
+ }
61
+ return descriptor.__destrWrapper;
62
+ } else {
63
+ if (!descriptor.writable) {
64
+ throw new TypeError("attempted to set read only private field");
65
+ }
66
+ return descriptor;
67
+ }
68
+ }
28
69
  function _classCallCheck(instance, Constructor) {
29
70
  if (!(instance instanceof Constructor)) {
30
71
  throw new TypeError("Cannot call a class as a function");
31
72
  }
32
73
  }
74
+ function _classExtractFieldDescriptor(receiver, privateMap, action) {
75
+ if (!privateMap.has(receiver)) {
76
+ throw new TypeError("attempted to " + action + " private field on non-instance");
77
+ }
78
+ return privateMap.get(receiver);
79
+ }
80
+ function _classPrivateFieldGet(receiver, privateMap) {
81
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get");
82
+ return _classApplyDescriptorGet(receiver, descriptor);
83
+ }
84
+ function _classPrivateFieldInit(obj, privateMap, value) {
85
+ _checkPrivateRedeclaration(obj, privateMap);
86
+ privateMap.set(obj, value);
87
+ }
88
+ function _classPrivateFieldSet(receiver, privateMap, value) {
89
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set");
90
+ _classApplyDescriptorSet(receiver, descriptor, value);
91
+ return value;
92
+ }
93
+ function _classPrivateFieldUpdate(receiver, privateMap) {
94
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "update");
95
+ return _classApplyDescriptorUpdate(receiver, descriptor);
96
+ }
97
+ function _classPrivateMethodGet(receiver, privateSet, fn) {
98
+ if (!privateSet.has(receiver)) {
99
+ throw new TypeError("attempted to get private field on non-instance");
100
+ }
101
+ return fn;
102
+ }
103
+ function _classPrivateMethodInit(obj, privateSet) {
104
+ _checkPrivateRedeclaration(obj, privateSet);
105
+ privateSet.add(obj);
106
+ }
33
107
  function isNativeReflectConstruct() {
34
108
  if (typeof Reflect === "undefined" || !Reflect.construct) return false;
35
109
  if (Reflect.construct.sham) return false;
@@ -226,10 +300,10 @@ var __generator = (void 0) && (void 0).__generator || function(thisArg, body) {
226
300
  return this;
227
301
  }), g;
228
302
  function verb(n) {
229
- return function(v) {
303
+ return function(v1) {
230
304
  return step([
231
305
  n,
232
- v
306
+ v1
233
307
  ]);
234
308
  };
235
309
  }
@@ -303,6 +377,8 @@ var __generator = (void 0) && (void 0).__generator || function(thisArg, body) {
303
377
  };
304
378
  }
305
379
  };
380
+ var _size = /*#__PURE__*/ new WeakMap(), _cache = /*#__PURE__*/ new WeakMap(), _oldCache = /*#__PURE__*/ new WeakMap(), _maxSize = /*#__PURE__*/ new WeakMap(), _maxAge = /*#__PURE__*/ new WeakMap(), _onEviction = /*#__PURE__*/ new WeakMap(), _emitEvictions = /*#__PURE__*/ new WeakSet(), _deleteIfExpired = /*#__PURE__*/ new WeakSet(), _getOrDeleteIfExpired = /*#__PURE__*/ new WeakSet(), _getItemValue = /*#__PURE__*/ new WeakSet(), _peek = /*#__PURE__*/ new WeakSet(), _set = /*#__PURE__*/ new WeakSet(), _moveToRecent = /*#__PURE__*/ new WeakSet(), _entriesAscending = /*#__PURE__*/ new WeakSet();
381
+ var _iterator = Symbol.iterator, _toStringTag = Symbol.toStringTag, tmp = Symbol.for("nodejs.util.inspect.custom");
306
382
  var QuickLRU = /*#__PURE__*/ function(Map1) {
307
383
  "use strict";
308
384
  _inherits(QuickLRU, Map1);
@@ -312,260 +388,68 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
312
388
  _classCallCheck(this, QuickLRU);
313
389
  var _this;
314
390
  _this = _super.call(this);
391
+ _classPrivateMethodInit(_assertThisInitialized(_this), _emitEvictions);
392
+ _classPrivateMethodInit(_assertThisInitialized(_this), _deleteIfExpired);
393
+ _classPrivateMethodInit(_assertThisInitialized(_this), _getOrDeleteIfExpired);
394
+ _classPrivateMethodInit(_assertThisInitialized(_this), _getItemValue);
395
+ _classPrivateMethodInit(_assertThisInitialized(_this), _peek);
396
+ _classPrivateMethodInit(_assertThisInitialized(_this), _set);
397
+ _classPrivateMethodInit(_assertThisInitialized(_this), _moveToRecent);
398
+ _classPrivateMethodInit(_assertThisInitialized(_this), _entriesAscending);
399
+ _classPrivateFieldInit(_assertThisInitialized(_this), _size, {
400
+ writable: true,
401
+ value: 0
402
+ });
403
+ _classPrivateFieldInit(_assertThisInitialized(_this), _cache, {
404
+ writable: true,
405
+ value: new Map()
406
+ });
407
+ _classPrivateFieldInit(_assertThisInitialized(_this), _oldCache, {
408
+ writable: true,
409
+ value: new Map()
410
+ });
411
+ _classPrivateFieldInit(_assertThisInitialized(_this), _maxSize, {
412
+ writable: true,
413
+ value: void 0
414
+ });
415
+ _classPrivateFieldInit(_assertThisInitialized(_this), _maxAge, {
416
+ writable: true,
417
+ value: void 0
418
+ });
419
+ _classPrivateFieldInit(_assertThisInitialized(_this), _onEviction, {
420
+ writable: true,
421
+ value: void 0
422
+ });
315
423
  if (!(options.maxSize && options.maxSize > 0)) {
316
424
  throw new TypeError("`maxSize` must be a number greater than 0");
317
425
  }
318
426
  if (typeof options.maxAge === "number" && options.maxAge === 0) {
319
427
  throw new TypeError("`maxAge` must be a number greater than 0");
320
428
  }
321
- // TODO: Use private class fields when ESLint supports them.
322
- _this.maxSize = options.maxSize;
323
- _this.maxAge = options.maxAge || Number.POSITIVE_INFINITY;
324
- _this.onEviction = options.onEviction;
325
- _this.cache = new Map();
326
- _this.oldCache = new Map();
327
- _this._size = 0;
429
+ _classPrivateFieldSet(_assertThisInitialized(_this), _maxSize, options.maxSize);
430
+ _classPrivateFieldSet(_assertThisInitialized(_this), _maxAge, options.maxAge || Number.POSITIVE_INFINITY);
431
+ _classPrivateFieldSet(_assertThisInitialized(_this), _onEviction, options.onEviction);
328
432
  return _this;
329
433
  }
330
434
  _createClass(QuickLRU, [
331
435
  {
332
- // TODO: Use private class methods when targeting Node.js 16.
333
- key: "_emitEvictions",
334
- value: function _emitEvictions(cache) {
335
- if (typeof this.onEviction !== "function") {
336
- return;
337
- }
338
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
339
- try {
340
- for(var _iterator = cache[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
341
- var _value = _slicedToArray(_step.value, 2), key = _value[0], item = _value[1];
342
- this.onEviction(key, item.value);
343
- }
344
- } catch (err) {
345
- _didIteratorError = true;
346
- _iteratorError = err;
347
- } finally{
348
- try {
349
- if (!_iteratorNormalCompletion && _iterator.return != null) {
350
- _iterator.return();
351
- }
352
- } finally{
353
- if (_didIteratorError) {
354
- throw _iteratorError;
355
- }
356
- }
357
- }
358
- }
359
- },
360
- {
361
- key: "_deleteIfExpired",
362
- value: function _deleteIfExpired(key, item) {
363
- if (typeof item.expiry === "number" && item.expiry <= Date.now()) {
364
- if (typeof this.onEviction === "function") {
365
- this.onEviction(key, item.value);
366
- }
367
- return this.delete(key);
368
- }
369
- return false;
370
- }
371
- },
372
- {
373
- key: "_getOrDeleteIfExpired",
374
- value: function _getOrDeleteIfExpired(key, item) {
375
- var deleted = this._deleteIfExpired(key, item);
376
- if (deleted === false) {
377
- return item.value;
378
- }
379
- }
380
- },
381
- {
382
- key: "_getItemValue",
383
- value: function _getItemValue(key, item) {
384
- return item.expiry ? this._getOrDeleteIfExpired(key, item) : item.value;
385
- }
386
- },
387
- {
388
- key: "_peek",
389
- value: function _peek(key, cache) {
390
- var item = cache.get(key);
391
- return this._getItemValue(key, item);
392
- }
393
- },
394
- {
395
- key: "_set",
396
- value: function _set(key, value) {
397
- this.cache.set(key, value);
398
- this._size++;
399
- if (this._size >= this.maxSize) {
400
- this._size = 0;
401
- this._emitEvictions(this.oldCache);
402
- this.oldCache = this.cache;
403
- this.cache = new Map();
404
- }
405
- }
406
- },
407
- {
408
- key: "_moveToRecent",
409
- value: function _moveToRecent(key, item) {
410
- this.oldCache.delete(key);
411
- this._set(key, item);
412
- }
413
- },
414
- {
415
- key: "_entriesAscending",
416
- value: function _entriesAscending() {
417
- var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, item, _item, key, value, deleted, err, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, item1, _item1, key1, value1, deleted1, err;
418
- return __generator(this, function(_state) {
419
- switch(_state.label){
420
- case 0:
421
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
422
- _state.label = 1;
423
- case 1:
424
- _state.trys.push([
425
- 1,
426
- 6,
427
- 7,
428
- 8
429
- ]);
430
- _iterator = this.oldCache[Symbol.iterator]();
431
- _state.label = 2;
432
- case 2:
433
- if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
434
- 3,
435
- 5
436
- ];
437
- item = _step.value;
438
- _item = _slicedToArray(item, 2), key = _item[0], value = _item[1];
439
- if (!!this.cache.has(key)) return [
440
- 3,
441
- 4
442
- ];
443
- deleted = this._deleteIfExpired(key, value);
444
- if (!(deleted === false)) return [
445
- 3,
446
- 4
447
- ];
448
- return [
449
- 4,
450
- item
451
- ];
452
- case 3:
453
- _state.sent();
454
- _state.label = 4;
455
- case 4:
456
- _iteratorNormalCompletion = true;
457
- return [
458
- 3,
459
- 2
460
- ];
461
- case 5:
462
- return [
463
- 3,
464
- 8
465
- ];
466
- case 6:
467
- err = _state.sent();
468
- _didIteratorError = true;
469
- _iteratorError = err;
470
- return [
471
- 3,
472
- 8
473
- ];
474
- case 7:
475
- try {
476
- if (!_iteratorNormalCompletion && _iterator.return != null) {
477
- _iterator.return();
478
- }
479
- } finally{
480
- if (_didIteratorError) {
481
- throw _iteratorError;
482
- }
483
- }
484
- return [
485
- 7
486
- ];
487
- case 8:
488
- _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
489
- _state.label = 9;
490
- case 9:
491
- _state.trys.push([
492
- 9,
493
- 14,
494
- 15,
495
- 16
496
- ]);
497
- _iterator1 = this.cache[Symbol.iterator]();
498
- _state.label = 10;
499
- case 10:
500
- if (!!(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done)) return [
501
- 3,
502
- 13
503
- ];
504
- item1 = _step1.value;
505
- _item1 = _slicedToArray(item1, 2), key1 = _item1[0], value1 = _item1[1];
506
- deleted1 = this._deleteIfExpired(key1, value1);
507
- if (!(deleted1 === false)) return [
508
- 3,
509
- 12
510
- ];
511
- return [
512
- 4,
513
- item1
514
- ];
515
- case 11:
516
- _state.sent();
517
- _state.label = 12;
518
- case 12:
519
- _iteratorNormalCompletion1 = true;
520
- return [
521
- 3,
522
- 10
523
- ];
524
- case 13:
525
- return [
526
- 3,
527
- 16
528
- ];
529
- case 14:
530
- err = _state.sent();
531
- _didIteratorError1 = true;
532
- _iteratorError1 = err;
533
- return [
534
- 3,
535
- 16
536
- ];
537
- case 15:
538
- try {
539
- if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
540
- _iterator1.return();
541
- }
542
- } finally{
543
- if (_didIteratorError1) {
544
- throw _iteratorError1;
545
- }
546
- }
547
- return [
548
- 7
549
- ];
550
- case 16:
551
- return [
552
- 2
553
- ];
554
- }
555
- });
436
+ key: "__oldCache",
437
+ get: // For tests.
438
+ function get() {
439
+ return _classPrivateFieldGet(this, _oldCache);
556
440
  }
557
441
  },
558
442
  {
559
443
  key: "get",
560
444
  value: function get(key) {
561
- if (this.cache.has(key)) {
562
- var item = this.cache.get(key);
563
- return this._getItemValue(key, item);
445
+ if (_classPrivateFieldGet(this, _cache).has(key)) {
446
+ var item = _classPrivateFieldGet(this, _cache).get(key);
447
+ return _classPrivateMethodGet(this, _getItemValue, getItemValue).call(this, key, item);
564
448
  }
565
- if (this.oldCache.has(key)) {
566
- var item1 = this.oldCache.get(key);
567
- if (this._deleteIfExpired(key, item1) === false) {
568
- this._moveToRecent(key, item1);
449
+ if (_classPrivateFieldGet(this, _oldCache).has(key)) {
450
+ var item1 = _classPrivateFieldGet(this, _oldCache).get(key);
451
+ if (_classPrivateMethodGet(this, _deleteIfExpired, deleteIfExpired).call(this, key, item1) === false) {
452
+ _classPrivateMethodGet(this, _moveToRecent, moveToRecent).call(this, key, item1);
569
453
  return item1.value;
570
454
  }
571
455
  }
@@ -573,16 +457,16 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
573
457
  },
574
458
  {
575
459
  key: "set",
576
- value: function set(key, value) {
577
- var ref = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {}, _maxAge = ref.maxAge, maxAge = _maxAge === void 0 ? this.maxAge : _maxAge;
460
+ value: function set1(key, value) {
461
+ var ref = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {}, _maxAge1 = ref.maxAge, maxAge = _maxAge1 === void 0 ? _classPrivateFieldGet(this, _maxAge) : _maxAge1;
578
462
  var expiry = typeof maxAge === "number" && maxAge !== Number.POSITIVE_INFINITY ? Date.now() + maxAge : undefined;
579
- if (this.cache.has(key)) {
580
- this.cache.set(key, {
463
+ if (_classPrivateFieldGet(this, _cache).has(key)) {
464
+ _classPrivateFieldGet(this, _cache).set(key, {
581
465
  value: value,
582
466
  expiry: expiry
583
467
  });
584
468
  } else {
585
- this._set(key, {
469
+ _classPrivateMethodGet(this, _set, set).call(this, key, {
586
470
  value: value,
587
471
  expiry: expiry
588
472
  });
@@ -593,42 +477,52 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
593
477
  {
594
478
  key: "has",
595
479
  value: function has(key) {
596
- if (this.cache.has(key)) {
597
- return !this._deleteIfExpired(key, this.cache.get(key));
480
+ if (_classPrivateFieldGet(this, _cache).has(key)) {
481
+ return !_classPrivateMethodGet(this, _deleteIfExpired, deleteIfExpired).call(this, key, _classPrivateFieldGet(this, _cache).get(key));
598
482
  }
599
- if (this.oldCache.has(key)) {
600
- return !this._deleteIfExpired(key, this.oldCache.get(key));
483
+ if (_classPrivateFieldGet(this, _oldCache).has(key)) {
484
+ return !_classPrivateMethodGet(this, _deleteIfExpired, deleteIfExpired).call(this, key, _classPrivateFieldGet(this, _oldCache).get(key));
601
485
  }
602
486
  return false;
603
487
  }
604
488
  },
605
489
  {
606
490
  key: "peek",
607
- value: function peek(key) {
608
- if (this.cache.has(key)) {
609
- return this._peek(key, this.cache);
491
+ value: function peek1(key) {
492
+ if (_classPrivateFieldGet(this, _cache).has(key)) {
493
+ return _classPrivateMethodGet(this, _peek, peek).call(this, key, _classPrivateFieldGet(this, _cache));
610
494
  }
611
- if (this.oldCache.has(key)) {
612
- return this._peek(key, this.oldCache);
495
+ if (_classPrivateFieldGet(this, _oldCache).has(key)) {
496
+ return _classPrivateMethodGet(this, _peek, peek).call(this, key, _classPrivateFieldGet(this, _oldCache));
497
+ }
498
+ }
499
+ },
500
+ {
501
+ key: "expiresIn",
502
+ value: function expiresIn(key) {
503
+ var ref;
504
+ var item = (ref = _classPrivateFieldGet(this, _cache).get(key)) !== null && ref !== void 0 ? ref : _classPrivateFieldGet(this, _oldCache).get(key);
505
+ if (item) {
506
+ return item.expiry ? item.expiry - Date.now() : Number.POSITIVE_INFINITY;
613
507
  }
614
508
  }
615
509
  },
616
510
  {
617
511
  key: "delete",
618
512
  value: function _delete(key) {
619
- var deleted = this.cache.delete(key);
513
+ var deleted = _classPrivateFieldGet(this, _cache).delete(key);
620
514
  if (deleted) {
621
- this._size--;
515
+ _classPrivateFieldUpdate(this, _size).value--;
622
516
  }
623
- return this.oldCache.delete(key) || deleted;
517
+ return _classPrivateFieldGet(this, _oldCache).delete(key) || deleted;
624
518
  }
625
519
  },
626
520
  {
627
521
  key: "clear",
628
522
  value: function clear() {
629
- this.cache.clear();
630
- this.oldCache.clear();
631
- this._size = 0;
523
+ _classPrivateFieldGet(this, _cache).clear();
524
+ _classPrivateFieldGet(this, _oldCache).clear();
525
+ _classPrivateFieldSet(this, _size, 0);
632
526
  }
633
527
  },
634
528
  {
@@ -637,21 +531,40 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
637
531
  if (!(newSize && newSize > 0)) {
638
532
  throw new TypeError("`maxSize` must be a number greater than 0");
639
533
  }
640
- var items = _toConsumableArray(this._entriesAscending());
534
+ var items = _toConsumableArray(_classPrivateMethodGet(this, _entriesAscending, entriesAscending).call(this));
641
535
  var removeCount = items.length - newSize;
642
536
  if (removeCount < 0) {
643
- this.cache = new Map(items);
644
- this.oldCache = new Map();
645
- this._size = items.length;
537
+ _classPrivateFieldSet(this, _cache, new Map(items));
538
+ _classPrivateFieldSet(this, _oldCache, new Map());
539
+ _classPrivateFieldSet(this, _size, items.length);
646
540
  } else {
647
541
  if (removeCount > 0) {
648
- this._emitEvictions(items.slice(0, removeCount));
542
+ _classPrivateMethodGet(this, _emitEvictions, emitEvictions).call(this, items.slice(0, removeCount));
649
543
  }
650
- this.oldCache = new Map(items.slice(removeCount));
651
- this.cache = new Map();
652
- this._size = 0;
544
+ _classPrivateFieldSet(this, _oldCache, new Map(items.slice(removeCount)));
545
+ _classPrivateFieldSet(this, _cache, new Map());
546
+ _classPrivateFieldSet(this, _size, 0);
547
+ }
548
+ _classPrivateFieldSet(this, _maxSize, newSize);
549
+ }
550
+ },
551
+ {
552
+ key: "evict",
553
+ value: function evict() {
554
+ var count = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : 1;
555
+ var requested = Number(count);
556
+ if (!requested || requested <= 0) {
557
+ return;
558
+ }
559
+ var items = _toConsumableArray(_classPrivateMethodGet(this, _entriesAscending, entriesAscending).call(this));
560
+ var evictCount = Math.trunc(Math.min(requested, Math.max(items.length - 1, 0)));
561
+ if (evictCount <= 0) {
562
+ return;
653
563
  }
654
- this.maxSize = newSize;
564
+ _classPrivateMethodGet(this, _emitEvictions, emitEvictions).call(this, items.slice(0, evictCount));
565
+ _classPrivateFieldSet(this, _oldCache, new Map(items.slice(evictCount)));
566
+ _classPrivateFieldSet(this, _cache, new Map());
567
+ _classPrivateFieldSet(this, _size, 0);
655
568
  }
656
569
  },
657
570
  {
@@ -797,7 +710,7 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
797
710
  }
798
711
  },
799
712
  {
800
- key: Symbol.iterator,
713
+ key: _iterator,
801
714
  value: function value() {
802
715
  var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, item, _item, key, value1, deleted, err, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, item1, _item1, key1, value2, deleted1, err;
803
716
  return __generator(this, function(_state) {
@@ -812,7 +725,7 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
812
725
  7,
813
726
  8
814
727
  ]);
815
- _iterator = this.cache[Symbol.iterator]();
728
+ _iterator = _classPrivateFieldGet(this, _cache)[Symbol.iterator]();
816
729
  _state.label = 2;
817
730
  case 2:
818
731
  if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
@@ -821,7 +734,7 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
821
734
  ];
822
735
  item = _step.value;
823
736
  _item = _slicedToArray(item, 2), key = _item[0], value1 = _item[1];
824
- deleted = this._deleteIfExpired(key, value1);
737
+ deleted = _classPrivateMethodGet(this, _deleteIfExpired, deleteIfExpired).call(this, key, value1);
825
738
  if (!(deleted === false)) return [
826
739
  3,
827
740
  4
@@ -878,7 +791,7 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
878
791
  15,
879
792
  16
880
793
  ]);
881
- _iterator1 = this.oldCache[Symbol.iterator]();
794
+ _iterator1 = _classPrivateFieldGet(this, _oldCache)[Symbol.iterator]();
882
795
  _state.label = 10;
883
796
  case 10:
884
797
  if (!!(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done)) return [
@@ -887,11 +800,11 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
887
800
  ];
888
801
  item1 = _step1.value;
889
802
  _item1 = _slicedToArray(item1, 2), key1 = _item1[0], value2 = _item1[1];
890
- if (!!this.cache.has(key1)) return [
803
+ if (!!_classPrivateFieldGet(this, _cache).has(key1)) return [
891
804
  3,
892
805
  12
893
806
  ];
894
- deleted1 = this._deleteIfExpired(key1, value2);
807
+ deleted1 = _classPrivateMethodGet(this, _deleteIfExpired, deleteIfExpired).call(this, key1, value2);
895
808
  if (!(deleted1 === false)) return [
896
809
  3,
897
810
  12
@@ -953,7 +866,7 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
953
866
  return __generator(this, function(_state) {
954
867
  switch(_state.label){
955
868
  case 0:
956
- items = _toConsumableArray(this.cache);
869
+ items = _toConsumableArray(_classPrivateFieldGet(this, _cache));
957
870
  i = items.length - 1;
958
871
  _state.label = 1;
959
872
  case 1:
@@ -963,7 +876,7 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
963
876
  ];
964
877
  item = items[i];
965
878
  _item = _slicedToArray(item, 2), key = _item[0], value = _item[1];
966
- deleted = this._deleteIfExpired(key, value);
879
+ deleted = _classPrivateMethodGet(this, _deleteIfExpired, deleteIfExpired).call(this, key, value);
967
880
  if (!(deleted === false)) return [
968
881
  3,
969
882
  3
@@ -985,7 +898,7 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
985
898
  1
986
899
  ];
987
900
  case 4:
988
- items = _toConsumableArray(this.oldCache);
901
+ items = _toConsumableArray(_classPrivateFieldGet(this, _oldCache));
989
902
  i1 = items.length - 1;
990
903
  _state.label = 5;
991
904
  case 5:
@@ -995,11 +908,11 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
995
908
  ];
996
909
  item1 = items[i1];
997
910
  _item1 = _slicedToArray(item1, 2), key1 = _item1[0], value1 = _item1[1];
998
- if (!!this.cache.has(key1)) return [
911
+ if (!!_classPrivateFieldGet(this, _cache).has(key1)) return [
999
912
  3,
1000
913
  7
1001
914
  ];
1002
- deleted1 = this._deleteIfExpired(key1, value1);
915
+ deleted1 = _classPrivateMethodGet(this, _deleteIfExpired, deleteIfExpired).call(this, key1, value1);
1003
916
  if (!(deleted1 === false)) return [
1004
917
  3,
1005
918
  7
@@ -1030,7 +943,7 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
1030
943
  },
1031
944
  {
1032
945
  key: "entriesAscending",
1033
- value: function entriesAscending() {
946
+ value: function entriesAscending1() {
1034
947
  var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, key, value, err;
1035
948
  return __generator(this, function(_state) {
1036
949
  switch(_state.label){
@@ -1044,7 +957,7 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
1044
957
  7,
1045
958
  8
1046
959
  ]);
1047
- _iterator = this._entriesAscending()[Symbol.iterator]();
960
+ _iterator = _classPrivateMethodGet(this, _entriesAscending, entriesAscending).call(this)[Symbol.iterator]();
1048
961
  _state.label = 2;
1049
962
  case 2:
1050
963
  if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
@@ -1105,15 +1018,15 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
1105
1018
  {
1106
1019
  key: "size",
1107
1020
  get: function get() {
1108
- if (!this._size) {
1109
- return this.oldCache.size;
1021
+ if (!_classPrivateFieldGet(this, _size)) {
1022
+ return _classPrivateFieldGet(this, _oldCache).size;
1110
1023
  }
1111
1024
  var oldCacheSize = 0;
1112
1025
  var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1113
1026
  try {
1114
- for(var _iterator = this.oldCache.keys()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1027
+ for(var _iterator = _classPrivateFieldGet(this, _oldCache).keys()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1115
1028
  var key = _step.value;
1116
- if (!this.cache.has(key)) {
1029
+ if (!_classPrivateFieldGet(this, _cache).has(key)) {
1117
1030
  oldCacheSize++;
1118
1031
  }
1119
1032
  }
@@ -1131,7 +1044,19 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
1131
1044
  }
1132
1045
  }
1133
1046
  }
1134
- return Math.min(this._size + oldCacheSize, this.maxSize);
1047
+ return Math.min(_classPrivateFieldGet(this, _size) + oldCacheSize, _classPrivateFieldGet(this, _maxSize));
1048
+ }
1049
+ },
1050
+ {
1051
+ key: "maxSize",
1052
+ get: function get() {
1053
+ return _classPrivateFieldGet(this, _maxSize);
1054
+ }
1055
+ },
1056
+ {
1057
+ key: "maxAge",
1058
+ get: function get() {
1059
+ return _classPrivateFieldGet(this, _maxAge);
1135
1060
  }
1136
1061
  },
1137
1062
  {
@@ -1167,11 +1092,225 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
1167
1092
  }
1168
1093
  },
1169
1094
  {
1170
- key: Symbol.toStringTag,
1095
+ key: _toStringTag,
1171
1096
  get: function get() {
1172
- return JSON.stringify(_toConsumableArray(this.entriesAscending()));
1097
+ return "QuickLRU";
1098
+ }
1099
+ },
1100
+ {
1101
+ key: "toString",
1102
+ value: function toString() {
1103
+ return "QuickLRU(".concat(this.size, "/").concat(this.maxSize, ")");
1104
+ }
1105
+ },
1106
+ {
1107
+ key: tmp,
1108
+ value: function value() {
1109
+ return this.toString();
1173
1110
  }
1174
1111
  }
1175
1112
  ]);
1176
1113
  return QuickLRU;
1177
1114
  }(_wrapNativeSuper(Map));
1115
+ function emitEvictions(cache) {
1116
+ if (typeof _classPrivateFieldGet(this, _onEviction) !== "function") {
1117
+ return;
1118
+ }
1119
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1120
+ try {
1121
+ for(var _iterator = cache[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1122
+ var _value = _slicedToArray(_step.value, 2), key = _value[0], item = _value[1];
1123
+ _classPrivateFieldGet(this, _onEviction).call(this, key, item.value);
1124
+ }
1125
+ } catch (err) {
1126
+ _didIteratorError = true;
1127
+ _iteratorError = err;
1128
+ } finally{
1129
+ try {
1130
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1131
+ _iterator.return();
1132
+ }
1133
+ } finally{
1134
+ if (_didIteratorError) {
1135
+ throw _iteratorError;
1136
+ }
1137
+ }
1138
+ }
1139
+ }
1140
+ function deleteIfExpired(key, item) {
1141
+ if (typeof item.expiry === "number" && item.expiry <= Date.now()) {
1142
+ if (typeof _classPrivateFieldGet(this, _onEviction) === "function") {
1143
+ _classPrivateFieldGet(this, _onEviction).call(this, key, item.value);
1144
+ }
1145
+ return this.delete(key);
1146
+ }
1147
+ return false;
1148
+ }
1149
+ function getOrDeleteIfExpired(key, item) {
1150
+ var deleted = _classPrivateMethodGet(this, _deleteIfExpired, deleteIfExpired).call(this, key, item);
1151
+ if (deleted === false) {
1152
+ return item.value;
1153
+ }
1154
+ }
1155
+ function getItemValue(key, item) {
1156
+ return item.expiry ? _classPrivateMethodGet(this, _getOrDeleteIfExpired, getOrDeleteIfExpired).call(this, key, item) : item.value;
1157
+ }
1158
+ function peek(key, cache) {
1159
+ var item = cache.get(key);
1160
+ return _classPrivateMethodGet(this, _getItemValue, getItemValue).call(this, key, item);
1161
+ }
1162
+ function set(key, value) {
1163
+ _classPrivateFieldGet(this, _cache).set(key, value);
1164
+ _classPrivateFieldUpdate(this, _size).value++;
1165
+ if (_classPrivateFieldGet(this, _size) >= _classPrivateFieldGet(this, _maxSize)) {
1166
+ _classPrivateFieldSet(this, _size, 0);
1167
+ _classPrivateMethodGet(this, _emitEvictions, emitEvictions).call(this, _classPrivateFieldGet(this, _oldCache));
1168
+ _classPrivateFieldSet(this, _oldCache, _classPrivateFieldGet(this, _cache));
1169
+ _classPrivateFieldSet(this, _cache, new Map());
1170
+ }
1171
+ }
1172
+ function moveToRecent(key, item) {
1173
+ _classPrivateFieldGet(this, _oldCache).delete(key);
1174
+ _classPrivateMethodGet(this, _set, set).call(this, key, item);
1175
+ }
1176
+ function entriesAscending() {
1177
+ var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, item, _item, key, value, deleted, err, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, item1, _item1, key1, value1, deleted1, err;
1178
+ return __generator(this, function(_state) {
1179
+ switch(_state.label){
1180
+ case 0:
1181
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1182
+ _state.label = 1;
1183
+ case 1:
1184
+ _state.trys.push([
1185
+ 1,
1186
+ 6,
1187
+ 7,
1188
+ 8
1189
+ ]);
1190
+ _iterator = _classPrivateFieldGet(this, _oldCache)[Symbol.iterator]();
1191
+ _state.label = 2;
1192
+ case 2:
1193
+ if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
1194
+ 3,
1195
+ 5
1196
+ ];
1197
+ item = _step.value;
1198
+ _item = _slicedToArray(item, 2), key = _item[0], value = _item[1];
1199
+ if (!!_classPrivateFieldGet(this, _cache).has(key)) return [
1200
+ 3,
1201
+ 4
1202
+ ];
1203
+ deleted = _classPrivateMethodGet(this, _deleteIfExpired, deleteIfExpired).call(this, key, value);
1204
+ if (!(deleted === false)) return [
1205
+ 3,
1206
+ 4
1207
+ ];
1208
+ return [
1209
+ 4,
1210
+ item
1211
+ ];
1212
+ case 3:
1213
+ _state.sent();
1214
+ _state.label = 4;
1215
+ case 4:
1216
+ _iteratorNormalCompletion = true;
1217
+ return [
1218
+ 3,
1219
+ 2
1220
+ ];
1221
+ case 5:
1222
+ return [
1223
+ 3,
1224
+ 8
1225
+ ];
1226
+ case 6:
1227
+ err = _state.sent();
1228
+ _didIteratorError = true;
1229
+ _iteratorError = err;
1230
+ return [
1231
+ 3,
1232
+ 8
1233
+ ];
1234
+ case 7:
1235
+ try {
1236
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1237
+ _iterator.return();
1238
+ }
1239
+ } finally{
1240
+ if (_didIteratorError) {
1241
+ throw _iteratorError;
1242
+ }
1243
+ }
1244
+ return [
1245
+ 7
1246
+ ];
1247
+ case 8:
1248
+ _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
1249
+ _state.label = 9;
1250
+ case 9:
1251
+ _state.trys.push([
1252
+ 9,
1253
+ 14,
1254
+ 15,
1255
+ 16
1256
+ ]);
1257
+ _iterator1 = _classPrivateFieldGet(this, _cache)[Symbol.iterator]();
1258
+ _state.label = 10;
1259
+ case 10:
1260
+ if (!!(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done)) return [
1261
+ 3,
1262
+ 13
1263
+ ];
1264
+ item1 = _step1.value;
1265
+ _item1 = _slicedToArray(item1, 2), key1 = _item1[0], value1 = _item1[1];
1266
+ deleted1 = _classPrivateMethodGet(this, _deleteIfExpired, deleteIfExpired).call(this, key1, value1);
1267
+ if (!(deleted1 === false)) return [
1268
+ 3,
1269
+ 12
1270
+ ];
1271
+ return [
1272
+ 4,
1273
+ item1
1274
+ ];
1275
+ case 11:
1276
+ _state.sent();
1277
+ _state.label = 12;
1278
+ case 12:
1279
+ _iteratorNormalCompletion1 = true;
1280
+ return [
1281
+ 3,
1282
+ 10
1283
+ ];
1284
+ case 13:
1285
+ return [
1286
+ 3,
1287
+ 16
1288
+ ];
1289
+ case 14:
1290
+ err = _state.sent();
1291
+ _didIteratorError1 = true;
1292
+ _iteratorError1 = err;
1293
+ return [
1294
+ 3,
1295
+ 16
1296
+ ];
1297
+ case 15:
1298
+ try {
1299
+ if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
1300
+ _iterator1.return();
1301
+ }
1302
+ } finally{
1303
+ if (_didIteratorError1) {
1304
+ throw _iteratorError1;
1305
+ }
1306
+ }
1307
+ return [
1308
+ 7
1309
+ ];
1310
+ case 16:
1311
+ return [
1312
+ 2
1313
+ ];
1314
+ }
1315
+ });
1316
+ }
package/package.json CHANGED
@@ -1,27 +1,27 @@
1
1
  {
2
2
  "name": "@common.js/quick-lru",
3
- "version": "6.1.2",
3
+ "version": "7.3.0",
4
4
  "description": "quick-lru package exported as CommonJS modules",
5
5
  "license": "MIT",
6
6
  "repository": "etienne-martin/common.js",
7
7
  "funding": "https://github.com/sponsors/sindresorhus",
8
8
  "type": "commonjs",
9
+ "sideEffects": false,
9
10
  "engines": {
10
- "node": ">=12"
11
+ "node": ">=18"
11
12
  },
12
13
  "scripts": {
13
- "//test": "xo && nyc ava && tsd",
14
- "test": "xo && ava"
14
+ "test": "xo && nyc ava && tsd"
15
15
  },
16
16
  "files": [
17
17
  "index.js",
18
18
  "index.d.ts"
19
19
  ],
20
20
  "devDependencies": {
21
- "ava": "^3.15.0",
21
+ "ava": "^5.3.1",
22
22
  "nyc": "^15.1.0",
23
- "tsd": "^0.14.0",
24
- "xo": "^0.37.1"
23
+ "tsd": "^0.29.0",
24
+ "xo": "^0.56.0"
25
25
  },
26
26
  "nyc": {
27
27
  "reporter": [
@@ -31,5 +31,6 @@
31
31
  },
32
32
  "homepage": "https://github.com/etienne-martin/common.js#readme",
33
33
  "dependencies": {},
34
- "main": "./index.js"
34
+ "main": "./index.js",
35
+ "types": "./index.d.ts"
35
36
  }