limited-cache 2.1.1 → 2.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 (59) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/cjs/index.cjs +296 -0
  3. package/dist/cjs/index.d.cts +107 -0
  4. package/dist/cjs/index.d.cts.map +1 -0
  5. package/dist/esm/index.d.ts +107 -0
  6. package/dist/esm/index.d.ts.map +1 -0
  7. package/dist/esm/index.js +280 -0
  8. package/dist/esm/index.js.map +1 -0
  9. package/package.json +46 -63
  10. package/src/__tests__/LimitedCache.test.ts +2 -2
  11. package/src/__tests__/LimitedCacheObject.test.ts +30 -27
  12. package/src/__tests__/lowLevelFunctions.test.ts +10 -8
  13. package/src/__tests__/scenarios/keys.test.ts +2 -2
  14. package/src/__tests__/scenarios/maxCacheSize.test.ts +8 -5
  15. package/src/__tests__/scenarios/maxCacheTime.test.ts +58 -58
  16. package/src/__tests__/scenarios/values.test.ts +2 -2
  17. package/src/__tests__/typeChecks/LimitedCacheObjectTypes.test.ts +15 -15
  18. package/src/__tests__/typeChecks/LimitedCacheTypes.test.ts +1 -1
  19. package/src/__tests__/typeChecks/lowLevelFunctionsTypes.test.ts +2 -2
  20. package/src/core/LimitedCache.ts +20 -21
  21. package/src/core/LimitedCacheObject.ts +30 -19
  22. package/src/core/defaultOptions.ts +5 -4
  23. package/src/core/limitedCacheUtil.ts +15 -3
  24. package/src/core/lowLevelFunctions.ts +41 -32
  25. package/src/types.ts +16 -3
  26. package/dist/core/LimitedCache.d.ts +0 -4
  27. package/dist/core/LimitedCache.d.ts.map +0 -1
  28. package/dist/core/LimitedCache.js +0 -29
  29. package/dist/core/LimitedCache.js.map +0 -1
  30. package/dist/core/LimitedCacheObject.d.ts +0 -5
  31. package/dist/core/LimitedCacheObject.d.ts.map +0 -1
  32. package/dist/core/LimitedCacheObject.js +0 -52
  33. package/dist/core/LimitedCacheObject.js.map +0 -1
  34. package/dist/core/builtIns.d.ts +0 -12
  35. package/dist/core/builtIns.d.ts.map +0 -1
  36. package/dist/core/builtIns.js +0 -5
  37. package/dist/core/builtIns.js.map +0 -1
  38. package/dist/core/defaultOptions.d.ts +0 -6
  39. package/dist/core/defaultOptions.d.ts.map +0 -1
  40. package/dist/core/defaultOptions.js +0 -14
  41. package/dist/core/defaultOptions.js.map +0 -1
  42. package/dist/core/limitedCacheUtil.d.ts +0 -13
  43. package/dist/core/limitedCacheUtil.d.ts.map +0 -1
  44. package/dist/core/limitedCacheUtil.js +0 -14
  45. package/dist/core/limitedCacheUtil.js.map +0 -1
  46. package/dist/core/lowLevelFunctions.d.ts +0 -14
  47. package/dist/core/lowLevelFunctions.d.ts.map +0 -1
  48. package/dist/core/lowLevelFunctions.js +0 -240
  49. package/dist/core/lowLevelFunctions.js.map +0 -1
  50. package/dist/index.cjs +0 -388
  51. package/dist/index.d.ts +0 -7
  52. package/dist/index.d.ts.map +0 -1
  53. package/dist/index.js +0 -7
  54. package/dist/index.js.map +0 -1
  55. package/dist/types.d.ts +0 -58
  56. package/dist/types.d.ts.map +0 -1
  57. package/dist/types.js +0 -2
  58. package/dist/types.js.map +0 -1
  59. package/src/core/builtIns.ts +0 -9
@@ -0,0 +1,280 @@
1
+ //#region src/core/defaultOptions.ts
2
+ const CURRENT_META_VERSION = 2;
3
+ const MAXIMUM_CACHE_TIME = 365 * 86400 * 1e3;
4
+ const defaultOptions = {
5
+ maxCacheSize: 100,
6
+ maxCacheTime: 86400 * 1e3,
7
+ warnIfItemPurgedBeforeTime: 5e3,
8
+ opLimit: 200,
9
+ scanLimit: 50
10
+ };
11
+ //#endregion
12
+ //#region src/core/lowLevelFunctions.ts
13
+ const positiveNumberOrZero = (value) => Math.max(value, 0) || 0;
14
+ const normalizeOptions = (cacheMetaOptions) => {
15
+ Object.assign(cacheMetaOptions, {
16
+ maxCacheSize: positiveNumberOrZero(cacheMetaOptions.maxCacheSize),
17
+ maxCacheTime: positiveNumberOrZero(cacheMetaOptions.maxCacheTime),
18
+ opLimit: positiveNumberOrZero(cacheMetaOptions.opLimit)
19
+ });
20
+ if (process.env.NODE_ENV !== "production") cacheMetaOptions.warnIfItemPurgedBeforeTime = positiveNumberOrZero(cacheMetaOptions.warnIfItemPurgedBeforeTime);
21
+ return cacheMetaOptions;
22
+ };
23
+ const isCacheMeta = (cacheMeta) => {
24
+ return !!cacheMeta?.limitedCacheMetaVersion;
25
+ };
26
+ const upgradeCacheMeta = (cacheMeta) => {
27
+ if (!isCacheMeta(cacheMeta)) throw new Error("Limited-cache metadata is missing: please check your usage");
28
+ if (cacheMeta.limitedCacheMetaVersion !== 2) {
29
+ console.warn("Limited-cache metadata is from an incompatible version (1). It must be reset.");
30
+ cacheMeta.limitedCacheMetaVersion = 2;
31
+ lowLevelReset(cacheMeta);
32
+ }
33
+ };
34
+ const lowLevelSetOptions = (cacheMeta, options) => {
35
+ upgradeCacheMeta(cacheMeta);
36
+ return normalizeOptions(Object.assign(cacheMeta.options, options));
37
+ };
38
+ const lowLevelInit = (optionsOrCacheMeta) => {
39
+ if (isCacheMeta(optionsOrCacheMeta)) {
40
+ const existingCacheMeta = optionsOrCacheMeta;
41
+ upgradeCacheMeta(existingCacheMeta);
42
+ return existingCacheMeta;
43
+ }
44
+ return lowLevelReset({
45
+ limitedCacheMetaVersion: 2,
46
+ options: normalizeOptions({
47
+ ...defaultOptions,
48
+ ...optionsOrCacheMeta
49
+ })
50
+ });
51
+ };
52
+ const _getExpireTime = (cacheMeta, cacheKey) => {
53
+ const { options: { maxCacheTime }, keyInfo: { [cacheKey]: keyInfo } } = cacheMeta;
54
+ if (!keyInfo) return 0;
55
+ const [setTime, expireTime] = keyInfo;
56
+ return expireTime || setTime + (maxCacheTime || 31536e6);
57
+ };
58
+ const _cacheKeyHasExpired = (cacheMeta, cacheKey, now) => {
59
+ return _getExpireTime(cacheMeta, cacheKey) < now;
60
+ };
61
+ const lowLevelDoMaintenance = (cacheMeta) => {
62
+ upgradeCacheMeta(cacheMeta);
63
+ const { cache, keyList, keyInfo } = cacheMeta;
64
+ const now = Date.now();
65
+ const [newCache, newKeyList, newKeyInfo] = keyList.reduce((acc, cacheKey) => {
66
+ const [accCache, accKeyList, accKeyInfo] = acc;
67
+ if (!_cacheKeyHasExpired(cacheMeta, cacheKey, now)) {
68
+ accCache[cacheKey] = cache[cacheKey];
69
+ accKeyList.push(cacheKey);
70
+ accKeyInfo[cacheKey] = keyInfo[cacheKey];
71
+ }
72
+ return acc;
73
+ }, [
74
+ {},
75
+ [],
76
+ Object.create(null)
77
+ ]);
78
+ return Object.assign(cacheMeta, {
79
+ cache: newCache,
80
+ keyList: newKeyList,
81
+ keyInfo: newKeyInfo,
82
+ opsLeft: cacheMeta.options.opLimit
83
+ });
84
+ };
85
+ const _removeFromIndex = (cacheMeta, startIndex, now) => {
86
+ const { cache, keyList, keyInfo } = cacheMeta;
87
+ let nextIndex = startIndex;
88
+ let nextCacheKey = keyList[startIndex];
89
+ const keyListLength = keyList.length;
90
+ do {
91
+ cache[nextCacheKey] = keyInfo[nextCacheKey] = void 0;
92
+ nextIndex++;
93
+ nextCacheKey = keyList[nextIndex];
94
+ } while (nextIndex < keyListLength && _cacheKeyHasExpired(cacheMeta, nextCacheKey, now));
95
+ keyList.splice(startIndex, nextIndex - startIndex);
96
+ };
97
+ const _removeItemsToMakeRoom = (cacheMeta, now) => {
98
+ const { options: { scanLimit, warnIfItemPurgedBeforeTime }, cache, keyList, keyInfo } = cacheMeta;
99
+ let oldestItemIndex = 0;
100
+ let oldestExpireTime = _getExpireTime(cacheMeta, keyList[0]);
101
+ if (oldestExpireTime > now) {
102
+ let indexToCheck = 0;
103
+ const maxIndexToCheck = Math.min(keyList.length, scanLimit);
104
+ while (indexToCheck < maxIndexToCheck) {
105
+ const cacheKeyForIndex = keyList[indexToCheck];
106
+ const expireTimeForIndex = _getExpireTime(cacheMeta, cacheKeyForIndex);
107
+ if (expireTimeForIndex < now) {
108
+ oldestItemIndex = indexToCheck;
109
+ oldestExpireTime = 0;
110
+ break;
111
+ }
112
+ if (expireTimeForIndex < oldestExpireTime) {
113
+ oldestItemIndex = indexToCheck;
114
+ oldestExpireTime = expireTimeForIndex;
115
+ }
116
+ indexToCheck += 1;
117
+ }
118
+ }
119
+ if (process.env.NODE_ENV !== "production" && warnIfItemPurgedBeforeTime && oldestExpireTime > now) {
120
+ const oldestItemKey = keyList[oldestItemIndex];
121
+ const [oldestItemSetTime, oldestItemExpireTime] = keyInfo[oldestItemKey];
122
+ if (now - oldestItemSetTime < warnIfItemPurgedBeforeTime) console.warn("Purged an item from cache while it was still fresh: you may want to increase maxCacheSize", {
123
+ currentTime: now,
124
+ key: oldestItemKey,
125
+ item: cache[oldestItemKey],
126
+ setTime: oldestItemSetTime,
127
+ expireTime: oldestItemExpireTime,
128
+ timeInCache: now - oldestItemSetTime
129
+ });
130
+ }
131
+ _removeFromIndex(cacheMeta, oldestItemIndex, now);
132
+ };
133
+ const lowLevelHas = (cacheMeta, cacheKey) => {
134
+ upgradeCacheMeta(cacheMeta);
135
+ const { cache } = cacheMeta;
136
+ if (Object.prototype.hasOwnProperty.call(cache, cacheKey) && cache[cacheKey] !== void 0) {
137
+ if (!_cacheKeyHasExpired(cacheMeta, cacheKey, Date.now())) return true;
138
+ cache[cacheKey] = void 0;
139
+ }
140
+ return false;
141
+ };
142
+ const lowLevelGetOne = (cacheMeta, cacheKey) => {
143
+ upgradeCacheMeta(cacheMeta);
144
+ if (lowLevelHas(cacheMeta, cacheKey)) return cacheMeta.cache[cacheKey];
145
+ };
146
+ const lowLevelGetAll = (cacheMeta) => {
147
+ upgradeCacheMeta(cacheMeta);
148
+ lowLevelDoMaintenance(cacheMeta);
149
+ return cacheMeta.cache;
150
+ };
151
+ const lowLevelSet = (cacheMeta, cacheKey, item) => {
152
+ upgradeCacheMeta(cacheMeta);
153
+ const { options: { maxCacheSize }, keyList, keyInfo } = cacheMeta;
154
+ const now = Date.now();
155
+ const isNew = !keyInfo[cacheKey];
156
+ if (cacheMeta.cache[cacheKey] !== item) cacheMeta.cache = {
157
+ ...cacheMeta.cache,
158
+ [cacheKey]: item
159
+ };
160
+ keyInfo[cacheKey] = [now, 0];
161
+ if (isNew) {
162
+ keyList.push(cacheKey);
163
+ cacheMeta.opsLeft--;
164
+ if (cacheMeta.opsLeft <= 0) lowLevelDoMaintenance(cacheMeta);
165
+ if (maxCacheSize && cacheMeta.keyList.length > maxCacheSize) _removeItemsToMakeRoom(cacheMeta, now);
166
+ }
167
+ if (_cacheKeyHasExpired(cacheMeta, keyList[0], now)) _removeFromIndex(cacheMeta, 0, now);
168
+ return cacheMeta;
169
+ };
170
+ const lowLevelRemove = (cacheMeta, cacheKey) => {
171
+ upgradeCacheMeta(cacheMeta);
172
+ const { cache, keyInfo } = cacheMeta;
173
+ if (keyInfo[cacheKey]) {
174
+ if (cache[cacheKey] !== void 0) cacheMeta.cache = {
175
+ ...cache,
176
+ [cacheKey]: void 0
177
+ };
178
+ keyInfo[cacheKey] = void 0;
179
+ }
180
+ return cacheMeta;
181
+ };
182
+ const lowLevelReset = (cacheMeta) => {
183
+ upgradeCacheMeta(cacheMeta);
184
+ return Object.assign(cacheMeta, {
185
+ cache: {},
186
+ keyList: [],
187
+ keyInfo: Object.create(null),
188
+ opsLeft: cacheMeta.options.opLimit
189
+ });
190
+ };
191
+ //#endregion
192
+ //#region src/core/LimitedCache.ts
193
+ const bindFunctionToCacheMeta = (fn, cacheMeta) => fn.bind(null, cacheMeta);
194
+ const LimitedCache = (options) => {
195
+ const cacheMeta = lowLevelInit(options);
196
+ return {
197
+ get: bindFunctionToCacheMeta(lowLevelGetOne, cacheMeta),
198
+ getAll: bindFunctionToCacheMeta(lowLevelGetAll, cacheMeta),
199
+ has: bindFunctionToCacheMeta(lowLevelHas, cacheMeta),
200
+ set: (cacheKey, item) => {
201
+ lowLevelSet(cacheMeta, cacheKey, item);
202
+ return item;
203
+ },
204
+ remove: (cacheKey) => {
205
+ lowLevelRemove(cacheMeta, cacheKey);
206
+ return true;
207
+ },
208
+ reset: bindFunctionToCacheMeta(lowLevelReset, cacheMeta),
209
+ getCacheMeta: () => cacheMeta,
210
+ getOptions: () => cacheMeta.options,
211
+ setOptions: bindFunctionToCacheMeta(lowLevelSetOptions, cacheMeta),
212
+ doMaintenance: bindFunctionToCacheMeta(lowLevelDoMaintenance, cacheMeta)
213
+ };
214
+ };
215
+ //#endregion
216
+ //#region src/core/LimitedCacheObject.ts
217
+ const proxyHandler = {
218
+ get: (cacheMeta, cacheKey) => {
219
+ if (cacheKey === "hasOwnProperty") return Object.prototype.hasOwnProperty;
220
+ return lowLevelGetOne(cacheMeta, cacheKey);
221
+ },
222
+ getOwnPropertyDescriptor: (cacheMeta, cacheKey) => {
223
+ const hasResult = lowLevelHas(cacheMeta, cacheKey);
224
+ const getResult = lowLevelGetOne(cacheMeta, cacheKey);
225
+ if (hasResult) return {
226
+ configurable: true,
227
+ enumerable: hasResult,
228
+ value: getResult,
229
+ writable: true
230
+ };
231
+ },
232
+ has: lowLevelHas,
233
+ set: (cacheMeta, cacheKey, item) => {
234
+ lowLevelSet(cacheMeta, cacheKey, item);
235
+ return item;
236
+ },
237
+ deleteProperty: (cacheMeta, cacheKey) => {
238
+ lowLevelRemove(cacheMeta, cacheKey);
239
+ return true;
240
+ },
241
+ ownKeys: (cacheMeta) => Object.keys(lowLevelGetAll(cacheMeta))
242
+ };
243
+ /**
244
+ * TypeScript's Proxy type models the runtime target, but LimitedCacheObject intentionally returns
245
+ * a facade with a different surface from the internal cache metadata target.
246
+ */
247
+ const internal_createLimitedCacheObjectProxy = (cacheMeta) => {
248
+ return new Proxy(cacheMeta, proxyHandler);
249
+ };
250
+ /**
251
+ * So that we can retrieve the cacheMeta for a LimitedCacheObject, without polluting its properties, each proxy
252
+ * is associated back to its internal cacheMeta here.
253
+ */
254
+ const cacheMetasForProxies = /* @__PURE__ */ new WeakMap();
255
+ const LimitedCacheObject = (options) => {
256
+ const cacheMeta = lowLevelInit(options);
257
+ const limitedCacheObject = internal_createLimitedCacheObjectProxy(cacheMeta);
258
+ cacheMetasForProxies.set(limitedCacheObject, cacheMeta);
259
+ return limitedCacheObject;
260
+ };
261
+ const getCacheMetaFromObject = (instance) => {
262
+ return cacheMetasForProxies.get(instance);
263
+ };
264
+ //#endregion
265
+ //#region src/core/limitedCacheUtil.ts
266
+ const limitedCacheUtil = {
267
+ init: lowLevelInit,
268
+ get: lowLevelGetOne,
269
+ getAll: lowLevelGetAll,
270
+ has: lowLevelHas,
271
+ set: lowLevelSet,
272
+ remove: lowLevelRemove,
273
+ reset: lowLevelReset,
274
+ doMaintenance: lowLevelDoMaintenance,
275
+ setOptions: lowLevelSetOptions
276
+ };
277
+ //#endregion
278
+ export { CURRENT_META_VERSION, LimitedCache, LimitedCacheObject, MAXIMUM_CACHE_TIME, defaultOptions, getCacheMetaFromObject, isCacheMeta, limitedCacheUtil, lowLevelDoMaintenance, lowLevelGetAll, lowLevelGetOne, lowLevelHas, lowLevelInit, lowLevelRemove, lowLevelReset, lowLevelSet, lowLevelSetOptions, upgradeCacheMeta };
279
+
280
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../src/core/defaultOptions.ts","../../src/core/lowLevelFunctions.ts","../../src/core/LimitedCache.ts","../../src/core/LimitedCacheObject.ts","../../src/core/limitedCacheUtil.ts"],"sourcesContent":["import type { LimitedCacheOptionsReadonly } from '../types.js';\n\nconst CURRENT_META_VERSION = 2 as const;\n// Default = 1 year\nconst MAXIMUM_CACHE_TIME: number = 365 * 86400 * 1000;\n\nconst defaultOptions: LimitedCacheOptionsReadonly = {\n // Public\n maxCacheSize: 100,\n maxCacheTime: 86400 * 1000,\n // Development-only\n warnIfItemPurgedBeforeTime: 5000,\n // Private\n opLimit: 200,\n scanLimit: 50,\n};\n\nexport { CURRENT_META_VERSION, defaultOptions, MAXIMUM_CACHE_TIME };\n","import type {\n DefaultItemType,\n LimitedCacheMeta,\n LimitedCacheOptions,\n LimitedCacheOptionsFull,\n LimitedCacheOptionsReadonly,\n} from '../types.js';\nimport { CURRENT_META_VERSION, defaultOptions, MAXIMUM_CACHE_TIME } from './defaultOptions.js';\n\n/* Initialization and options */\n\nconst positiveNumberOrZero = (value: number): number => Math.max(value, 0) || 0;\n\nconst normalizeOptions = (cacheMetaOptions: LimitedCacheOptionsFull): LimitedCacheOptionsFull => {\n Object.assign(cacheMetaOptions, {\n maxCacheSize: positiveNumberOrZero(cacheMetaOptions.maxCacheSize),\n maxCacheTime: positiveNumberOrZero(cacheMetaOptions.maxCacheTime),\n opLimit: positiveNumberOrZero(cacheMetaOptions.opLimit),\n });\n\n if (process.env.NODE_ENV !== 'production') {\n cacheMetaOptions.warnIfItemPurgedBeforeTime = positiveNumberOrZero(\n cacheMetaOptions.warnIfItemPurgedBeforeTime,\n );\n }\n return cacheMetaOptions;\n};\n\nconst isCacheMeta = (cacheMeta: unknown): cacheMeta is LimitedCacheMeta => {\n // @ts-expect-error Duck-typing the unknown value\n return !!cacheMeta?.limitedCacheMetaVersion;\n};\n\nconst upgradeCacheMeta = (cacheMeta: LimitedCacheMeta): void => {\n if (!isCacheMeta(cacheMeta)) {\n throw new Error('Limited-cache metadata is missing: please check your usage');\n }\n if (cacheMeta.limitedCacheMetaVersion !== CURRENT_META_VERSION) {\n // Version is out of date! (Today the only prior version is 1)\n // Version 1: Cache meta cannot be migrated because timestamps and keys are incompatible\n // biome-ignore lint/suspicious/noConsole: Intentional warning\n console.warn('Limited-cache metadata is from an incompatible version (1). It must be reset.');\n cacheMeta.limitedCacheMetaVersion = CURRENT_META_VERSION;\n lowLevelReset(cacheMeta);\n }\n};\n\nconst lowLevelSetOptions = <ItemType = DefaultItemType>(\n cacheMeta: LimitedCacheMeta<ItemType>,\n options: LimitedCacheOptions,\n): LimitedCacheOptionsReadonly => {\n upgradeCacheMeta(cacheMeta);\n return normalizeOptions(Object.assign(cacheMeta.options, options));\n};\n\nconst lowLevelInit = <ItemType = DefaultItemType>(\n optionsOrCacheMeta?: LimitedCacheOptions | LimitedCacheMeta<ItemType>,\n): LimitedCacheMeta<ItemType> => {\n if (isCacheMeta(optionsOrCacheMeta)) {\n const existingCacheMeta = optionsOrCacheMeta;\n upgradeCacheMeta(existingCacheMeta);\n return existingCacheMeta;\n }\n // Else: it's options\n const fullOptions = normalizeOptions({\n ...defaultOptions,\n ...optionsOrCacheMeta,\n });\n\n // The cacheMeta is created once, and persists per instance\n const newCacheMeta = lowLevelReset({\n limitedCacheMetaVersion: CURRENT_META_VERSION,\n options: fullOptions,\n } as LimitedCacheMeta<ItemType>);\n\n return newCacheMeta;\n};\n\n/* Internal cache manipulation */\n\nconst _getExpireTime = (cacheMeta: LimitedCacheMeta, cacheKey: string): number => {\n const {\n options: { maxCacheTime },\n keyInfo: { [cacheKey]: keyInfo },\n } = cacheMeta;\n if (!keyInfo) {\n // A missing record is always treated as expired\n return 0;\n }\n // If we have an exact expireTime then honor it. Otherwise it'll depend on the current maxCacheTime.\n const [setTime, expireTime] = keyInfo;\n return expireTime || setTime + (maxCacheTime || MAXIMUM_CACHE_TIME);\n};\n\nconst _cacheKeyHasExpired = (\n cacheMeta: LimitedCacheMeta,\n cacheKey: string,\n now: number,\n): boolean => {\n return _getExpireTime(cacheMeta, cacheKey) < now;\n};\n\nconst lowLevelDoMaintenance = <ItemType = DefaultItemType>(\n cacheMeta: LimitedCacheMeta<ItemType>,\n): LimitedCacheMeta<ItemType> => {\n upgradeCacheMeta(cacheMeta);\n const { cache, keyList, keyInfo } = cacheMeta;\n const now = Date.now();\n\n // Rebuild cache from keyList only, checking timestamps to auto-remove expired\n const [newCache, newKeyList, newKeyInfo] = keyList.reduce(\n (acc, cacheKey) => {\n const [accCache, accKeyList, accKeyInfo] = acc;\n if (!_cacheKeyHasExpired(cacheMeta, cacheKey, now)) {\n accCache[cacheKey] = cache[cacheKey];\n accKeyList.push(cacheKey);\n accKeyInfo[cacheKey] = keyInfo[cacheKey];\n }\n return acc;\n },\n [\n // This manual assertion is required because TypeScript doesn't know that the initial value is of the same type as the accumulator.\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion\n {} as (typeof cacheMeta)['cache'],\n [] as (typeof cacheMeta)['keyList'],\n Object.create(null) as (typeof cacheMeta)['keyInfo'],\n ],\n );\n\n return Object.assign(cacheMeta, {\n cache: newCache,\n keyList: newKeyList,\n keyInfo: newKeyInfo,\n opsLeft: cacheMeta.options.opLimit,\n });\n};\n\nconst _removeFromIndex = (cacheMeta: LimitedCacheMeta, startIndex: number, now: number): void => {\n const { cache, keyList, keyInfo } = cacheMeta;\n\n // Always remove the item requested, and also remove any neighbors who have expired\n let nextIndex = startIndex;\n let nextCacheKey = keyList[startIndex] as string;\n const keyListLength = keyList.length;\n do {\n // Remove the 'next' item\n\n cache[nextCacheKey] = keyInfo[nextCacheKey] = undefined;\n\n // Now advance and decide whether to keep going\n nextIndex++;\n nextCacheKey = keyList[nextIndex] as string;\n } while (nextIndex < keyListLength && _cacheKeyHasExpired(cacheMeta, nextCacheKey, now));\n\n // Remove the index for everything from the startIndex until we stopped\n keyList.splice(startIndex, nextIndex - startIndex);\n};\n\nconst _removeItemsToMakeRoom = (cacheMeta: LimitedCacheMeta, now: number): void => {\n const {\n options: { scanLimit, warnIfItemPurgedBeforeTime },\n cache,\n keyList,\n keyInfo,\n } = cacheMeta;\n\n // These track the soonest-to-expire thing we've found. It may not actually be \"oldest\".\n // By default we'll remove the item at the head of the queue, unless we find something better.\n let oldestItemIndex = 0;\n let oldestExpireTime = _getExpireTime(cacheMeta, keyList[0] as string);\n\n if (oldestExpireTime > now) {\n // The head of the list hasn't yet expired: scan for a better candidate to remove\n let indexToCheck = 0;\n const maxIndexToCheck = Math.min(keyList.length, scanLimit);\n while (indexToCheck < maxIndexToCheck) {\n const cacheKeyForIndex = keyList[indexToCheck] as string;\n const expireTimeForIndex = _getExpireTime(cacheMeta, cacheKeyForIndex);\n\n // We only consider it if it's eligible for expiration: otherwise it can't be a better option\n // than the default head-of-queue\n if (expireTimeForIndex < now) {\n // We found an expired item! This wins automatically\n oldestItemIndex = indexToCheck;\n oldestExpireTime = 0;\n break;\n }\n if (expireTimeForIndex < oldestExpireTime) {\n // We have a new leader\n oldestItemIndex = indexToCheck;\n oldestExpireTime = expireTimeForIndex;\n }\n indexToCheck += 1;\n }\n }\n\n // Warn if the 'oldest' item is more recent than we'd like: this means it cycled into and out of\n // cache too quickly for the cache to be useful.\n if (\n process.env.NODE_ENV !== 'production' &&\n warnIfItemPurgedBeforeTime &&\n oldestExpireTime > now\n ) {\n const oldestItemKey = keyList[oldestItemIndex] as string;\n const [oldestItemSetTime, oldestItemExpireTime] = keyInfo[oldestItemKey] as [number, number];\n\n if (now - oldestItemSetTime < warnIfItemPurgedBeforeTime) {\n // biome-ignore lint/suspicious/noConsole: Dev-only code\n console.warn(\n 'Purged an item from cache while it was still fresh: you may want to increase maxCacheSize',\n {\n currentTime: now,\n key: oldestItemKey,\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n item: cache[oldestItemKey],\n setTime: oldestItemSetTime,\n expireTime: oldestItemExpireTime,\n timeInCache: now - oldestItemSetTime,\n },\n );\n }\n }\n\n // Remove the oldest item we found, plus any expired neighbors\n _removeFromIndex(cacheMeta, oldestItemIndex, now);\n};\n\n/* Accessors */\n\nconst lowLevelHas = <ItemType = DefaultItemType>(\n cacheMeta: LimitedCacheMeta<ItemType>,\n cacheKey: string,\n): boolean => {\n upgradeCacheMeta(cacheMeta);\n const { cache } = cacheMeta;\n // biome-ignore lint/suspicious/noPrototypeBuiltins: Keeping the legacy hasOwnProperty to avoid a breaking change\n if (Object.prototype.hasOwnProperty.call(cache, cacheKey) && cache[cacheKey] !== undefined) {\n if (!_cacheKeyHasExpired(cacheMeta, cacheKey, Date.now())) {\n return true;\n }\n // If it's expired, clear the value so that we can short-circuit future lookups\n cache[cacheKey] = undefined;\n }\n return false;\n};\n\nconst lowLevelGetOne = <ItemType = DefaultItemType>(\n cacheMeta: LimitedCacheMeta<ItemType>,\n cacheKey: string,\n): ItemType | undefined => {\n upgradeCacheMeta(cacheMeta);\n if (lowLevelHas(cacheMeta, cacheKey)) {\n return cacheMeta.cache[cacheKey];\n }\n return;\n};\n\nconst lowLevelGetAll = <ItemType = DefaultItemType>(\n cacheMeta: LimitedCacheMeta<ItemType>,\n): Record<string, ItemType> => {\n upgradeCacheMeta(cacheMeta);\n // Remove all expired values, and return whatever's left\n lowLevelDoMaintenance(cacheMeta);\n // Retype because there won't be any `undefined` values after doMaintenance\n return cacheMeta.cache as Record<string, ItemType>;\n};\n\nconst lowLevelSet = <ItemType = DefaultItemType>(\n cacheMeta: LimitedCacheMeta<ItemType>,\n cacheKey: string,\n item: ItemType,\n): LimitedCacheMeta<ItemType> => {\n upgradeCacheMeta(cacheMeta);\n\n const {\n options: { maxCacheSize },\n keyList,\n keyInfo,\n } = cacheMeta;\n\n const now = Date.now();\n const isNew = !keyInfo[cacheKey];\n\n if (cacheMeta.cache[cacheKey] !== item) {\n // The cache itself is immutable (but the rest of cacheMeta is not)\n cacheMeta.cache = {\n ...cacheMeta.cache,\n [cacheKey]: item,\n };\n }\n // We've now set or updated it. Regardless of whether it's new, bump its set time\n // @TODO: expireTime override\n keyInfo[cacheKey] = [now, 0];\n\n if (isNew) {\n // It's a new key: grow the cache, then shrink it if we can\n keyList.push(cacheKey);\n\n cacheMeta.opsLeft--;\n if (cacheMeta.opsLeft <= 0) {\n // Time for an oil change\n lowLevelDoMaintenance(cacheMeta);\n }\n\n if (maxCacheSize && cacheMeta.keyList.length > maxCacheSize) {\n // We're still over the limit: drop at least one item\n _removeItemsToMakeRoom(cacheMeta, now);\n }\n }\n\n if (_cacheKeyHasExpired(cacheMeta, keyList[0] as string, now)) {\n // While we're here, if we need to expire the head of the queue then drop it\n _removeFromIndex(cacheMeta, 0, now);\n }\n\n return cacheMeta;\n};\n\nconst lowLevelRemove = <ItemType = DefaultItemType>(\n cacheMeta: LimitedCacheMeta<ItemType>,\n cacheKey: string,\n): LimitedCacheMeta<ItemType> => {\n upgradeCacheMeta(cacheMeta);\n const { cache, keyInfo } = cacheMeta;\n\n if (keyInfo[cacheKey]) {\n if (cache[cacheKey] !== undefined) {\n cacheMeta.cache = {\n ...cache,\n [cacheKey]: undefined,\n };\n }\n keyInfo[cacheKey] = undefined;\n }\n\n return cacheMeta;\n};\n\nconst lowLevelReset = <ItemType = DefaultItemType>(\n cacheMeta: LimitedCacheMeta<ItemType>,\n): LimitedCacheMeta<ItemType> => {\n upgradeCacheMeta(cacheMeta);\n return Object.assign(cacheMeta, {\n cache: {},\n keyList: [],\n keyInfo: Object.create(null) as Record<string, ItemType>,\n opsLeft: cacheMeta.options.opLimit,\n });\n};\n\nexport {\n isCacheMeta,\n lowLevelDoMaintenance,\n lowLevelGetAll,\n lowLevelGetOne,\n lowLevelHas,\n lowLevelInit,\n lowLevelRemove,\n lowLevelReset,\n lowLevelSet,\n lowLevelSetOptions,\n upgradeCacheMeta,\n};\n","import type {\n DefaultItemType,\n LimitedCacheInstance,\n LimitedCacheMeta,\n LimitedCacheOptions,\n LimitedCacheOptionsReadonly,\n} from '../types.js';\nimport {\n lowLevelDoMaintenance,\n lowLevelGetAll,\n lowLevelGetOne,\n lowLevelHas,\n lowLevelInit,\n lowLevelRemove,\n lowLevelReset,\n lowLevelSet,\n lowLevelSetOptions,\n} from './lowLevelFunctions.js';\n\n// Most public functions just call a low-level function directly, passing the cacheMeta.\n// Doing this via a helper function makes the typeChecks easier, and minifies better.\nconst bindFunctionToCacheMeta = <ItemType, OtherArgs extends unknown[], ReturnValue>(\n fn: (cacheMeta: LimitedCacheMeta<ItemType>, ...otherArgs: OtherArgs) => ReturnValue,\n cacheMeta: LimitedCacheMeta<ItemType>,\n): ((...otherArgs: OtherArgs) => ReturnValue) => fn.bind(null, cacheMeta);\n\nconst LimitedCache = <ItemType = DefaultItemType>(\n options?: LimitedCacheOptions,\n): LimitedCacheInstance<ItemType> => {\n const cacheMeta = lowLevelInit<ItemType>(options);\n\n return {\n get: bindFunctionToCacheMeta(lowLevelGetOne, cacheMeta),\n getAll: bindFunctionToCacheMeta(lowLevelGetAll, cacheMeta),\n has: bindFunctionToCacheMeta(lowLevelHas, cacheMeta),\n set: (cacheKey, item): ItemType => {\n lowLevelSet(cacheMeta, cacheKey, item);\n return item;\n },\n remove: (cacheKey): true => {\n lowLevelRemove(cacheMeta, cacheKey);\n return true;\n },\n reset: bindFunctionToCacheMeta(lowLevelReset, cacheMeta),\n getCacheMeta: (): LimitedCacheMeta<ItemType> => cacheMeta,\n getOptions: (): LimitedCacheOptionsReadonly => cacheMeta.options,\n setOptions: bindFunctionToCacheMeta(lowLevelSetOptions, cacheMeta),\n doMaintenance: bindFunctionToCacheMeta(lowLevelDoMaintenance, cacheMeta),\n };\n};\n\nexport { LimitedCache };\n","import type {\n DefaultItemType,\n LimitedCacheMeta,\n LimitedCacheObjectInstance,\n LimitedCacheOptions,\n} from '../types.js';\nimport {\n lowLevelGetAll,\n lowLevelGetOne,\n lowLevelHas,\n lowLevelInit,\n lowLevelRemove,\n lowLevelSet,\n} from './lowLevelFunctions.js';\n\nconst proxyHandler: ProxyHandler<LimitedCacheMeta> = {\n get: (cacheMeta: LimitedCacheMeta, cacheKey: string) => {\n if (cacheKey === 'hasOwnProperty') {\n return Object.prototype.hasOwnProperty;\n }\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return lowLevelGetOne(cacheMeta, cacheKey);\n },\n getOwnPropertyDescriptor: (cacheMeta: LimitedCacheMeta, cacheKey: string) => {\n const hasResult = lowLevelHas(cacheMeta, cacheKey);\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n const getResult = lowLevelGetOne(cacheMeta, cacheKey);\n\n if (hasResult) {\n return {\n configurable: true,\n enumerable: hasResult,\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n value: getResult,\n writable: true,\n };\n }\n return;\n },\n has: lowLevelHas,\n set: <T>(cacheMeta: LimitedCacheMeta, cacheKey: string, item: T): T => {\n lowLevelSet(cacheMeta, cacheKey, item);\n return item;\n },\n deleteProperty: (cacheMeta: LimitedCacheMeta, cacheKey: string): true => {\n lowLevelRemove(cacheMeta, cacheKey);\n return true;\n },\n ownKeys: (cacheMeta: LimitedCacheMeta) => Object.keys(lowLevelGetAll(cacheMeta)),\n};\n\n/**\n * TypeScript's Proxy type models the runtime target, but LimitedCacheObject intentionally returns\n * a facade with a different surface from the internal cache metadata target.\n */\nconst internal_createLimitedCacheObjectProxy = <ItemType = DefaultItemType>(\n cacheMeta: LimitedCacheMeta<ItemType>,\n): LimitedCacheObjectInstance<ItemType> => {\n return new Proxy(cacheMeta, proxyHandler) as unknown as LimitedCacheObjectInstance<ItemType>;\n};\n\n/**\n * So that we can retrieve the cacheMeta for a LimitedCacheObject, without polluting its properties, each proxy\n * is associated back to its internal cacheMeta here.\n */\nconst cacheMetasForProxies = new WeakMap();\n\nconst LimitedCacheObject = <ItemType = DefaultItemType>(\n options?: LimitedCacheOptions,\n): LimitedCacheObjectInstance<ItemType> => {\n const cacheMeta = lowLevelInit<ItemType>(options);\n const limitedCacheObject = internal_createLimitedCacheObjectProxy(cacheMeta);\n\n cacheMetasForProxies.set(limitedCacheObject, cacheMeta);\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return limitedCacheObject;\n};\n\nconst getCacheMetaFromObject = (instance: LimitedCacheObjectInstance): LimitedCacheMeta => {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return cacheMetasForProxies.get(instance);\n};\n\nexport { getCacheMetaFromObject, LimitedCacheObject };\n","import {\n lowLevelDoMaintenance,\n lowLevelGetAll,\n lowLevelGetOne,\n lowLevelHas,\n lowLevelInit,\n lowLevelRemove,\n lowLevelReset,\n lowLevelSet,\n lowLevelSetOptions,\n} from './lowLevelFunctions.js';\n\ntype LimitedCacheUtil = {\n init: typeof lowLevelInit;\n get: typeof lowLevelGetOne;\n getAll: typeof lowLevelGetAll;\n has: typeof lowLevelHas;\n set: typeof lowLevelSet;\n remove: typeof lowLevelRemove;\n reset: typeof lowLevelReset;\n doMaintenance: typeof lowLevelDoMaintenance;\n setOptions: typeof lowLevelSetOptions;\n};\n\nconst limitedCacheUtil: LimitedCacheUtil = {\n init: lowLevelInit,\n get: lowLevelGetOne,\n getAll: lowLevelGetAll,\n has: lowLevelHas,\n set: lowLevelSet,\n remove: lowLevelRemove,\n reset: lowLevelReset,\n doMaintenance: lowLevelDoMaintenance,\n setOptions: lowLevelSetOptions,\n};\n\nexport { limitedCacheUtil };\n"],"mappings":";AAEA,MAAM,uBAAuB;AAE7B,MAAM,qBAA6B,MAAM,QAAQ;AAEjD,MAAM,iBAA8C;CAElD,cAAc;CACd,cAAc,QAAQ;CAEtB,4BAA4B;CAE5B,SAAS;CACT,WAAW;AACb;;;ACJA,MAAM,wBAAwB,UAA0B,KAAK,IAAI,OAAO,CAAC,KAAK;AAE9E,MAAM,oBAAoB,qBAAuE;CAC/F,OAAO,OAAO,kBAAkB;EAC9B,cAAc,qBAAqB,iBAAiB,YAAY;EAChE,cAAc,qBAAqB,iBAAiB,YAAY;EAChE,SAAS,qBAAqB,iBAAiB,OAAO;CACxD,CAAC;CAED,IAAI,QAAQ,IAAI,aAAa,cAC3B,iBAAiB,6BAA6B,qBAC5C,iBAAiB,0BACnB;CAEF,OAAO;AACT;AAEA,MAAM,eAAe,cAAsD;CAEzE,OAAO,CAAC,CAAC,WAAW;AACtB;AAEA,MAAM,oBAAoB,cAAsC;CAC9D,IAAI,CAAC,YAAY,SAAS,GACxB,MAAM,IAAI,MAAM,4DAA4D;CAE9E,IAAI,UAAU,4BAAA,GAAkD;EAI9D,QAAQ,KAAK,+EAA+E;EAC5F,UAAU,0BAAA;EACV,cAAc,SAAS;CACzB;AACF;AAEA,MAAM,sBACJ,WACA,YACgC;CAChC,iBAAiB,SAAS;CAC1B,OAAO,iBAAiB,OAAO,OAAO,UAAU,SAAS,OAAO,CAAC;AACnE;AAEA,MAAM,gBACJ,uBAC+B;CAC/B,IAAI,YAAY,k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AAM,0BAA0B,aAA2D;CAEzF,OAAO,qBAAqB,IAAI,QAAQ;AAC1C;;;ACzDA,MAAM,mBAAqC;CACzC,MAAM;CACN,KAAK;CACL,QAAQ;CACR,KAAK;CACL,KAAK;CACL,QAAQ;CACR,OAAO;CACP,eAAe;CACf,YAAY;AACd"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "limited-cache",
3
- "version": "2.1.1",
3
+ "version": "2.3.0",
4
4
  "description": "A minimal JS cache: like using an object, except it won't grow forever",
5
5
  "keywords": [
6
6
  "limited cache",
@@ -23,7 +23,7 @@
23
23
  },
24
24
  "author": {
25
25
  "name": "Steven Pautz",
26
- "url": "https://github.com/spautz"
26
+ "url": "https://stevenpautz.com"
27
27
  },
28
28
  "publishConfig": {
29
29
  "access": "public",
@@ -33,7 +33,6 @@
33
33
  "files": [
34
34
  "dist/",
35
35
  "docs/",
36
- "legacy-types/",
37
36
  "src/",
38
37
  "LICENSE",
39
38
  "*.md"
@@ -42,84 +41,68 @@
42
41
  "type": "module",
43
42
  "exports": {
44
43
  ".": {
45
- "import": "./dist/index.js",
46
- "require": "./dist/index.cjs",
47
- "types": "./dist/index.d.ts"
44
+ "local-dev": "./src/index.ts",
45
+ "import": {
46
+ "types": "./dist/esm/index.d.ts",
47
+ "default": "./dist/esm/index.js"
48
+ },
49
+ "require": {
50
+ "types": "./dist/cjs/index.d.cts",
51
+ "default": "./dist/cjs/index.cjs"
52
+ }
48
53
  },
49
54
  "./package.json": "./package.json"
50
55
  },
51
- "main": "./dist/index.cjs",
52
- "module": "./dist/index.js",
53
- "jsnext:main": "./dist/index.js",
54
- "types": "./dist/index.d.ts",
56
+ "main": "./dist/cjs/index.cjs",
57
+ "module": "./dist/esm/index.js",
58
+ "types": "./dist/esm/index.d.ts",
55
59
  "sideEffects": false,
56
60
  "dependencies": {},
57
61
  "devDependencies": {
58
- "@types/node": "20.1.1"
62
+ "@arethetypeswrong/cli": "0.18.3",
63
+ "@size-limit/esbuild": "12.1.0",
64
+ "@size-limit/esbuild-why": "12.1.0",
65
+ "@size-limit/file": "12.1.0",
66
+ "publint": "0.3.21",
67
+ "size-limit": "12.1.0",
68
+ "tsdown": "0.22.3"
59
69
  },
60
70
  "peerDependencies": {},
61
71
  "size-limit": [
62
72
  {
63
- "path": "dist/index.js",
73
+ "path": "./dist/esm/index.js",
64
74
  "import": "{}",
65
75
  "limit": "20 B"
66
76
  },
67
77
  {
68
- "path": "dist/index.js",
69
- "limit": "2 kB"
78
+ "path": "./dist/esm/index.js",
79
+ "import": "{ LimitedCache }",
80
+ "limit": "1.2 kb"
70
81
  }
71
82
  ],
72
- "typesVersions": {
73
- "<4.0": {
74
- "*": [
75
- "legacy-types/ts3.5/index.d.ts"
76
- ]
77
- },
78
- "<4.5": {
79
- "*": [
80
- "legacy-types/ts4.0/index.d.ts"
81
- ]
82
- },
83
- "<4.7": {
84
- "*": [
85
- "legacy-types/ts4.5/index.d.ts"
86
- ]
87
- },
88
- "*": {
89
- "*": [
90
- "dist/index.d.ts"
91
- ]
92
- }
93
- },
94
83
  "scripts": {
95
84
  "____ HOOKS _________________________________________________________": "",
96
- "____ INTEGRATION ___________________________________________________": "",
97
- "clean": "pnpm run build:clean && pnpm run test:clean && rimraf --glob ./node_modules/.cache *.log",
98
- "all": "pnpm run format && pnpm run typecheck && pnpm run lint:fix && pnpm run test:coverage && pnpm run build",
99
- "all:readonly": "pnpm run format:verify && pnpm run typecheck && pnpm run lint && pnpm run test:quick",
100
- "all:quick": "pnpm run format && pnpm run typecheck && pnpm run lint:fix",
101
- "all:ci": "pnpm run format:verify && pnpm run typecheck && pnpm run lint && pnpm run test:ci && pnpm run build",
102
- "____ INDIVIDUAL COMMANDS ___________________________________________": "",
103
- "build": "pnpm run build:main && pnpm run sizecheck && pnpm run build:legacytypes",
104
- "build:clean": "rimraf ./dist ./legacy-types",
105
- "build:main": "pnpm run build:clean && tsup src/index.ts --format cjs && tsc -p ./tsconfig.build.json",
106
- "build:legacytypes": "pnpm run build:legacytypes:3.5 && pnpm run build:legacytypes:4.0 && pnpm run build:legacytypes:4.5",
107
- "build:legacytypes:3.5": "downlevel-dts ./dist ./legacy-types/ts3.5 --to=3.5",
108
- "build:legacytypes:4.0": "downlevel-dts ./dist ./legacy-types/ts4.0 --to=4.0",
109
- "build:legacytypes:4.5": "downlevel-dts ./dist ./legacy-types/ts4.5 --to=4.5",
110
- "build:watch": "pnpm run build:clean && tsup src/index.ts --format esm,cjs --dts --watch",
111
- "format": "prettier --write .",
112
- "format:verify": "prettier --list-different .",
113
- "lint": "eslint . --max-warnings 0",
114
- "lint:fix": "eslint . --max-warnings 0 --fix",
115
- "sizecheck": "size-limit",
116
- "test": "pnpm run test:coverage",
85
+ "____ STANDARD PACKAGE TASKS ________________________________________": "",
86
+ "all": "turbo run pipeline-all",
87
+ "all:ci": "turbo run pipeline-ci --ui=stream",
88
+ "build": "pnpm run build:clean && pnpm run build:lib && pnpm run build:verify",
89
+ "clean": "pnpm run \"/^.*:clean$/\" && rimraf --glob ./node_modules/.cache ./.turbo ./*.log",
90
+ "dev": "pnpm run build:lib:watch",
91
+ "lint": "biome check --write .",
92
+ "lint:verify": "biome check .",
93
+ "publish:yalc": "pnpm run prepack && yalc publish --no-scripts --sig --push --replace",
117
94
  "test:clean": "rimraf ./coverage",
118
- "test:ci": "pnpm run test:clean && vitest run --coverage",
119
- "test:coverage": "pnpm run test:clean && vitest run --coverage",
120
- "test:quick": "pnpm run test:clean && vitest run --coverage=false",
121
- "test:watch": "pnpm run test:clean && vitest watch --coverage=false",
122
- "test:watchcoverage": "pnpm run test:clean && vitest watch --coverage",
123
- "typecheck": "tsc -p ./tsconfig.json --noEmit"
95
+ "test": "pnpm run test:clean && vitest run --coverage",
96
+ "test:ci": "pnpm run test:clean && vitest run --coverage --bail 1",
97
+ "test:ui": "pnpm run test:clean && vitest --ui",
98
+ "test:watch": "pnpm run test:clean && vitest watch --coverage",
99
+ "typecheck": "tsgo -p ./tsconfig.json --noEmit",
100
+ "____ BUILD & VALIDATION TASKS ______________________________________": "",
101
+ "build:clean": "rimraf --glob ./dist ./*.tgz",
102
+ "build:lib": "tsdown --config ./tsdown.config.ts",
103
+ "build:lib:watch": "tsdown --config ./tsdown.config.ts --watch",
104
+ "build:verify": "publint --strict && size-limit",
105
+ "build:verifypack": "node --experimental-strip-types ../../scripts/verify-packed-tarballs.ts",
106
+ "size-check": "size-limit --why"
124
107
  }
125
108
  }
@@ -1,7 +1,7 @@
1
- import { describe, beforeEach, expect, it } from 'vitest';
1
+ import { beforeEach, describe, expect, it } from 'vitest';
2
2
  import { defaultOptions } from '../core/defaultOptions.js';
3
3
  import { LimitedCache } from '../core/LimitedCache.js';
4
- import { LimitedCacheInstance } from '../types.js';
4
+ import type { LimitedCacheInstance } from '../types.js';
5
5
 
6
6
  describe('LimitedCache', () => {
7
7
  it('initializes without options', () => {
@@ -1,7 +1,10 @@
1
- import { describe, beforeEach, expect, it } from 'vitest';
1
+ // biome-ignore-all lint/performance/noDelete: Delete is explicitly used to unset items for these tests
2
+ // biome-ignore-all lint/suspicious/noAssignInExpressions: Assignment expressions are explicitly tested
3
+
4
+ import { beforeEach, describe, expect, it } from 'vitest';
2
5
  import { defaultOptions } from '../core/defaultOptions.js';
3
- import { LimitedCacheObject, getCacheMetaFromObject } from '../core/LimitedCacheObject.js';
4
- import { LimitedCacheObjectInstance } from '../types.js';
6
+ import { getCacheMetaFromObject, LimitedCacheObject } from '../core/LimitedCacheObject.js';
7
+ import type { LimitedCacheObjectInstance } from '../types.js';
5
8
 
6
9
  describe('LimitedCacheObject', () => {
7
10
  it('initializes without options', () => {
@@ -29,13 +32,13 @@ describe('LimitedCacheObject', () => {
29
32
  });
30
33
 
31
34
  it('has: when missing, via prototype hasOwnProperty', () => {
32
- const result = Object.prototype.hasOwnProperty.call(myCache, 'asdf');
35
+ const result = Object.hasOwn(myCache, 'asdf');
33
36
  expect(result).toEqual(false);
34
37
  });
35
38
 
36
39
  it('has: when missing, via local hasOwnProperty', () => {
37
40
  // eslint-disable-next-line no-prototype-builtins
38
- const result = myCache.hasOwnProperty('asdf');
41
+ const result = Object.hasOwn(myCache, 'asdf');
39
42
  expect(result).toEqual(false);
40
43
  });
41
44
 
@@ -45,71 +48,71 @@ describe('LimitedCacheObject', () => {
45
48
  });
46
49
 
47
50
  it('has: when present, via prototype hasOwnProperty', () => {
48
- myCache.abc = 123;
51
+ myCache['abc'] = 123;
49
52
 
50
- const result = Object.prototype.hasOwnProperty.call(myCache, 'abc');
53
+ const result = Object.hasOwn(myCache, 'abc');
51
54
  expect(result).toEqual(true);
52
55
  });
53
56
 
54
57
  it('has: when present, via local hasOwnProperty', () => {
55
- myCache.abc = 123;
58
+ myCache['abc'] = 123;
56
59
 
57
60
  // eslint-disable-next-line no-prototype-builtins
58
- const result = myCache.hasOwnProperty('abc');
61
+ const result = Object.hasOwn(myCache, 'abc');
59
62
  expect(result).toEqual(true);
60
63
  });
61
64
 
62
65
  it('has: when present, via "in"', () => {
63
- myCache.abc = 123;
66
+ myCache['abc'] = 123;
64
67
 
65
68
  const result = 'abc' in myCache;
66
69
  expect(result).toEqual(true);
67
70
  });
68
71
 
69
72
  it('get: when missing', () => {
70
- const result = myCache.asdf;
73
+ const result = myCache['asdf'];
71
74
  expect(result).toBeUndefined();
72
75
  });
73
76
 
74
77
  it('get: when present', () => {
75
- myCache.abc = 123;
78
+ myCache['abc'] = 123;
76
79
 
77
- const result = myCache.abc;
80
+ const result = myCache['abc'];
78
81
  expect(result).toEqual(123);
79
82
  });
80
83
 
81
84
  it('set: when new', () => {
82
- const result = (myCache.abc = 123);
85
+ const result = (myCache['abc'] = 123);
83
86
  expect(result).toEqual(123);
84
87
  });
85
88
 
86
89
  it('set: when already present', () => {
87
- myCache.abc = 123;
90
+ myCache['abc'] = 123;
88
91
 
89
- const result = (myCache.abc = 456);
92
+ const result = (myCache['abc'] = 456);
90
93
  expect(result).toEqual(456);
91
94
  });
92
95
 
93
96
  it('delete and return value', () => {
94
- myCache.abc = 123;
97
+ myCache['abc'] = 123;
95
98
 
96
- const result = delete myCache.abc;
99
+ const result = delete myCache['abc'];
97
100
  expect(result).toEqual(true);
98
101
  });
99
102
 
100
103
  it('delete, then check', () => {
101
- myCache.abc = 123;
102
- delete myCache.abc;
104
+ myCache['abc'] = 123;
105
+ delete myCache['abc'];
103
106
 
104
- const result = Object.prototype.hasOwnProperty.call(myCache, 'abc');
107
+ const result = Object.hasOwn(myCache, 'abc');
105
108
  expect(result).toEqual(false);
106
109
  });
107
110
 
108
111
  it('delete, then get', () => {
109
- myCache.abc = 123;
110
- delete myCache.abc;
112
+ myCache['abc'] = 123;
113
+ delete myCache['abc'];
111
114
 
112
- const result = myCache.abc;
115
+ const result = myCache['abc'];
113
116
  expect(result).toEqual(undefined);
114
117
  });
115
118
 
@@ -119,15 +122,15 @@ describe('LimitedCacheObject', () => {
119
122
  });
120
123
 
121
124
  it('keys: when populated', () => {
122
- myCache.abc = 123;
125
+ myCache['abc'] = 123;
123
126
 
124
127
  const result = Object.keys(myCache);
125
128
  expect(result).toEqual(['abc']);
126
129
  });
127
130
 
128
131
  it('keys: when all are removed', () => {
129
- myCache.abc = 123;
130
- delete myCache.abc;
132
+ myCache['abc'] = 123;
133
+ delete myCache['abc'];
131
134
 
132
135
  const result = Object.keys(myCache);
133
136
  expect(result).toEqual([]);