@choksheak/ts-utils 0.3.4 → 0.3.5

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 (72) hide show
  1. package/README.md +9 -3
  2. package/asNumber.cjs +3 -3
  3. package/asNumber.d.mts +1 -1
  4. package/asNumber.d.ts +1 -1
  5. package/asNumber.min.cjs +1 -1
  6. package/asNumber.min.cjs.map +1 -1
  7. package/asNumber.min.mjs +1 -1
  8. package/asNumber.min.mjs.map +1 -1
  9. package/asNumber.mjs +3 -3
  10. package/kvStore.cjs +230 -239
  11. package/kvStore.d.mts +142 -44
  12. package/kvStore.d.ts +142 -44
  13. package/kvStore.min.cjs +1 -1
  14. package/kvStore.min.cjs.map +1 -1
  15. package/kvStore.min.mjs +1 -1
  16. package/kvStore.min.mjs.map +1 -1
  17. package/kvStore.mjs +229 -237
  18. package/localStore.cjs +187 -186
  19. package/localStore.d.mts +135 -34
  20. package/localStore.d.ts +135 -34
  21. package/localStore.min.cjs +1 -1
  22. package/localStore.min.cjs.map +1 -1
  23. package/localStore.min.mjs +1 -1
  24. package/localStore.min.mjs.map +1 -1
  25. package/localStore.mjs +186 -184
  26. package/mean.cjs +3 -3
  27. package/mean.min.cjs +1 -1
  28. package/mean.min.cjs.map +1 -1
  29. package/mean.min.mjs +1 -1
  30. package/mean.min.mjs.map +1 -1
  31. package/mean.mjs +3 -3
  32. package/median.cjs +4 -4
  33. package/median.min.cjs +1 -1
  34. package/median.min.cjs.map +1 -1
  35. package/median.min.mjs +1 -1
  36. package/median.min.mjs.map +1 -1
  37. package/median.mjs +4 -4
  38. package/package.json +2 -34
  39. package/safeBtoa.d.mts +7 -0
  40. package/safeBtoa.d.ts +7 -0
  41. package/safeBtoa.min.cjs.map +1 -1
  42. package/safeBtoa.min.mjs.map +1 -1
  43. package/sum.cjs +3 -3
  44. package/sum.min.cjs +1 -1
  45. package/sum.min.cjs.map +1 -1
  46. package/sum.min.mjs +1 -1
  47. package/sum.min.mjs.map +1 -1
  48. package/sum.mjs +3 -3
  49. package/timer.cjs +19 -24
  50. package/timer.d.mts +9 -6
  51. package/timer.d.ts +9 -6
  52. package/timer.min.cjs +1 -1
  53. package/timer.min.cjs.map +1 -1
  54. package/timer.min.mjs +1 -1
  55. package/timer.min.mjs.map +1 -1
  56. package/timer.mjs +19 -23
  57. package/safeParseFloat.cjs +0 -33
  58. package/safeParseFloat.d.mts +0 -6
  59. package/safeParseFloat.d.ts +0 -6
  60. package/safeParseFloat.min.cjs +0 -2
  61. package/safeParseFloat.min.cjs.map +0 -1
  62. package/safeParseFloat.min.mjs +0 -2
  63. package/safeParseFloat.min.mjs.map +0 -1
  64. package/safeParseFloat.mjs +0 -8
  65. package/safeParseInt.cjs +0 -33
  66. package/safeParseInt.d.mts +0 -6
  67. package/safeParseInt.d.ts +0 -6
  68. package/safeParseInt.min.cjs +0 -2
  69. package/safeParseInt.min.cjs.map +0 -1
  70. package/safeParseInt.min.mjs +0 -2
  71. package/safeParseInt.min.mjs.map +0 -1
  72. package/safeParseInt.mjs +0 -8
package/localStore.cjs CHANGED
@@ -20,10 +20,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/localStore.ts
21
21
  var localStore_exports = {};
22
22
  __export(localStore_exports, {
23
- LocalStore: () => LocalStore,
24
23
  LocalStoreConfig: () => LocalStoreConfig,
25
- LocalStoreItem: () => LocalStoreItem,
26
24
  configureLocalStore: () => configureLocalStore,
25
+ createLocalStore: () => createLocalStore,
27
26
  localStore: () => localStore,
28
27
  localStoreItem: () => localStoreItem
29
28
  });
@@ -66,202 +65,204 @@ function validateStoredObject(obj) {
66
65
  }
67
66
  return obj;
68
67
  }
69
- var LocalStore = class {
70
- constructor(storeName, options) {
71
- this.storeName = storeName;
72
- this.keyPrefix = storeName + ":";
73
- this.defaultExpiryMs = options?.defaultExpiryMs ? durationOrMsToMs(options.defaultExpiryMs) : LocalStoreConfig.expiryMs;
74
- this.gcIntervalMs = options?.gcIntervalMs ? durationOrMsToMs(options.gcIntervalMs) : LocalStoreConfig.gcIntervalMs;
75
- this.gcMsStorageKey = `__localStore:lastGcMs:${storeName}`;
76
- }
77
- /**
78
- * The prefix string for the local storage key which identifies items
79
- * belonging to this namespace.
80
- */
81
- keyPrefix;
82
- /** Local storage key name for the last GC completed timestamp. */
83
- gcMsStorageKey;
84
- defaultExpiryMs;
85
- gcIntervalMs;
86
- /** Set a value in the store. */
87
- set(key, value, expiryDeltaMs = this.defaultExpiryMs) {
88
- const nowMs = Date.now();
89
- const obj = {
90
- value,
91
- storedMs: nowMs,
92
- expiryMs: nowMs + durationOrMsToMs(expiryDeltaMs)
93
- };
94
- localStorage.setItem(this.keyPrefix + key, JSON.stringify(obj));
95
- this.gc();
96
- return value;
97
- }
98
- /** Delete one or multiple keys. */
99
- delete(key) {
100
- if (typeof key === "string") {
101
- localStorage.removeItem(this.keyPrefix + key);
102
- } else {
103
- for (const k of key) {
104
- localStorage.removeItem(this.keyPrefix + k);
68
+ function createLocalStore(storeName, options) {
69
+ const keyPrefix = storeName + ":";
70
+ const defaultExpiryMs = options?.defaultExpiryMs ? durationOrMsToMs(options.defaultExpiryMs) : LocalStoreConfig.expiryMs;
71
+ const gcIntervalMs = options?.gcIntervalMs ? durationOrMsToMs(options.gcIntervalMs) : LocalStoreConfig.gcIntervalMs;
72
+ const gcMsStorageKey = `__localStore:lastGcMs:${storeName}`;
73
+ const obj = {
74
+ /** Input name for the store. */
75
+ storeName,
76
+ /**
77
+ * The prefix string for the local storage key which identifies items
78
+ * belonging to this namespace.
79
+ */
80
+ keyPrefix,
81
+ /** Default expiry to use if not specified in set(). */
82
+ defaultExpiryMs,
83
+ /** Time interval for when GC's occur. */
84
+ gcIntervalMs,
85
+ /** Local storage key name for the last GC completed timestamp. */
86
+ gcMsStorageKey,
87
+ /** Set a value in the store. */
88
+ set(key, value, expiryDeltaMs) {
89
+ const nowMs = Date.now();
90
+ const stored = {
91
+ value,
92
+ storedMs: nowMs,
93
+ expiryMs: nowMs + durationOrMsToMs(expiryDeltaMs ?? defaultExpiryMs)
94
+ };
95
+ localStorage.setItem(keyPrefix + key, JSON.stringify(stored));
96
+ obj.gc();
97
+ return value;
98
+ },
99
+ /** Delete one or multiple keys. */
100
+ delete(key) {
101
+ if (typeof key === "string") {
102
+ localStorage.removeItem(keyPrefix + key);
103
+ } else {
104
+ for (const k of key) {
105
+ localStorage.removeItem(keyPrefix + k);
106
+ }
105
107
  }
106
- }
107
- }
108
- /** Mainly used to get the expiration timestamp of an object. */
109
- getStoredObject(key) {
110
- const k = this.keyPrefix + key;
111
- const stored = localStorage.getItem(k);
112
- if (!stored) {
113
- return void 0;
114
- }
115
- try {
116
- const parsed = JSON.parse(stored);
117
- const obj = validateStoredObject(parsed);
118
- if (!obj) {
119
- this.delete(k);
120
- this.gc();
108
+ },
109
+ /** Mainly used to get the expiration timestamp of an object. */
110
+ getStoredObject(key) {
111
+ const k = keyPrefix + key;
112
+ const stored = localStorage.getItem(k);
113
+ if (!stored) {
121
114
  return void 0;
122
115
  }
123
- return obj;
124
- } catch (e) {
125
- console.error(`Invalid local value: ${k}=${stored}:`, e);
126
- this.delete(k);
127
- this.gc();
128
- return void 0;
129
- }
130
- }
131
- /** Get a value by key, or undefined if it does not exist. */
132
- get(key) {
133
- const obj = this.getStoredObject(key);
134
- return obj?.value;
135
- }
136
- /** Generic way to iterate through all entries. */
137
- forEach(callback) {
138
- for (const k of Object.keys(localStorage)) {
139
- if (!k.startsWith(this.keyPrefix)) continue;
140
- const key = k.slice(this.keyPrefix.length);
141
- const obj = this.getStoredObject(key);
142
- if (!obj) continue;
143
- callback(key, obj.value, obj.expiryMs, obj.storedMs);
144
- }
145
- }
146
- /**
147
- * Returns the number of items in the store. Note that getting the size
148
- * requires iterating through the entire store because the items could expire
149
- * at any time, and hence the size is a dynamic number.
150
- */
151
- size() {
152
- let count = 0;
153
- this.forEach(() => {
154
- count++;
155
- });
156
- return count;
157
- }
158
- /** Remove all items from the store. */
159
- clear() {
160
- for (const key of Object.keys(localStorage)) {
161
- if (key.startsWith(this.keyPrefix)) {
162
- localStorage.removeItem(key);
116
+ try {
117
+ const parsed = JSON.parse(stored);
118
+ const valid = validateStoredObject(parsed);
119
+ if (!valid) {
120
+ obj.delete(k);
121
+ obj.gc();
122
+ return void 0;
123
+ }
124
+ return valid;
125
+ } catch (e) {
126
+ console.error(`Invalid local value: ${k}=${stored}:`, e);
127
+ obj.delete(k);
128
+ obj.gc();
129
+ return void 0;
163
130
  }
164
- }
165
- }
166
- /**
167
- * Returns all items as map of key to value, mainly used for debugging dumps.
168
- * The type T is applied to all values, even though they might not be of type
169
- * T (in the case when you store different data types in the same store).
170
- */
171
- asMap() {
172
- const map = /* @__PURE__ */ new Map();
173
- this.forEach((key, value, expiryMs, storedMs) => {
174
- map.set(key, { value, expiryMs, storedMs });
175
- });
176
- return map;
177
- }
178
- /** Returns the ms timestamp for the last GC (garbage collection). */
179
- get lastGcMs() {
180
- const lastGcMsStr = globalThis.localStorage.getItem(this.gcMsStorageKey);
181
- if (!lastGcMsStr) return 0;
182
- const ms = Number(lastGcMsStr);
183
- return isNaN(ms) ? 0 : ms;
184
- }
185
- /** Set the ms timestamp for the last GC (garbage collection). */
186
- set lastGcMs(ms) {
187
- globalThis.localStorage.setItem(this.gcMsStorageKey, String(ms));
188
- }
189
- /** Perform garbage-collection if due, else do nothing. */
190
- gc() {
191
- const lastGcMs = this.lastGcMs;
192
- if (!lastGcMs) {
193
- this.lastGcMs = Date.now();
194
- return;
195
- }
196
- if (Date.now() < lastGcMs + this.gcIntervalMs) {
197
- return;
198
- }
199
- this.gcNow();
200
- }
201
- /**
202
- * Perform garbage collection immediately without checking whether we are
203
- * due for the next GC or not.
204
- */
205
- gcNow() {
206
- console.log(`Starting localStore GC on ${this.storeName}`);
207
- this.lastGcMs = Date.now();
208
- let count = 0;
209
- this.forEach((key, value, expiryMs) => {
210
- if (Date.now() >= expiryMs) {
211
- this.delete(key);
131
+ },
132
+ /** Get a value by key, or undefined if it does not exist. */
133
+ get(key) {
134
+ const stored = obj.getStoredObject(key);
135
+ return stored?.value;
136
+ },
137
+ /** Generic way to iterate through all entries. */
138
+ forEach(callback) {
139
+ for (const k of Object.keys(localStorage)) {
140
+ if (!k.startsWith(keyPrefix)) continue;
141
+ const key = k.slice(keyPrefix.length);
142
+ const stored = obj.getStoredObject(key);
143
+ if (!stored) continue;
144
+ callback(key, stored.value, stored.expiryMs, stored.storedMs);
145
+ }
146
+ },
147
+ /**
148
+ * Returns the number of items in the store. Note that getting the size
149
+ * requires iterating through the entire store because the items could expire
150
+ * at any time, and hence the size is a dynamic number.
151
+ */
152
+ size() {
153
+ let count = 0;
154
+ obj.forEach(() => {
212
155
  count++;
156
+ });
157
+ return count;
158
+ },
159
+ /** Remove all items from the store. */
160
+ clear() {
161
+ for (const key of Object.keys(localStorage)) {
162
+ if (key.startsWith(keyPrefix)) {
163
+ localStorage.removeItem(key);
164
+ }
213
165
  }
214
- });
215
- console.log(
216
- `Finished localStore GC on ${this.storeName} - deleted ${count} keys`
217
- );
218
- this.lastGcMs = Date.now();
219
- }
220
- /** Returns `this` casted into a StorageAdapter<T>. */
221
- asStorageAdapter() {
222
- return this;
223
- }
224
- };
225
- var localStore = new LocalStore(LocalStoreConfig.storeName);
226
- var LocalStoreItem = class {
227
- constructor(key, defaultExpiryMs = LocalStoreConfig.expiryMs, store = localStore) {
228
- this.key = key;
229
- this.store = store;
230
- this.defaultExpiryMs = defaultExpiryMs && durationOrMsToMs(defaultExpiryMs);
231
- }
232
- defaultExpiryMs;
233
- /** Set a value in the store. */
234
- set(value, expiryDeltaMs = this.defaultExpiryMs) {
235
- this.store.set(this.key, value, expiryDeltaMs);
236
- }
237
- /**
238
- * Example usage:
239
- *
240
- * const { value, storedMs, expiryMs, storedMs } =
241
- * await myLocalItem.getStoredObject();
242
- */
243
- getStoredObject() {
244
- return this.store.getStoredObject(this.key);
245
- }
246
- /** Get a value by key, or undefined if it does not exist. */
247
- get() {
248
- return this.store.get(this.key);
249
- }
250
- /** Delete this key from the store. */
251
- delete() {
252
- this.store.delete(this.key);
253
- }
254
- };
166
+ },
167
+ /**
168
+ * Returns all items as map of key to value, mainly used for debugging dumps.
169
+ * The type T is applied to all values, even though they might not be of type
170
+ * T (in the case when you store different data types in the same store).
171
+ */
172
+ asMap() {
173
+ const map = /* @__PURE__ */ new Map();
174
+ obj.forEach(
175
+ (key, value, expiryMs, storedMs) => {
176
+ map.set(key, { value, expiryMs, storedMs });
177
+ }
178
+ );
179
+ return map;
180
+ },
181
+ /** Returns the ms timestamp for the last GC (garbage collection). */
182
+ getLastGcMs() {
183
+ const lastGcMsStr = localStorage.getItem(gcMsStorageKey);
184
+ if (!lastGcMsStr) return 0;
185
+ const ms = Number(lastGcMsStr);
186
+ return isNaN(ms) ? 0 : ms;
187
+ },
188
+ /** Set the ms timestamp for the last GC (garbage collection). */
189
+ setLastGcMs(ms) {
190
+ localStorage.setItem(gcMsStorageKey, String(ms));
191
+ },
192
+ /** Perform garbage-collection if due, else do nothing. */
193
+ gc() {
194
+ const lastGcMs = obj.getLastGcMs();
195
+ if (!lastGcMs) {
196
+ obj.setLastGcMs(Date.now());
197
+ return;
198
+ }
199
+ if (Date.now() < lastGcMs + gcIntervalMs) {
200
+ return;
201
+ }
202
+ obj.gcNow();
203
+ },
204
+ /**
205
+ * Perform garbage collection immediately without checking whether we are
206
+ * due for the next GC or not.
207
+ */
208
+ gcNow() {
209
+ console.log(`Starting localStore GC on ${storeName}`);
210
+ obj.setLastGcMs(Date.now());
211
+ let count = 0;
212
+ obj.forEach((key, value, expiryMs) => {
213
+ if (Date.now() >= expiryMs) {
214
+ obj.delete(key);
215
+ count++;
216
+ }
217
+ });
218
+ console.log(
219
+ `Finished localStore GC on ${storeName} - deleted ${count} keys`
220
+ );
221
+ obj.setLastGcMs(Date.now());
222
+ },
223
+ /** Returns `this` casted into a StorageAdapter<T>. */
224
+ asStorageAdapter() {
225
+ return obj;
226
+ }
227
+ };
228
+ return obj;
229
+ }
230
+ var localStore = createLocalStore(LocalStoreConfig.storeName);
255
231
  function localStoreItem(key, expiryMs, store = localStore) {
256
- expiryMs = expiryMs && durationOrMsToMs(expiryMs);
257
- return new LocalStoreItem(key, expiryMs, store);
232
+ const defaultExpiryMs = expiryMs && durationOrMsToMs(expiryMs);
233
+ const obj = {
234
+ key,
235
+ defaultExpiryMs,
236
+ store,
237
+ /** Set a value in the store. */
238
+ set(value, expiryDeltaMs) {
239
+ store.set(key, value, expiryDeltaMs ?? defaultExpiryMs);
240
+ },
241
+ /**
242
+ * Example usage:
243
+ *
244
+ * const { value, storedMs, expiryMs, storedMs } =
245
+ * await myLocalItem.getStoredObject();
246
+ */
247
+ getStoredObject() {
248
+ return store.getStoredObject(key);
249
+ },
250
+ /** Get a value by key, or undefined if it does not exist. */
251
+ get() {
252
+ return store.get(key);
253
+ },
254
+ /** Delete this key from the store. */
255
+ delete() {
256
+ store.delete(key);
257
+ }
258
+ };
259
+ return obj;
258
260
  }
259
261
  // Annotate the CommonJS export names for ESM import in node:
260
262
  0 && (module.exports = {
261
- LocalStore,
262
263
  LocalStoreConfig,
263
- LocalStoreItem,
264
264
  configureLocalStore,
265
+ createLocalStore,
265
266
  localStore,
266
267
  localStoreItem
267
268
  });
package/localStore.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Duration } from './duration.mjs';
2
- import { FullStorageAdapter, StoredObject, StorageAdapter } from './storageAdapter.mjs';
2
+ import { StoredObject, StorageAdapter } from './storageAdapter.mjs';
3
3
 
4
4
  /**
5
5
  * Local storage key-value store with support for auto-expirations.
@@ -33,72 +33,174 @@ declare function configureLocalStore(config: Partial<LocalStoreConfig>): void;
33
33
  * You can create multiple LocalStores if you want, but most likely you will only
34
34
  * need to use the default `localStore` instance.
35
35
  */
36
- declare class LocalStore implements FullStorageAdapter<any> {
36
+ declare function createLocalStore(storeName: string, options?: {
37
+ defaultExpiryMs?: number | Duration;
38
+ gcIntervalMs?: number | Duration;
39
+ }): {
40
+ /** Input name for the store. */
37
41
  readonly storeName: string;
38
42
  /**
39
43
  * The prefix string for the local storage key which identifies items
40
44
  * belonging to this namespace.
41
45
  */
42
46
  readonly keyPrefix: string;
43
- /** Local storage key name for the last GC completed timestamp. */
44
- readonly gcMsStorageKey: string;
47
+ /** Default expiry to use if not specified in set(). */
45
48
  readonly defaultExpiryMs: number;
49
+ /** Time interval for when GC's occur. */
46
50
  readonly gcIntervalMs: number;
47
- constructor(storeName: string, options?: {
48
- defaultExpiryMs?: number | Duration;
49
- gcIntervalMs?: number | Duration;
50
- });
51
+ /** Local storage key name for the last GC completed timestamp. */
52
+ readonly gcMsStorageKey: string;
51
53
  /** Set a value in the store. */
52
- set<T>(key: string, value: T, expiryDeltaMs?: number | Duration): T;
54
+ readonly set: <T>(key: string, value: T, expiryDeltaMs?: number | Duration) => T;
53
55
  /** Delete one or multiple keys. */
54
- delete(key: string | string[]): void;
56
+ readonly delete: (key: string | string[]) => void;
55
57
  /** Mainly used to get the expiration timestamp of an object. */
56
- getStoredObject<T>(key: string): StoredObject<T> | undefined;
58
+ readonly getStoredObject: <T>(key: string) => StoredObject<T> | undefined;
57
59
  /** Get a value by key, or undefined if it does not exist. */
58
- get<T>(key: string): T | undefined;
60
+ readonly get: <T>(key: string) => T | undefined;
59
61
  /** Generic way to iterate through all entries. */
60
- forEach<T>(callback: (key: string, value: T, expiryMs: number, storedMs: number) => void): void;
62
+ readonly forEach: <T>(callback: (key: string, value: T, expiryMs: number, storedMs: number) => void) => void;
61
63
  /**
62
64
  * Returns the number of items in the store. Note that getting the size
63
65
  * requires iterating through the entire store because the items could expire
64
66
  * at any time, and hence the size is a dynamic number.
65
67
  */
66
- size(): number;
68
+ readonly size: () => number;
67
69
  /** Remove all items from the store. */
68
- clear(): void;
70
+ readonly clear: () => void;
69
71
  /**
70
72
  * Returns all items as map of key to value, mainly used for debugging dumps.
71
73
  * The type T is applied to all values, even though they might not be of type
72
74
  * T (in the case when you store different data types in the same store).
73
75
  */
74
- asMap<T>(): Map<string, StoredObject<T>>;
76
+ readonly asMap: <T>() => Map<string, StoredObject<T>>;
75
77
  /** Returns the ms timestamp for the last GC (garbage collection). */
76
- get lastGcMs(): number;
78
+ readonly getLastGcMs: () => number;
77
79
  /** Set the ms timestamp for the last GC (garbage collection). */
78
- set lastGcMs(ms: number);
80
+ readonly setLastGcMs: (ms: number) => void;
79
81
  /** Perform garbage-collection if due, else do nothing. */
80
- gc(): void;
82
+ readonly gc: () => void;
81
83
  /**
82
84
  * Perform garbage collection immediately without checking whether we are
83
85
  * due for the next GC or not.
84
86
  */
85
- gcNow(): void;
87
+ readonly gcNow: () => void;
86
88
  /** Returns `this` casted into a StorageAdapter<T>. */
87
- asStorageAdapter<T>(): StorageAdapter<T>;
88
- }
89
+ readonly asStorageAdapter: <T>() => StorageAdapter<T>;
90
+ };
91
+ type LocalStore = ReturnType<typeof createLocalStore>;
89
92
  /**
90
93
  * Default local store ready for immediate use. You can create new instances if
91
94
  * you want, but most likely you will only need one store instance.
92
95
  */
93
- declare const localStore: LocalStore;
94
- /**
95
- * Class to represent one key in the store with a default expiration.
96
- */
97
- declare class LocalStoreItem<T> {
98
- readonly key: string;
99
- readonly store: LocalStore;
96
+ declare const localStore: {
97
+ /** Input name for the store. */
98
+ readonly storeName: string;
99
+ /**
100
+ * The prefix string for the local storage key which identifies items
101
+ * belonging to this namespace.
102
+ */
103
+ readonly keyPrefix: string;
104
+ /** Default expiry to use if not specified in set(). */
100
105
  readonly defaultExpiryMs: number;
101
- constructor(key: string, defaultExpiryMs?: number | Duration, store?: LocalStore);
106
+ /** Time interval for when GC's occur. */
107
+ readonly gcIntervalMs: number;
108
+ /** Local storage key name for the last GC completed timestamp. */
109
+ readonly gcMsStorageKey: string;
110
+ /** Set a value in the store. */
111
+ readonly set: <T>(key: string, value: T, expiryDeltaMs?: number | Duration) => T;
112
+ /** Delete one or multiple keys. */
113
+ readonly delete: (key: string | string[]) => void;
114
+ /** Mainly used to get the expiration timestamp of an object. */
115
+ readonly getStoredObject: <T>(key: string) => StoredObject<T> | undefined;
116
+ /** Get a value by key, or undefined if it does not exist. */
117
+ readonly get: <T>(key: string) => T | undefined;
118
+ /** Generic way to iterate through all entries. */
119
+ readonly forEach: <T>(callback: (key: string, value: T, expiryMs: number, storedMs: number) => void) => void;
120
+ /**
121
+ * Returns the number of items in the store. Note that getting the size
122
+ * requires iterating through the entire store because the items could expire
123
+ * at any time, and hence the size is a dynamic number.
124
+ */
125
+ readonly size: () => number;
126
+ /** Remove all items from the store. */
127
+ readonly clear: () => void;
128
+ /**
129
+ * Returns all items as map of key to value, mainly used for debugging dumps.
130
+ * The type T is applied to all values, even though they might not be of type
131
+ * T (in the case when you store different data types in the same store).
132
+ */
133
+ readonly asMap: <T>() => Map<string, StoredObject<T>>;
134
+ /** Returns the ms timestamp for the last GC (garbage collection). */
135
+ readonly getLastGcMs: () => number;
136
+ /** Set the ms timestamp for the last GC (garbage collection). */
137
+ readonly setLastGcMs: (ms: number) => void;
138
+ /** Perform garbage-collection if due, else do nothing. */
139
+ readonly gc: () => void;
140
+ /**
141
+ * Perform garbage collection immediately without checking whether we are
142
+ * due for the next GC or not.
143
+ */
144
+ readonly gcNow: () => void;
145
+ /** Returns `this` casted into a StorageAdapter<T>. */
146
+ readonly asStorageAdapter: <T>() => StorageAdapter<T>;
147
+ };
148
+ /** Create a local store item with a key and a default expiration. */
149
+ declare function localStoreItem<T>(key: string, expiryMs?: number | Duration, store?: LocalStore): {
150
+ key: string;
151
+ defaultExpiryMs: number | undefined;
152
+ store: {
153
+ /** Input name for the store. */
154
+ readonly storeName: string;
155
+ /**
156
+ * The prefix string for the local storage key which identifies items
157
+ * belonging to this namespace.
158
+ */
159
+ readonly keyPrefix: string;
160
+ /** Default expiry to use if not specified in set(). */
161
+ readonly defaultExpiryMs: number;
162
+ /** Time interval for when GC's occur. */
163
+ readonly gcIntervalMs: number;
164
+ /** Local storage key name for the last GC completed timestamp. */
165
+ readonly gcMsStorageKey: string;
166
+ /** Set a value in the store. */
167
+ readonly set: <T_1>(key: string, value: T_1, expiryDeltaMs?: number | Duration) => T_1;
168
+ /** Delete one or multiple keys. */
169
+ readonly delete: (key: string | string[]) => void;
170
+ /** Mainly used to get the expiration timestamp of an object. */
171
+ readonly getStoredObject: <T_1>(key: string) => StoredObject<T_1> | undefined;
172
+ /** Get a value by key, or undefined if it does not exist. */
173
+ readonly get: <T_1>(key: string) => T_1 | undefined;
174
+ /** Generic way to iterate through all entries. */
175
+ readonly forEach: <T_1>(callback: (key: string, value: T_1, expiryMs: number, storedMs: number) => void) => void;
176
+ /**
177
+ * Returns the number of items in the store. Note that getting the size
178
+ * requires iterating through the entire store because the items could expire
179
+ * at any time, and hence the size is a dynamic number.
180
+ */
181
+ readonly size: () => number;
182
+ /** Remove all items from the store. */
183
+ readonly clear: () => void;
184
+ /**
185
+ * Returns all items as map of key to value, mainly used for debugging dumps.
186
+ * The type T is applied to all values, even though they might not be of type
187
+ * T (in the case when you store different data types in the same store).
188
+ */
189
+ readonly asMap: <T_1>() => Map<string, StoredObject<T_1>>;
190
+ /** Returns the ms timestamp for the last GC (garbage collection). */
191
+ readonly getLastGcMs: () => number;
192
+ /** Set the ms timestamp for the last GC (garbage collection). */
193
+ readonly setLastGcMs: (ms: number) => void;
194
+ /** Perform garbage-collection if due, else do nothing. */
195
+ readonly gc: () => void;
196
+ /**
197
+ * Perform garbage collection immediately without checking whether we are
198
+ * due for the next GC or not.
199
+ */
200
+ readonly gcNow: () => void;
201
+ /** Returns `this` casted into a StorageAdapter<T>. */
202
+ readonly asStorageAdapter: <T_1>() => StorageAdapter<T_1>;
203
+ };
102
204
  /** Set a value in the store. */
103
205
  set(value: T, expiryDeltaMs?: number | undefined): void;
104
206
  /**
@@ -112,8 +214,7 @@ declare class LocalStoreItem<T> {
112
214
  get(): T | undefined;
113
215
  /** Delete this key from the store. */
114
216
  delete(): void;
115
- }
116
- /** Create a local store item with a key and a default expiration. */
117
- declare function localStoreItem<T>(key: string, expiryMs?: number | Duration, store?: LocalStore): LocalStoreItem<T>;
217
+ };
218
+ type LocalStoreItem<T> = ReturnType<typeof localStoreItem<T>>;
118
219
 
119
- export { LocalStore, LocalStoreConfig, LocalStoreItem, configureLocalStore, localStore, localStoreItem };
220
+ export { type LocalStore, LocalStoreConfig, type LocalStoreItem, configureLocalStore, createLocalStore, localStore, localStoreItem };