@choksheak/ts-utils 0.1.7 → 0.1.9
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 +8 -0
- package/arrayBuffer.js +41 -0
- package/arrayBuffer.js.map +1 -0
- package/dateTimeStr.d.ts +8 -14
- package/dateTimeStr.js +54 -42
- package/dateTimeStr.js.map +1 -1
- package/duration.d.ts +62 -0
- package/duration.js +174 -0
- package/duration.js.map +1 -0
- package/localStorageCache.d.ts +27 -7
- package/localStorageCache.js +62 -31
- package/localStorageCache.js.map +1 -1
- package/package.json +1 -1
- package/safeBtoa.d.ts +4 -0
- package/safeBtoa.js +35 -0
- package/safeBtoa.js.map +1 -0
- package/sha256.d.ts +4 -0
- package/sha256.js +58 -0
- package/sha256.js.map +1 -0
- package/src/arrayBuffer.ts +29 -0
- package/src/dateTimeStr.ts +70 -71
- package/src/duration.ts +229 -0
- package/src/localStorageCache.ts +63 -32
- package/src/safeBtoa.ts +15 -0
- package/src/sha256.ts +13 -0
- package/src/timeConstants.ts +22 -0
- package/timeConstants.d.ts +18 -0
- package/timeConstants.js +70 -0
- package/timeConstants.js.map +1 -0
package/src/localStorageCache.ts
CHANGED
|
@@ -1,18 +1,63 @@
|
|
|
1
|
+
import { Duration, durationToMs } from "./duration";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Simple local storage cache with support for auto-expiration.
|
|
3
5
|
* Note that this works in the browser context only because nodejs does not
|
|
4
6
|
* have local storage.
|
|
7
|
+
*
|
|
8
|
+
* Create a cache item accessor object with auto-expiration. The value will
|
|
9
|
+
* always be stored as a string by applying JSON.stringify(), and will be
|
|
10
|
+
* returned in the same object type by applying JSON.parse().
|
|
11
|
+
*
|
|
12
|
+
* In order to provide proper type-checking, please always specify the T
|
|
13
|
+
* type parameter. E.g. const item = storeItem<string>("name", 10_000);
|
|
14
|
+
*
|
|
15
|
+
* expires - Either a number in milliseconds, or a Duration object
|
|
5
16
|
*/
|
|
6
|
-
export
|
|
7
|
-
|
|
8
|
-
|
|
17
|
+
export function storeItem<T>(
|
|
18
|
+
key: string,
|
|
19
|
+
expires: number | Duration,
|
|
20
|
+
logError = true,
|
|
21
|
+
defaultValue?: T,
|
|
22
|
+
) {
|
|
23
|
+
const expireDeltaMs =
|
|
24
|
+
typeof expires === "number" ? expires : durationToMs(expires);
|
|
25
|
+
|
|
26
|
+
return new CacheItem<T>(key, expireDeltaMs, logError, defaultValue);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
class CacheItem<T> {
|
|
30
|
+
/**
|
|
31
|
+
* Create a cache item accessor object with auto-expiration.
|
|
32
|
+
*/
|
|
33
|
+
public constructor(
|
|
34
|
+
public readonly key: string,
|
|
35
|
+
public readonly expireDeltaMs: number,
|
|
36
|
+
public readonly logError: boolean,
|
|
37
|
+
defaultValue: T | undefined,
|
|
38
|
+
) {
|
|
39
|
+
if (defaultValue !== undefined) {
|
|
40
|
+
if (this.get() === undefined) {
|
|
41
|
+
this.set(defaultValue);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Set the value of this item with auto-expiration.
|
|
48
|
+
*/
|
|
49
|
+
public set(value: T): void {
|
|
50
|
+
const expireMs = Date.now() + this.expireDeltaMs;
|
|
9
51
|
const valueStr = JSON.stringify({ value, expireMs });
|
|
10
52
|
|
|
11
|
-
globalThis.localStorage.setItem(key, valueStr);
|
|
53
|
+
globalThis.localStorage.setItem(this.key, valueStr);
|
|
12
54
|
}
|
|
13
55
|
|
|
14
|
-
|
|
15
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Get the value of this item, or undefined if value is not set or expired.
|
|
58
|
+
*/
|
|
59
|
+
public get(): T | undefined {
|
|
60
|
+
const jsonStr = globalThis.localStorage.getItem(this.key);
|
|
16
61
|
|
|
17
62
|
if (!jsonStr || typeof jsonStr !== "string") {
|
|
18
63
|
return undefined;
|
|
@@ -29,41 +74,27 @@ export class LocalStorageCache {
|
|
|
29
74
|
typeof obj.expireMs !== "number" ||
|
|
30
75
|
Date.now() >= obj.expireMs
|
|
31
76
|
) {
|
|
32
|
-
globalThis.localStorage.removeItem(key);
|
|
77
|
+
globalThis.localStorage.removeItem(this.key);
|
|
33
78
|
return undefined;
|
|
34
79
|
}
|
|
35
80
|
|
|
36
81
|
return obj.value;
|
|
37
82
|
} catch (e) {
|
|
38
|
-
if (logError) {
|
|
39
|
-
console.error(
|
|
83
|
+
if (this.logError) {
|
|
84
|
+
console.error(
|
|
85
|
+
`Found invalid storage value: ${this.key}=${jsonStr}:`,
|
|
86
|
+
e,
|
|
87
|
+
);
|
|
40
88
|
}
|
|
41
|
-
globalThis.localStorage.removeItem(key);
|
|
89
|
+
globalThis.localStorage.removeItem(this.key);
|
|
42
90
|
return undefined;
|
|
43
91
|
}
|
|
44
92
|
}
|
|
45
|
-
}
|
|
46
93
|
|
|
47
|
-
/**
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
public readonly logError = true,
|
|
53
|
-
defaultValue?: T,
|
|
54
|
-
) {
|
|
55
|
-
if (defaultValue !== undefined) {
|
|
56
|
-
if (this.get() === undefined) {
|
|
57
|
-
this.set(defaultValue);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
public set(value: T) {
|
|
63
|
-
LocalStorageCache.setValue(this.key, value, this.expireDeltaMs);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
public get(): T | undefined {
|
|
67
|
-
return LocalStorageCache.getValue(this.key, this.logError);
|
|
94
|
+
/**
|
|
95
|
+
* Remove the value of this item.
|
|
96
|
+
*/
|
|
97
|
+
public remove(): void {
|
|
98
|
+
globalThis.localStorage.removeItem(this.key);
|
|
68
99
|
}
|
|
69
100
|
}
|
package/src/safeBtoa.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base 64 encode the given input string, but safely.
|
|
3
|
+
*/
|
|
4
|
+
export function safeBtoa(input: string): string {
|
|
5
|
+
// Convert the string to a UTF-8 encoded binary-safe string
|
|
6
|
+
const utf8Bytes = new TextEncoder().encode(input);
|
|
7
|
+
|
|
8
|
+
// Convert the binary data to a string for btoa
|
|
9
|
+
const binaryString = Array.from(utf8Bytes)
|
|
10
|
+
.map((byte) => String.fromCodePoint(byte))
|
|
11
|
+
.join("");
|
|
12
|
+
|
|
13
|
+
// Use btoa to encode the binary-safe string
|
|
14
|
+
return btoa(binaryString);
|
|
15
|
+
}
|
package/src/sha256.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SHA-256 hash an input string into an ArrayBuffer.
|
|
3
|
+
*/
|
|
4
|
+
export async function sha256(input: string): Promise<ArrayBuffer> {
|
|
5
|
+
// Encode the input string as a Uint8Array
|
|
6
|
+
const encoder = new TextEncoder();
|
|
7
|
+
const uint8Array = encoder.encode(input);
|
|
8
|
+
|
|
9
|
+
// Compute the SHA-256 hash using the SubtleCrypto API
|
|
10
|
+
const arrayBuffer = await crypto.subtle.digest("SHA-256", uint8Array);
|
|
11
|
+
|
|
12
|
+
return arrayBuffer;
|
|
13
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Note that month and year do not have fixed durations, and hence are excluded
|
|
3
|
+
* from this file.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export const MS_PER_SECOND = 1000;
|
|
7
|
+
export const MS_PER_MINUTE = 60_000;
|
|
8
|
+
export const MS_PER_HOUR = 3_600_000;
|
|
9
|
+
export const MS_PER_DAY = 86_400_000;
|
|
10
|
+
export const MS_PER_WEEK = 604_800_000;
|
|
11
|
+
|
|
12
|
+
export const SECONDS_PER_MINUTE = 60;
|
|
13
|
+
export const SECONDS_PER_HOUR = 3_600;
|
|
14
|
+
export const SECONDS_PER_DAY = 86_400;
|
|
15
|
+
export const SECONDS_PER_WEEK = 604_800;
|
|
16
|
+
|
|
17
|
+
export const MINUTES_PER_HOUR = 60;
|
|
18
|
+
export const MINUTES_PER_DAY = 1440;
|
|
19
|
+
export const MINUTES_PER_WEEK = 10_080;
|
|
20
|
+
|
|
21
|
+
export const HOURS_PER_DAY = 24;
|
|
22
|
+
export const HOURS_PER_WEEK = 168;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Note that month and year do not have fixed durations, and hence are excluded
|
|
3
|
+
* from this file.
|
|
4
|
+
*/
|
|
5
|
+
export declare const MS_PER_SECOND = 1000;
|
|
6
|
+
export declare const MS_PER_MINUTE = 60000;
|
|
7
|
+
export declare const MS_PER_HOUR = 3600000;
|
|
8
|
+
export declare const MS_PER_DAY = 86400000;
|
|
9
|
+
export declare const MS_PER_WEEK = 604800000;
|
|
10
|
+
export declare const SECONDS_PER_MINUTE = 60;
|
|
11
|
+
export declare const SECONDS_PER_HOUR = 3600;
|
|
12
|
+
export declare const SECONDS_PER_DAY = 86400;
|
|
13
|
+
export declare const SECONDS_PER_WEEK = 604800;
|
|
14
|
+
export declare const MINUTES_PER_HOUR = 60;
|
|
15
|
+
export declare const MINUTES_PER_DAY = 1440;
|
|
16
|
+
export declare const MINUTES_PER_WEEK = 10080;
|
|
17
|
+
export declare const HOURS_PER_DAY = 24;
|
|
18
|
+
export declare const HOURS_PER_WEEK = 168;
|
package/timeConstants.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
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 __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/timeConstants.ts
|
|
21
|
+
var timeConstants_exports = {};
|
|
22
|
+
__export(timeConstants_exports, {
|
|
23
|
+
HOURS_PER_DAY: () => HOURS_PER_DAY,
|
|
24
|
+
HOURS_PER_WEEK: () => HOURS_PER_WEEK,
|
|
25
|
+
MINUTES_PER_DAY: () => MINUTES_PER_DAY,
|
|
26
|
+
MINUTES_PER_HOUR: () => MINUTES_PER_HOUR,
|
|
27
|
+
MINUTES_PER_WEEK: () => MINUTES_PER_WEEK,
|
|
28
|
+
MS_PER_DAY: () => MS_PER_DAY,
|
|
29
|
+
MS_PER_HOUR: () => MS_PER_HOUR,
|
|
30
|
+
MS_PER_MINUTE: () => MS_PER_MINUTE,
|
|
31
|
+
MS_PER_SECOND: () => MS_PER_SECOND,
|
|
32
|
+
MS_PER_WEEK: () => MS_PER_WEEK,
|
|
33
|
+
SECONDS_PER_DAY: () => SECONDS_PER_DAY,
|
|
34
|
+
SECONDS_PER_HOUR: () => SECONDS_PER_HOUR,
|
|
35
|
+
SECONDS_PER_MINUTE: () => SECONDS_PER_MINUTE,
|
|
36
|
+
SECONDS_PER_WEEK: () => SECONDS_PER_WEEK
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(timeConstants_exports);
|
|
39
|
+
var MS_PER_SECOND = 1e3;
|
|
40
|
+
var MS_PER_MINUTE = 6e4;
|
|
41
|
+
var MS_PER_HOUR = 36e5;
|
|
42
|
+
var MS_PER_DAY = 864e5;
|
|
43
|
+
var MS_PER_WEEK = 6048e5;
|
|
44
|
+
var SECONDS_PER_MINUTE = 60;
|
|
45
|
+
var SECONDS_PER_HOUR = 3600;
|
|
46
|
+
var SECONDS_PER_DAY = 86400;
|
|
47
|
+
var SECONDS_PER_WEEK = 604800;
|
|
48
|
+
var MINUTES_PER_HOUR = 60;
|
|
49
|
+
var MINUTES_PER_DAY = 1440;
|
|
50
|
+
var MINUTES_PER_WEEK = 10080;
|
|
51
|
+
var HOURS_PER_DAY = 24;
|
|
52
|
+
var HOURS_PER_WEEK = 168;
|
|
53
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
54
|
+
0 && (module.exports = {
|
|
55
|
+
HOURS_PER_DAY,
|
|
56
|
+
HOURS_PER_WEEK,
|
|
57
|
+
MINUTES_PER_DAY,
|
|
58
|
+
MINUTES_PER_HOUR,
|
|
59
|
+
MINUTES_PER_WEEK,
|
|
60
|
+
MS_PER_DAY,
|
|
61
|
+
MS_PER_HOUR,
|
|
62
|
+
MS_PER_MINUTE,
|
|
63
|
+
MS_PER_SECOND,
|
|
64
|
+
MS_PER_WEEK,
|
|
65
|
+
SECONDS_PER_DAY,
|
|
66
|
+
SECONDS_PER_HOUR,
|
|
67
|
+
SECONDS_PER_MINUTE,
|
|
68
|
+
SECONDS_PER_WEEK
|
|
69
|
+
});
|
|
70
|
+
//# sourceMappingURL=timeConstants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/timeConstants.ts"],"sourcesContent":["/**\n * Note that month and year do not have fixed durations, and hence are excluded\n * from this file.\n */\n\nexport const MS_PER_SECOND = 1000;\nexport const MS_PER_MINUTE = 60_000;\nexport const MS_PER_HOUR = 3_600_000;\nexport const MS_PER_DAY = 86_400_000;\nexport const MS_PER_WEEK = 604_800_000;\n\nexport const SECONDS_PER_MINUTE = 60;\nexport const SECONDS_PER_HOUR = 3_600;\nexport const SECONDS_PER_DAY = 86_400;\nexport const SECONDS_PER_WEEK = 604_800;\n\nexport const MINUTES_PER_HOUR = 60;\nexport const MINUTES_PER_DAY = 1440;\nexport const MINUTES_PER_WEEK = 10_080;\n\nexport const HOURS_PER_DAY = 24;\nexport const HOURS_PER_WEEK = 168;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAKO,IAAM,gBAAgB;AACtB,IAAM,gBAAgB;AACtB,IAAM,cAAc;AACpB,IAAM,aAAa;AACnB,IAAM,cAAc;AAEpB,IAAM,qBAAqB;AAC3B,IAAM,mBAAmB;AACzB,IAAM,kBAAkB;AACxB,IAAM,mBAAmB;AAEzB,IAAM,mBAAmB;AACzB,IAAM,kBAAkB;AACxB,IAAM,mBAAmB;AAEzB,IAAM,gBAAgB;AACtB,IAAM,iBAAiB;","names":[]}
|