@choksheak/ts-utils 0.3.0 → 0.3.1
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.
- package/arrayBuffer.d.ts +4 -2
- package/assert.d.ts +3 -1
- package/average.d.ts +3 -1
- package/base64Url.d.ts +4 -2
- package/dateTimeStr.d.ts +15 -13
- package/duration.d.ts +15 -13
- package/isEmpty.d.ts +3 -1
- package/iterators.cjs +34 -0
- package/iterators.d.mts +4 -0
- package/iterators.d.ts +4 -0
- package/iterators.min.cjs +2 -0
- package/iterators.min.cjs.map +1 -0
- package/iterators.min.mjs +2 -0
- package/iterators.min.mjs.map +1 -0
- package/iterators.mjs +9 -0
- package/kvStore.cjs +103 -71
- package/kvStore.d.mts +91 -49
- package/kvStore.d.ts +95 -50
- package/kvStore.min.cjs +1 -1
- package/kvStore.min.cjs.map +1 -1
- package/kvStore.min.mjs +1 -1
- package/kvStore.min.mjs.map +1 -1
- package/kvStore.mjs +99 -66
- package/localStore.cjs +267 -0
- package/localStore.d.mts +119 -0
- package/localStore.d.ts +119 -0
- package/localStore.min.cjs +2 -0
- package/localStore.min.cjs.map +1 -0
- package/localStore.min.mjs +2 -0
- package/localStore.min.mjs.map +1 -0
- package/localStore.mjs +235 -0
- package/logging.d.ts +4 -2
- package/nonEmpty.d.ts +3 -1
- package/nonNil.d.ts +3 -1
- package/package.json +48 -15
- package/round.d.ts +4 -2
- package/safeBtoa.d.ts +3 -1
- package/safeParseFloat.d.ts +3 -1
- package/safeParseInt.d.ts +3 -1
- package/sha256.d.ts +3 -1
- package/sleep.d.ts +3 -1
- package/storageAdapter.cjs +18 -0
- package/storageAdapter.d.mts +33 -0
- package/storageAdapter.d.ts +33 -0
- package/storageAdapter.min.cjs +2 -0
- package/storageAdapter.min.cjs.map +1 -0
- package/storageAdapter.min.mjs +1 -0
- package/storageAdapter.min.mjs.map +1 -0
- package/storageAdapter.mjs +0 -0
- package/sum.d.ts +3 -1
- package/timeConstants.d.ts +16 -14
- package/timer.d.ts +4 -2
- package/localStorageCache.cjs +0 -119
- package/localStorageCache.d.mts +0 -57
- package/localStorageCache.d.ts +0 -55
- package/localStorageCache.min.cjs +0 -2
- package/localStorageCache.min.cjs.map +0 -1
- package/localStorageCache.min.mjs +0 -2
- package/localStorageCache.min.mjs.map +0 -1
- package/localStorageCache.mjs +0 -92
package/localStore.mjs
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
// src/timeConstants.ts
|
|
2
|
+
var MS_PER_SECOND = 1e3;
|
|
3
|
+
var MS_PER_MINUTE = 6e4;
|
|
4
|
+
var MS_PER_HOUR = 36e5;
|
|
5
|
+
var MS_PER_DAY = 864e5;
|
|
6
|
+
|
|
7
|
+
// src/duration.ts
|
|
8
|
+
function durationToMs(duration) {
|
|
9
|
+
const daysMs = (duration.days ?? 0) * MS_PER_DAY;
|
|
10
|
+
const hoursMs = (duration.hours ?? 0) * MS_PER_HOUR;
|
|
11
|
+
const minsMs = (duration.minutes ?? 0) * MS_PER_MINUTE;
|
|
12
|
+
const secsMs = (duration.seconds ?? 0) * MS_PER_SECOND;
|
|
13
|
+
const msMs = duration.milliseconds ?? 0;
|
|
14
|
+
return daysMs + hoursMs + minsMs + secsMs + msMs;
|
|
15
|
+
}
|
|
16
|
+
function durationOrMsToMs(duration) {
|
|
17
|
+
return typeof duration === "number" ? duration : durationToMs(duration);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// src/localStore.ts
|
|
21
|
+
var LocalStoreConfig = {
|
|
22
|
+
/** All items with the same store name will share the same storage space. */
|
|
23
|
+
storeName: "ts-utils",
|
|
24
|
+
/** 30 days in ms. */
|
|
25
|
+
expiryMs: MS_PER_DAY * 30,
|
|
26
|
+
/** Do GC once per day. */
|
|
27
|
+
gcIntervalMs: MS_PER_DAY
|
|
28
|
+
};
|
|
29
|
+
function configureLocalStore(config) {
|
|
30
|
+
Object.assign(LocalStoreConfig, config);
|
|
31
|
+
}
|
|
32
|
+
function validateStoredObject(obj) {
|
|
33
|
+
if (!obj || typeof obj !== "object" || obj.value === void 0 || typeof obj.storedMs !== "number" || typeof obj.expiryMs !== "number" || Date.now() >= obj.expiryMs) {
|
|
34
|
+
return void 0;
|
|
35
|
+
}
|
|
36
|
+
return obj;
|
|
37
|
+
}
|
|
38
|
+
var LocalStore = class {
|
|
39
|
+
constructor(storeName, options) {
|
|
40
|
+
this.storeName = storeName;
|
|
41
|
+
this.keyPrefix = storeName + ":";
|
|
42
|
+
this.defaultExpiryMs = options?.defaultExpiryMs ? durationOrMsToMs(options.defaultExpiryMs) : LocalStoreConfig.expiryMs;
|
|
43
|
+
this.gcIntervalMs = options?.gcIntervalMs ? durationOrMsToMs(options.gcIntervalMs) : LocalStoreConfig.gcIntervalMs;
|
|
44
|
+
this.gcMsStorageKey = `__localStore:lastGcMs:${storeName}`;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The prefix string for the local storage key which identifies items
|
|
48
|
+
* belonging to this namespace.
|
|
49
|
+
*/
|
|
50
|
+
keyPrefix;
|
|
51
|
+
/** Local storage key name for the last GC completed timestamp. */
|
|
52
|
+
gcMsStorageKey;
|
|
53
|
+
defaultExpiryMs;
|
|
54
|
+
gcIntervalMs;
|
|
55
|
+
/** Set a value in the store. */
|
|
56
|
+
set(key, value, expiryDeltaMs = this.defaultExpiryMs) {
|
|
57
|
+
const nowMs = Date.now();
|
|
58
|
+
const obj = {
|
|
59
|
+
value,
|
|
60
|
+
storedMs: nowMs,
|
|
61
|
+
expiryMs: nowMs + durationOrMsToMs(expiryDeltaMs)
|
|
62
|
+
};
|
|
63
|
+
localStorage.setItem(this.keyPrefix + key, JSON.stringify(obj));
|
|
64
|
+
this.gc();
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
/** Delete one or multiple keys. */
|
|
68
|
+
delete(key) {
|
|
69
|
+
if (typeof key === "string") {
|
|
70
|
+
localStorage.removeItem(this.keyPrefix + key);
|
|
71
|
+
} else {
|
|
72
|
+
for (const k of key) {
|
|
73
|
+
localStorage.removeItem(this.keyPrefix + k);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/** Mainly used to get the expiration timestamp of an object. */
|
|
78
|
+
getStoredObject(key) {
|
|
79
|
+
const k = this.keyPrefix + key;
|
|
80
|
+
const stored = localStorage.getItem(k);
|
|
81
|
+
if (!stored) {
|
|
82
|
+
return void 0;
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
const parsed = JSON.parse(stored);
|
|
86
|
+
const obj = validateStoredObject(parsed);
|
|
87
|
+
if (!obj) {
|
|
88
|
+
this.delete(k);
|
|
89
|
+
this.gc();
|
|
90
|
+
return void 0;
|
|
91
|
+
}
|
|
92
|
+
return obj;
|
|
93
|
+
} catch (e) {
|
|
94
|
+
console.error(`Invalid local value: ${k}=${stored}:`, e);
|
|
95
|
+
this.delete(k);
|
|
96
|
+
this.gc();
|
|
97
|
+
return void 0;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/** Get a value by key, or undefined if it does not exist. */
|
|
101
|
+
get(key) {
|
|
102
|
+
const obj = this.getStoredObject(key);
|
|
103
|
+
return obj?.value;
|
|
104
|
+
}
|
|
105
|
+
/** Generic way to iterate through all entries. */
|
|
106
|
+
forEach(callback) {
|
|
107
|
+
for (const k of Object.keys(localStorage)) {
|
|
108
|
+
if (!k.startsWith(this.keyPrefix)) continue;
|
|
109
|
+
const key = k.slice(this.keyPrefix.length);
|
|
110
|
+
const obj = this.getStoredObject(key);
|
|
111
|
+
if (!obj) continue;
|
|
112
|
+
callback(key, obj.value, obj.expiryMs, obj.storedMs);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Returns the number of items in the store. Note that getting the size
|
|
117
|
+
* requires iterating through the entire store because the items could expire
|
|
118
|
+
* at any time, and hence the size is a dynamic number.
|
|
119
|
+
*/
|
|
120
|
+
size() {
|
|
121
|
+
let count = 0;
|
|
122
|
+
this.forEach(() => {
|
|
123
|
+
count++;
|
|
124
|
+
});
|
|
125
|
+
return count;
|
|
126
|
+
}
|
|
127
|
+
/** Remove all items from the store. */
|
|
128
|
+
clear() {
|
|
129
|
+
for (const key of Object.keys(localStorage)) {
|
|
130
|
+
if (key.startsWith(this.keyPrefix)) {
|
|
131
|
+
localStorage.removeItem(key);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Returns all items as map of key to value, mainly used for debugging dumps.
|
|
137
|
+
* The type T is applied to all values, even though they might not be of type
|
|
138
|
+
* T (in the case when you store different data types in the same store).
|
|
139
|
+
*/
|
|
140
|
+
asMap() {
|
|
141
|
+
const map = /* @__PURE__ */ new Map();
|
|
142
|
+
this.forEach((key, value, expiryMs, storedMs) => {
|
|
143
|
+
map.set(key, { value, expiryMs, storedMs });
|
|
144
|
+
});
|
|
145
|
+
return map;
|
|
146
|
+
}
|
|
147
|
+
/** Returns the ms timestamp for the last GC (garbage collection). */
|
|
148
|
+
get lastGcMs() {
|
|
149
|
+
const lastGcMsStr = globalThis.localStorage.getItem(this.gcMsStorageKey);
|
|
150
|
+
if (!lastGcMsStr) return 0;
|
|
151
|
+
const ms = Number(lastGcMsStr);
|
|
152
|
+
return isNaN(ms) ? 0 : ms;
|
|
153
|
+
}
|
|
154
|
+
/** Set the ms timestamp for the last GC (garbage collection). */
|
|
155
|
+
set lastGcMs(ms) {
|
|
156
|
+
globalThis.localStorage.setItem(this.gcMsStorageKey, String(ms));
|
|
157
|
+
}
|
|
158
|
+
/** Perform garbage-collection if due, else do nothing. */
|
|
159
|
+
gc() {
|
|
160
|
+
const lastGcMs = this.lastGcMs;
|
|
161
|
+
if (!lastGcMs) {
|
|
162
|
+
this.lastGcMs = Date.now();
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (Date.now() < lastGcMs + this.gcIntervalMs) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
this.gcNow();
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Perform garbage collection immediately without checking whether we are
|
|
172
|
+
* due for the next GC or not.
|
|
173
|
+
*/
|
|
174
|
+
gcNow() {
|
|
175
|
+
console.log(`Starting localStore GC on ${this.storeName}`);
|
|
176
|
+
this.lastGcMs = Date.now();
|
|
177
|
+
let count = 0;
|
|
178
|
+
this.forEach((key, value, expiryMs) => {
|
|
179
|
+
if (Date.now() >= expiryMs) {
|
|
180
|
+
this.delete(key);
|
|
181
|
+
count++;
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
console.log(
|
|
185
|
+
`Finished localStore GC on ${this.storeName} - deleted ${count} keys`
|
|
186
|
+
);
|
|
187
|
+
this.lastGcMs = Date.now();
|
|
188
|
+
}
|
|
189
|
+
/** Returns `this` casted into a StorageAdapter<T>. */
|
|
190
|
+
asStorageAdapter() {
|
|
191
|
+
return this;
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
var localStore = new LocalStore(LocalStoreConfig.storeName);
|
|
195
|
+
var LocalStoreItem = class {
|
|
196
|
+
constructor(key, defaultExpiryMs = LocalStoreConfig.expiryMs, store = localStore) {
|
|
197
|
+
this.key = key;
|
|
198
|
+
this.store = store;
|
|
199
|
+
this.defaultExpiryMs = defaultExpiryMs && durationOrMsToMs(defaultExpiryMs);
|
|
200
|
+
}
|
|
201
|
+
defaultExpiryMs;
|
|
202
|
+
/** Set a value in the store. */
|
|
203
|
+
set(value, expiryDeltaMs = this.defaultExpiryMs) {
|
|
204
|
+
this.store.set(this.key, value, expiryDeltaMs);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Example usage:
|
|
208
|
+
*
|
|
209
|
+
* const { value, storedMs, expiryMs, storedMs } =
|
|
210
|
+
* await myLocalItem.getStoredObject();
|
|
211
|
+
*/
|
|
212
|
+
getStoredObject() {
|
|
213
|
+
return this.store.getStoredObject(this.key);
|
|
214
|
+
}
|
|
215
|
+
/** Get a value by key, or undefined if it does not exist. */
|
|
216
|
+
get() {
|
|
217
|
+
return this.store.get(this.key);
|
|
218
|
+
}
|
|
219
|
+
/** Delete this key from the store. */
|
|
220
|
+
delete() {
|
|
221
|
+
this.store.delete(this.key);
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
function localStoreItem(key, expiryMs, store = localStore) {
|
|
225
|
+
expiryMs = expiryMs && durationOrMsToMs(expiryMs);
|
|
226
|
+
return new LocalStoreItem(key, expiryMs, store);
|
|
227
|
+
}
|
|
228
|
+
export {
|
|
229
|
+
LocalStore,
|
|
230
|
+
LocalStoreConfig,
|
|
231
|
+
LocalStoreItem,
|
|
232
|
+
configureLocalStore,
|
|
233
|
+
localStore,
|
|
234
|
+
localStoreItem
|
|
235
|
+
};
|
package/logging.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
declare function stringify(u: unknown): string;
|
|
2
|
+
declare function capLength(u: unknown, maxLength?: number): string;
|
|
3
|
+
|
|
4
|
+
export { capLength, stringify };
|
package/nonEmpty.d.ts
CHANGED
|
@@ -5,4 +5,6 @@
|
|
|
5
5
|
* @param varName The variable name to include in the error to throw when t is
|
|
6
6
|
* empty. Defaults to 'value'.
|
|
7
7
|
*/
|
|
8
|
-
|
|
8
|
+
declare function nonEmpty<T>(t: T | null | undefined | "" | 0 | -0 | 0n | false | typeof NaN, varName?: string): T;
|
|
9
|
+
|
|
10
|
+
export { nonEmpty };
|
package/nonNil.d.ts
CHANGED
|
@@ -5,4 +5,6 @@
|
|
|
5
5
|
* @param varName The variable name to include in the error to throw when t is
|
|
6
6
|
* nil. Defaults to 'value'.
|
|
7
7
|
*/
|
|
8
|
-
|
|
8
|
+
declare function nonNil<T>(t: T | null | undefined, varName?: string): T;
|
|
9
|
+
|
|
10
|
+
export { nonNil };
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@choksheak/ts-utils",
|
|
3
3
|
"license": "The Unlicense",
|
|
4
|
-
"version": "0.3.
|
|
4
|
+
"version": "0.3.1",
|
|
5
5
|
"description": "Random Typescript utilities with support for full tree-shaking",
|
|
6
6
|
"private": false,
|
|
7
7
|
"scripts": {
|
|
8
8
|
"lint": "eslint .",
|
|
9
9
|
"fix": "eslint . --fix",
|
|
10
|
-
"tsc": "npx tsc
|
|
10
|
+
"tsc": "npx tsc",
|
|
11
11
|
"ft": "npm run fix && npm run tsc",
|
|
12
12
|
"test": "vitest run",
|
|
13
13
|
"test:watch": "vitest",
|
|
@@ -34,11 +34,11 @@
|
|
|
34
34
|
"lint-staged": {
|
|
35
35
|
"*.{ts,tsx,js,jsx}": [
|
|
36
36
|
"echo Using `eslint --fix` will revert changes without printing errors!",
|
|
37
|
-
"
|
|
37
|
+
"npm run lint"
|
|
38
38
|
],
|
|
39
39
|
"*.{ts,tsx}": [
|
|
40
40
|
"echo `tsc-files --noEmit` cannot read *.module.css files",
|
|
41
|
-
"
|
|
41
|
+
"bash -c 'npx tsc'"
|
|
42
42
|
]
|
|
43
43
|
},
|
|
44
44
|
"repository": {
|
|
@@ -70,9 +70,10 @@
|
|
|
70
70
|
"eslint-plugin-simple-import-sort": "^12.1.1",
|
|
71
71
|
"fake-indexeddb": "^6.0.0",
|
|
72
72
|
"globals": "^16.4.0",
|
|
73
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
73
74
|
"jiti": "^2.6.1",
|
|
74
75
|
"lint-staged": "^16.2.6",
|
|
75
|
-
"
|
|
76
|
+
"mock-local-storage": "^1.1.24",
|
|
76
77
|
"prettier": "^3.6.2",
|
|
77
78
|
"simple-git-hooks": "^2.13.1",
|
|
78
79
|
"tsup": "^8.5.0",
|
|
@@ -175,6 +176,19 @@
|
|
|
175
176
|
"default": "./isEmpty.cjs"
|
|
176
177
|
}
|
|
177
178
|
},
|
|
179
|
+
"./iterators": {
|
|
180
|
+
"types": "./iterators.d.ts",
|
|
181
|
+
"import": {
|
|
182
|
+
"production": "./iterators.min.mjs",
|
|
183
|
+
"development": "./iterators.mjs",
|
|
184
|
+
"default": "./iterators.mjs"
|
|
185
|
+
},
|
|
186
|
+
"require": {
|
|
187
|
+
"production": "./iterators.min.cjs",
|
|
188
|
+
"development": "./iterators.cjs",
|
|
189
|
+
"default": "./iterators.cjs"
|
|
190
|
+
}
|
|
191
|
+
},
|
|
178
192
|
"./kvStore": {
|
|
179
193
|
"types": "./kvStore.d.ts",
|
|
180
194
|
"import": {
|
|
@@ -188,17 +202,17 @@
|
|
|
188
202
|
"default": "./kvStore.cjs"
|
|
189
203
|
}
|
|
190
204
|
},
|
|
191
|
-
"./
|
|
192
|
-
"types": "./
|
|
205
|
+
"./localStore": {
|
|
206
|
+
"types": "./localStore.d.ts",
|
|
193
207
|
"import": {
|
|
194
|
-
"production": "./
|
|
195
|
-
"development": "./
|
|
196
|
-
"default": "./
|
|
208
|
+
"production": "./localStore.min.mjs",
|
|
209
|
+
"development": "./localStore.mjs",
|
|
210
|
+
"default": "./localStore.mjs"
|
|
197
211
|
},
|
|
198
212
|
"require": {
|
|
199
|
-
"production": "./
|
|
200
|
-
"development": "./
|
|
201
|
-
"default": "./
|
|
213
|
+
"production": "./localStore.min.cjs",
|
|
214
|
+
"development": "./localStore.cjs",
|
|
215
|
+
"default": "./localStore.cjs"
|
|
202
216
|
}
|
|
203
217
|
},
|
|
204
218
|
"./logging": {
|
|
@@ -318,6 +332,19 @@
|
|
|
318
332
|
"default": "./sleep.cjs"
|
|
319
333
|
}
|
|
320
334
|
},
|
|
335
|
+
"./storageAdapter": {
|
|
336
|
+
"types": "./storageAdapter.d.ts",
|
|
337
|
+
"import": {
|
|
338
|
+
"production": "./storageAdapter.min.mjs",
|
|
339
|
+
"development": "./storageAdapter.mjs",
|
|
340
|
+
"default": "./storageAdapter.mjs"
|
|
341
|
+
},
|
|
342
|
+
"require": {
|
|
343
|
+
"production": "./storageAdapter.min.cjs",
|
|
344
|
+
"development": "./storageAdapter.cjs",
|
|
345
|
+
"default": "./storageAdapter.cjs"
|
|
346
|
+
}
|
|
347
|
+
},
|
|
321
348
|
"./sum": {
|
|
322
349
|
"types": "./sum.d.ts",
|
|
323
350
|
"import": {
|
|
@@ -381,11 +408,14 @@
|
|
|
381
408
|
"isEmpty": [
|
|
382
409
|
"./isEmpty.d.ts"
|
|
383
410
|
],
|
|
411
|
+
"iterators": [
|
|
412
|
+
"./iterators.d.ts"
|
|
413
|
+
],
|
|
384
414
|
"kvStore": [
|
|
385
415
|
"./kvStore.d.ts"
|
|
386
416
|
],
|
|
387
|
-
"
|
|
388
|
-
"./
|
|
417
|
+
"localStore": [
|
|
418
|
+
"./localStore.d.ts"
|
|
389
419
|
],
|
|
390
420
|
"logging": [
|
|
391
421
|
"./logging.d.ts"
|
|
@@ -414,6 +444,9 @@
|
|
|
414
444
|
"sleep": [
|
|
415
445
|
"./sleep.d.ts"
|
|
416
446
|
],
|
|
447
|
+
"storageAdapter": [
|
|
448
|
+
"./storageAdapter.d.ts"
|
|
449
|
+
],
|
|
417
450
|
"sum": [
|
|
418
451
|
"./sum.d.ts"
|
|
419
452
|
],
|
package/round.d.ts
CHANGED
|
@@ -4,9 +4,11 @@
|
|
|
4
4
|
* at the end. To show the exact number of decimal places, you'll need to use
|
|
5
5
|
* toFixed(). E.g. round(1.20, 2).toFixed(2).
|
|
6
6
|
*/
|
|
7
|
-
|
|
7
|
+
declare function round(n: number, numDecimalPlaces?: number): number;
|
|
8
8
|
/**
|
|
9
9
|
* Returns a string with the number in the exact number of decimal places
|
|
10
10
|
* specified, in case the number ends with zeroes.
|
|
11
11
|
*/
|
|
12
|
-
|
|
12
|
+
declare function roundS(n: number, numDecimalPlaces?: number): string;
|
|
13
|
+
|
|
14
|
+
export { round, roundS };
|
package/safeBtoa.d.ts
CHANGED
package/safeParseFloat.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Returns 0 or the given defaultValue if the string is not a valid number.
|
|
3
3
|
*/
|
|
4
|
-
|
|
4
|
+
declare function safeParseFloat<T>(s: string, defaultValue?: T | number): T | number;
|
|
5
|
+
|
|
6
|
+
export { safeParseFloat };
|
package/safeParseInt.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Returns 0 or the given defaultValue if the string is not a valid number.
|
|
3
3
|
*/
|
|
4
|
-
|
|
4
|
+
declare function safeParseInt<T>(s: string, defaultValue?: T | number): T | number;
|
|
5
|
+
|
|
6
|
+
export { safeParseInt };
|
package/sha256.d.ts
CHANGED
package/sleep.d.ts
CHANGED
|
@@ -2,4 +2,6 @@
|
|
|
2
2
|
* Sleep for a given number of milliseconds. Note that this method is async,
|
|
3
3
|
* so please remember to call it with await, like `await sleep(1000);`.
|
|
4
4
|
*/
|
|
5
|
-
|
|
5
|
+
declare function sleep(ms: number): Promise<void>;
|
|
6
|
+
|
|
7
|
+
export { sleep };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
|
|
16
|
+
// src/storageAdapter.ts
|
|
17
|
+
var storageAdapter_exports = {};
|
|
18
|
+
module.exports = __toCommonJS(storageAdapter_exports);
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Duration } from './duration.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Most basic interface for a storage implementation. This is designed to be
|
|
5
|
+
* quite easily implemented by users to plug in any new underlying storage.
|
|
6
|
+
*/
|
|
7
|
+
type StorageAdapter<T> = {
|
|
8
|
+
set: (key: string, value: T) => Promise<void> | void;
|
|
9
|
+
get: (key: string) => Promise<T | undefined> | T | undefined;
|
|
10
|
+
delete: (key: string) => Promise<void> | void;
|
|
11
|
+
clear: () => Promise<void> | void;
|
|
12
|
+
};
|
|
13
|
+
/** Most basic interface for stored object. */
|
|
14
|
+
type StoredObject<T> = {
|
|
15
|
+
value: T;
|
|
16
|
+
storedMs: number;
|
|
17
|
+
expiryMs: number;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Full interface for a storage implementation. Each method can be either sync
|
|
21
|
+
* or async, and the interface works with either implementation scheme.
|
|
22
|
+
*/
|
|
23
|
+
type FullStorageAdapter<T> = StorageAdapter<T> & {
|
|
24
|
+
set: (key: string, value: T, expiryDeltaMs?: number | Duration) => Promise<void> | void;
|
|
25
|
+
getStoredObject: (key: string) => Promise<StoredObject<T> | undefined> | StoredObject<T> | undefined;
|
|
26
|
+
forEach(callback: (key: string, value: T, expiryMs: number, storedMs: number) => void | Promise<void>): Promise<void> | void;
|
|
27
|
+
size(): Promise<number> | number;
|
|
28
|
+
asMap<T>(): Promise<Map<string, StoredObject<T>>> | Map<string, StoredObject<T>>;
|
|
29
|
+
gc: () => Promise<void> | void;
|
|
30
|
+
gcNow: () => Promise<void> | void;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type { FullStorageAdapter, StorageAdapter, StoredObject };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Duration } from './duration.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Most basic interface for a storage implementation. This is designed to be
|
|
5
|
+
* quite easily implemented by users to plug in any new underlying storage.
|
|
6
|
+
*/
|
|
7
|
+
type StorageAdapter<T> = {
|
|
8
|
+
set: (key: string, value: T) => Promise<void> | void;
|
|
9
|
+
get: (key: string) => Promise<T | undefined> | T | undefined;
|
|
10
|
+
delete: (key: string) => Promise<void> | void;
|
|
11
|
+
clear: () => Promise<void> | void;
|
|
12
|
+
};
|
|
13
|
+
/** Most basic interface for stored object. */
|
|
14
|
+
type StoredObject<T> = {
|
|
15
|
+
value: T;
|
|
16
|
+
storedMs: number;
|
|
17
|
+
expiryMs: number;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Full interface for a storage implementation. Each method can be either sync
|
|
21
|
+
* or async, and the interface works with either implementation scheme.
|
|
22
|
+
*/
|
|
23
|
+
type FullStorageAdapter<T> = StorageAdapter<T> & {
|
|
24
|
+
set: (key: string, value: T, expiryDeltaMs?: number | Duration) => Promise<void> | void;
|
|
25
|
+
getStoredObject: (key: string) => Promise<StoredObject<T> | undefined> | StoredObject<T> | undefined;
|
|
26
|
+
forEach(callback: (key: string, value: T, expiryMs: number, storedMs: number) => void | Promise<void>): Promise<void> | void;
|
|
27
|
+
size(): Promise<number> | number;
|
|
28
|
+
asMap<T>(): Promise<Map<string, StoredObject<T>>> | Map<string, StoredObject<T>>;
|
|
29
|
+
gc: () => Promise<void> | void;
|
|
30
|
+
gcNow: () => Promise<void> | void;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type { FullStorageAdapter, StorageAdapter, StoredObject };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var t=Object.defineProperty;var s=Object.getOwnPropertyDescriptor;var n=Object.getOwnPropertyNames;var a=Object.prototype.hasOwnProperty;var m=(r,e,d,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of n(e))!a.call(r,o)&&o!==d&&t(r,o,{get:()=>e[o],enumerable:!(i=s(e,o))||i.enumerable});return r};var v=r=>m(t({},"__esModule",{value:!0}),r);var u={};module.exports=v(u);
|
|
2
|
+
//# sourceMappingURL=storageAdapter.min.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/storageAdapter.ts"],"sourcesContent":["import { Duration } from \"./duration\";\n\n/**\n * Most basic interface for a storage implementation. This is designed to be\n * quite easily implemented by users to plug in any new underlying storage.\n */\nexport type StorageAdapter<T> = {\n set: (key: string, value: T) => Promise<void> | void;\n get: (key: string) => Promise<T | undefined> | T | undefined;\n delete: (key: string) => Promise<void> | void;\n clear: () => Promise<void> | void;\n};\n\n/** Most basic interface for stored object. */\nexport type StoredObject<T> = {\n value: T;\n storedMs: number;\n expiryMs: number;\n};\n\n/**\n * Full interface for a storage implementation. Each method can be either sync\n * or async, and the interface works with either implementation scheme.\n */\nexport type FullStorageAdapter<T> = StorageAdapter<T> & {\n // Redefining `set()`, but with an optional expiration parameter.\n set: (\n key: string,\n value: T,\n expiryDeltaMs?: number | Duration,\n ) => Promise<void> | void;\n\n getStoredObject: (\n key: string,\n ) => Promise<StoredObject<T> | undefined> | StoredObject<T> | undefined;\n\n forEach(\n callback: (\n key: string,\n value: T,\n expiryMs: number,\n storedMs: number,\n ) => void | Promise<void>,\n ): Promise<void> | void;\n\n size(): Promise<number> | number;\n\n asMap<T>():\n | Promise<Map<string, StoredObject<T>>>\n | Map<string, StoredObject<T>>;\n\n gc: () => Promise<void> | void;\n\n gcNow: () => Promise<void> | void;\n};\n"],"mappings":"+WAAA,IAAAA,EAAA,kBAAAC,EAAAD","names":["storageAdapter_exports","__toCommonJS"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=storageAdapter.min.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
File without changes
|
package/sum.d.ts
CHANGED
|
@@ -2,4 +2,6 @@
|
|
|
2
2
|
* Add all the numbers together in the given array. Treats null, undefined and
|
|
3
3
|
* NaN as zero.
|
|
4
4
|
*/
|
|
5
|
-
|
|
5
|
+
declare function sum(numbers: (number | null | undefined)[]): number;
|
|
6
|
+
|
|
7
|
+
export { sum };
|
package/timeConstants.d.ts
CHANGED
|
@@ -2,17 +2,19 @@
|
|
|
2
2
|
* Note that month and year do not have fixed durations, and hence are excluded
|
|
3
3
|
* from this file.
|
|
4
4
|
*/
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
5
|
+
declare const MS_PER_SECOND = 1000;
|
|
6
|
+
declare const MS_PER_MINUTE = 60000;
|
|
7
|
+
declare const MS_PER_HOUR = 3600000;
|
|
8
|
+
declare const MS_PER_DAY = 86400000;
|
|
9
|
+
declare const MS_PER_WEEK = 604800000;
|
|
10
|
+
declare const SECONDS_PER_MINUTE = 60;
|
|
11
|
+
declare const SECONDS_PER_HOUR = 3600;
|
|
12
|
+
declare const SECONDS_PER_DAY = 86400;
|
|
13
|
+
declare const SECONDS_PER_WEEK = 604800;
|
|
14
|
+
declare const MINUTES_PER_HOUR = 60;
|
|
15
|
+
declare const MINUTES_PER_DAY = 1440;
|
|
16
|
+
declare const MINUTES_PER_WEEK = 10080;
|
|
17
|
+
declare const HOURS_PER_DAY = 24;
|
|
18
|
+
declare const HOURS_PER_WEEK = 168;
|
|
19
|
+
|
|
20
|
+
export { HOURS_PER_DAY, HOURS_PER_WEEK, MINUTES_PER_DAY, MINUTES_PER_HOUR, MINUTES_PER_WEEK, MS_PER_DAY, MS_PER_HOUR, MS_PER_MINUTE, MS_PER_SECOND, MS_PER_WEEK, SECONDS_PER_DAY, SECONDS_PER_HOUR, SECONDS_PER_MINUTE, SECONDS_PER_WEEK };
|
package/timer.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
declare class Timer {
|
|
2
2
|
startMs: number;
|
|
3
3
|
endMs: number;
|
|
4
4
|
constructor();
|
|
@@ -8,4 +8,6 @@ export declare class Timer {
|
|
|
8
8
|
toString(): string;
|
|
9
9
|
}
|
|
10
10
|
/** Shorthand for `new Timer()` to make this easier to use. */
|
|
11
|
-
|
|
11
|
+
declare function timer(): Timer;
|
|
12
|
+
|
|
13
|
+
export { Timer, timer };
|