@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/arrayBuffer.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encode an input ArrayBuffer into a hex string.
|
|
3
|
+
*/
|
|
4
|
+
export declare function arrayBufferToHex(buffer: ArrayBuffer): string;
|
|
5
|
+
/**
|
|
6
|
+
* Encode an input ArrayBuffer into a base64 string.
|
|
7
|
+
*/
|
|
8
|
+
export declare function arrayBufferToBase64(buffer: ArrayBuffer): string;
|
package/arrayBuffer.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
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/arrayBuffer.ts
|
|
21
|
+
var arrayBuffer_exports = {};
|
|
22
|
+
__export(arrayBuffer_exports, {
|
|
23
|
+
arrayBufferToBase64: () => arrayBufferToBase64,
|
|
24
|
+
arrayBufferToHex: () => arrayBufferToHex
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(arrayBuffer_exports);
|
|
27
|
+
function arrayBufferToHex(buffer) {
|
|
28
|
+
const byteArray = new Uint8Array(buffer);
|
|
29
|
+
return Array.from(byteArray).map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
30
|
+
}
|
|
31
|
+
function arrayBufferToBase64(buffer) {
|
|
32
|
+
const byteArray = new Uint8Array(buffer);
|
|
33
|
+
const binaryString = Array.from(byteArray).map((byte) => String.fromCodePoint(byte)).join("");
|
|
34
|
+
return btoa(binaryString);
|
|
35
|
+
}
|
|
36
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
37
|
+
0 && (module.exports = {
|
|
38
|
+
arrayBufferToBase64,
|
|
39
|
+
arrayBufferToHex
|
|
40
|
+
});
|
|
41
|
+
//# sourceMappingURL=arrayBuffer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/arrayBuffer.ts"],"sourcesContent":["/**\n * Encode an input ArrayBuffer into a hex string.\n */\nexport function arrayBufferToHex(buffer: ArrayBuffer): string {\n // Create a Uint8Array view of the ArrayBuffer\n const byteArray = new Uint8Array(buffer);\n\n // Convert each byte to a two-character hexadecimal string\n return Array.from(byteArray)\n .map((byte) => byte.toString(16).padStart(2, \"0\"))\n .join(\"\");\n}\n\n/**\n * Encode an input ArrayBuffer into a base64 string.\n */\nexport function arrayBufferToBase64(buffer: ArrayBuffer): string {\n // Convert the ArrayBuffer to a Uint8Array\n const byteArray = new Uint8Array(buffer);\n\n // Create a binary string from the byte array\n const binaryString = Array.from(byteArray)\n .map((byte) => String.fromCodePoint(byte))\n .join(\"\");\n\n // Encode the binary string to base64. No need to use safeBtoa because we\n // already simplified the binary input above.\n return btoa(binaryString);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGO,SAAS,iBAAiB,QAA6B;AAE5D,QAAM,YAAY,IAAI,WAAW,MAAM;AAGvC,SAAO,MAAM,KAAK,SAAS,EACxB,IAAI,CAAC,SAAS,KAAK,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG,CAAC,EAChD,KAAK,EAAE;AACZ;AAKO,SAAS,oBAAoB,QAA6B;AAE/D,QAAM,YAAY,IAAI,WAAW,MAAM;AAGvC,QAAM,eAAe,MAAM,KAAK,SAAS,EACtC,IAAI,CAAC,SAAS,OAAO,cAAc,IAAI,CAAC,EACxC,KAAK,EAAE;AAIV,SAAO,KAAK,YAAY;AAC1B;","names":[]}
|
package/dateTimeStr.d.ts
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
export declare
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
static utc(dt?: Date): string;
|
|
10
|
-
/** Default full local date+time string with full & concise info. */
|
|
11
|
-
static get now(): string;
|
|
12
|
-
/** Default full UTC date+time string with full & concise info. */
|
|
13
|
-
static get utcNow(): string;
|
|
14
|
-
}
|
|
1
|
+
export declare function yyyyMmDd(dt?: Date, separator?: string): string;
|
|
2
|
+
export declare function hhMmSs(dt?: Date, separator?: string): string;
|
|
3
|
+
export declare function hhMmSsMs(dt?: Date, timeSeparator?: string, msSeparator?: string): string;
|
|
4
|
+
export declare function tzShort(dt?: Date): string;
|
|
5
|
+
export declare function getLongMonthNameZeroIndexed(month: number, locales?: Intl.LocalesArgument): string;
|
|
6
|
+
export declare function getLongMonthNameOneIndexed(month: number, locales?: Intl.LocalesArgument): string;
|
|
7
|
+
export declare function getShortMonthNameZeroIndexed(month: number, locales?: Intl.LocalesArgument): string;
|
|
8
|
+
export declare function getShortMonthNameOneIndexed(month: number, locales?: Intl.LocalesArgument): string;
|
package/dateTimeStr.js
CHANGED
|
@@ -20,52 +20,64 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/dateTimeStr.ts
|
|
21
21
|
var dateTimeStr_exports = {};
|
|
22
22
|
__export(dateTimeStr_exports, {
|
|
23
|
-
|
|
23
|
+
getLongMonthNameOneIndexed: () => getLongMonthNameOneIndexed,
|
|
24
|
+
getLongMonthNameZeroIndexed: () => getLongMonthNameZeroIndexed,
|
|
25
|
+
getShortMonthNameOneIndexed: () => getShortMonthNameOneIndexed,
|
|
26
|
+
getShortMonthNameZeroIndexed: () => getShortMonthNameZeroIndexed,
|
|
27
|
+
hhMmSs: () => hhMmSs,
|
|
28
|
+
hhMmSsMs: () => hhMmSsMs,
|
|
29
|
+
tzShort: () => tzShort,
|
|
30
|
+
yyyyMmDd: () => yyyyMmDd
|
|
24
31
|
});
|
|
25
32
|
module.exports = __toCommonJS(dateTimeStr_exports);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
33
|
+
function yyyyMmDd(dt = /* @__PURE__ */ new Date(), separator = "-") {
|
|
34
|
+
const yr = dt.getFullYear();
|
|
35
|
+
const mth = dt.getMonth() + 1;
|
|
36
|
+
const day = dt.getDate();
|
|
37
|
+
return yr + separator + (mth < 10 ? "0" + mth : mth) + separator + (day < 10 ? "0" + day : day);
|
|
38
|
+
}
|
|
39
|
+
function hhMmSs(dt = /* @__PURE__ */ new Date(), separator = ":") {
|
|
40
|
+
const hr = dt.getHours();
|
|
41
|
+
const min = dt.getMinutes();
|
|
42
|
+
const sec = dt.getSeconds();
|
|
43
|
+
return (hr < 10 ? "0" + hr : hr) + separator + (min < 10 ? "0" + min : min) + separator + (sec < 10 ? "0" + sec : sec);
|
|
44
|
+
}
|
|
45
|
+
function hhMmSsMs(dt = /* @__PURE__ */ new Date(), timeSeparator = ":", msSeparator = ".") {
|
|
46
|
+
const ms = dt.getMilliseconds();
|
|
47
|
+
return hhMmSs(dt, timeSeparator) + msSeparator + (ms < 10 ? "00" + ms : ms < 100 ? "0" + ms : ms);
|
|
48
|
+
}
|
|
49
|
+
function tzShort(dt = /* @__PURE__ */ new Date()) {
|
|
50
|
+
if (dt.getTimezoneOffset() === 0) {
|
|
51
|
+
return "Z";
|
|
32
52
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
return _DateTimeStr.yyyyMmDd(dt, dateSeparator) + dtSeparator + _DateTimeStr.hhMmSsMs(dt, timeSeparator, msSeparator) + _DateTimeStr.tz(dt);
|
|
53
|
-
}
|
|
54
|
-
/** Use the default ISO string function to keep things simple here. */
|
|
55
|
-
static utc(dt = /* @__PURE__ */ new Date()) {
|
|
56
|
-
return dt.toISOString();
|
|
57
|
-
}
|
|
58
|
-
/** Default full local date+time string with full & concise info. */
|
|
59
|
-
static get now() {
|
|
60
|
-
return _DateTimeStr.local();
|
|
61
|
-
}
|
|
62
|
-
/** Default full UTC date+time string with full & concise info. */
|
|
63
|
-
static get utcNow() {
|
|
64
|
-
return _DateTimeStr.utc();
|
|
65
|
-
}
|
|
66
|
-
};
|
|
53
|
+
const tzHours = dt.getTimezoneOffset() / 60;
|
|
54
|
+
return tzHours >= 0 ? "+" + tzHours : String(tzHours);
|
|
55
|
+
}
|
|
56
|
+
function getLongMonthNameZeroIndexed(month, locales = "default") {
|
|
57
|
+
return new Date(2024, month, 15).toLocaleString(locales, {
|
|
58
|
+
month: "long"
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
function getLongMonthNameOneIndexed(month, locales = "default") {
|
|
62
|
+
return getLongMonthNameZeroIndexed(month - 1, locales);
|
|
63
|
+
}
|
|
64
|
+
function getShortMonthNameZeroIndexed(month, locales = "default") {
|
|
65
|
+
return new Date(2e3, month, 15).toLocaleString(locales, {
|
|
66
|
+
month: "short"
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
function getShortMonthNameOneIndexed(month, locales = "default") {
|
|
70
|
+
return getShortMonthNameZeroIndexed(month - 1, locales);
|
|
71
|
+
}
|
|
67
72
|
// Annotate the CommonJS export names for ESM import in node:
|
|
68
73
|
0 && (module.exports = {
|
|
69
|
-
|
|
74
|
+
getLongMonthNameOneIndexed,
|
|
75
|
+
getLongMonthNameZeroIndexed,
|
|
76
|
+
getShortMonthNameOneIndexed,
|
|
77
|
+
getShortMonthNameZeroIndexed,
|
|
78
|
+
hhMmSs,
|
|
79
|
+
hhMmSsMs,
|
|
80
|
+
tzShort,
|
|
81
|
+
yyyyMmDd
|
|
70
82
|
});
|
|
71
83
|
//# sourceMappingURL=dateTimeStr.js.map
|
package/dateTimeStr.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/dateTimeStr.ts"],"sourcesContent":["export
|
|
1
|
+
{"version":3,"sources":["../src/dateTimeStr.ts"],"sourcesContent":["export function yyyyMmDd(dt = new Date(), separator = \"-\"): string {\n const yr = dt.getFullYear();\n const mth = dt.getMonth() + 1;\n const day = dt.getDate();\n\n return (\n yr +\n separator +\n (mth < 10 ? \"0\" + mth : mth) +\n separator +\n (day < 10 ? \"0\" + day : day)\n );\n}\n\nexport function hhMmSs(dt = new Date(), separator = \":\"): string {\n const hr = dt.getHours();\n const min = dt.getMinutes();\n const sec = dt.getSeconds();\n\n return (\n (hr < 10 ? \"0\" + hr : hr) +\n separator +\n (min < 10 ? \"0\" + min : min) +\n separator +\n (sec < 10 ? \"0\" + sec : sec)\n );\n}\n\nexport function hhMmSsMs(\n dt = new Date(),\n timeSeparator = \":\",\n msSeparator = \".\",\n): string {\n const ms = dt.getMilliseconds();\n\n return (\n hhMmSs(dt, timeSeparator) +\n msSeparator +\n (ms < 10 ? \"00\" + ms : ms < 100 ? \"0\" + ms : ms)\n );\n}\n\nexport function tzShort(dt = new Date()): string {\n if (dt.getTimezoneOffset() === 0) {\n return \"Z\";\n }\n\n const tzHours = dt.getTimezoneOffset() / 60;\n return tzHours >= 0 ? \"+\" + tzHours : String(tzHours);\n}\n\nexport function getLongMonthNameZeroIndexed(\n month: number,\n locales: Intl.LocalesArgument = \"default\",\n): string {\n return new Date(2024, month, 15).toLocaleString(locales, {\n month: \"long\",\n });\n}\n\nexport function getLongMonthNameOneIndexed(\n month: number,\n locales: Intl.LocalesArgument = \"default\",\n): string {\n return getLongMonthNameZeroIndexed(month - 1, locales);\n}\n\nexport function getShortMonthNameZeroIndexed(\n month: number,\n locales: Intl.LocalesArgument = \"default\",\n): string {\n return new Date(2000, month, 15).toLocaleString(locales, {\n month: \"short\",\n });\n}\n\nexport function getShortMonthNameOneIndexed(\n month: number,\n locales: Intl.LocalesArgument = \"default\",\n): string {\n return getShortMonthNameZeroIndexed(month - 1, locales);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;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OAAO,EAAE,EAAE,eAAe,SAAS;AAAA,IACvD,OAAO;AAAA,EACT,CAAC;AACH;AAEO,SAAS,4BACd,OACA,UAAgC,WACxB;AACR,SAAO,6BAA6B,QAAQ,GAAG,OAAO;AACxD;","names":[]}
|
package/duration.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bunch of miscellaneous constants and utility functions related to handling
|
|
3
|
+
* date and time durations.
|
|
4
|
+
*
|
|
5
|
+
* Note that month and year do not have fixed durations, and hence are excluded
|
|
6
|
+
* from this file.
|
|
7
|
+
*/
|
|
8
|
+
export type Duration = {
|
|
9
|
+
days?: number;
|
|
10
|
+
hours?: number;
|
|
11
|
+
minutes?: number;
|
|
12
|
+
seconds?: number;
|
|
13
|
+
milliseconds?: number;
|
|
14
|
+
};
|
|
15
|
+
export type DurationType = keyof Duration;
|
|
16
|
+
export declare const DURATION_TYPE_SEQUENCE: DurationType[];
|
|
17
|
+
/**
|
|
18
|
+
* Follows the same format as Intl.DurationFormat.prototype.format().
|
|
19
|
+
*
|
|
20
|
+
* Short: 1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns
|
|
21
|
+
* Long: 1 year, 2 months, 3 weeks, 3 days, 4 hours, 5 minutes, 6 seconds,
|
|
22
|
+
* 7 milliseconds, 8 microseconds, 9 nanoseconds
|
|
23
|
+
* Narrow: 1y 2mo 3w 3d 4h 5m 6s 7ms 8μs 9ns
|
|
24
|
+
*/
|
|
25
|
+
export type DurationStyle = "short" | "long" | "narrow";
|
|
26
|
+
export type DurationSuffixMap = {
|
|
27
|
+
short: string;
|
|
28
|
+
shorts: string;
|
|
29
|
+
long: string;
|
|
30
|
+
longs: string;
|
|
31
|
+
narrow: string;
|
|
32
|
+
};
|
|
33
|
+
export type DurationSuffixType = keyof DurationSuffixMap;
|
|
34
|
+
export declare const DURATION_STYLE_SUFFIX_MAP: Record<DurationType, DurationSuffixMap>;
|
|
35
|
+
/**
|
|
36
|
+
* Convert a milliseconds duration into a Duration object. If the given ms is
|
|
37
|
+
* zero, then return an object with a single field of zero.
|
|
38
|
+
*
|
|
39
|
+
* durationTypeForZero - Defaults to 'milliseconds'
|
|
40
|
+
*/
|
|
41
|
+
export declare function msToDuration(ms: number, durationTypeForZero?: DurationType): Duration;
|
|
42
|
+
/**
|
|
43
|
+
* Returns the number of milliseconds for the given duration.
|
|
44
|
+
*/
|
|
45
|
+
export declare function durationToMs(duration: Duration): number;
|
|
46
|
+
/**
|
|
47
|
+
* Format a given Duration object into a string. If the object has no fields,
|
|
48
|
+
* then returns an empty string.
|
|
49
|
+
*
|
|
50
|
+
* style - Defaults to 'short'
|
|
51
|
+
*/
|
|
52
|
+
export declare function formatDuration(duration: Duration, style?: DurationStyle): string;
|
|
53
|
+
/**
|
|
54
|
+
* Convert a millisecond duration into a human-readable duration string.
|
|
55
|
+
*
|
|
56
|
+
* options.durationTypeForZero - Defaults to 'milliseconds'
|
|
57
|
+
* options.style - Defaults to 'short'
|
|
58
|
+
*/
|
|
59
|
+
export declare function readableDuration(ms: number, options?: {
|
|
60
|
+
durationTypeForZero?: DurationType;
|
|
61
|
+
style?: DurationStyle;
|
|
62
|
+
}): string;
|
package/duration.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
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/duration.ts
|
|
21
|
+
var duration_exports = {};
|
|
22
|
+
__export(duration_exports, {
|
|
23
|
+
DURATION_STYLE_SUFFIX_MAP: () => DURATION_STYLE_SUFFIX_MAP,
|
|
24
|
+
DURATION_TYPE_SEQUENCE: () => DURATION_TYPE_SEQUENCE,
|
|
25
|
+
durationToMs: () => durationToMs,
|
|
26
|
+
formatDuration: () => formatDuration,
|
|
27
|
+
msToDuration: () => msToDuration,
|
|
28
|
+
readableDuration: () => readableDuration
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(duration_exports);
|
|
31
|
+
|
|
32
|
+
// src/timeConstants.ts
|
|
33
|
+
var MS_PER_SECOND = 1e3;
|
|
34
|
+
var MS_PER_MINUTE = 6e4;
|
|
35
|
+
var MS_PER_HOUR = 36e5;
|
|
36
|
+
var MS_PER_DAY = 864e5;
|
|
37
|
+
var SECONDS_PER_MINUTE = 60;
|
|
38
|
+
var MINUTES_PER_HOUR = 60;
|
|
39
|
+
var HOURS_PER_DAY = 24;
|
|
40
|
+
|
|
41
|
+
// src/duration.ts
|
|
42
|
+
var DURATION_TYPE_SEQUENCE = [
|
|
43
|
+
"days",
|
|
44
|
+
"hours",
|
|
45
|
+
"minutes",
|
|
46
|
+
"seconds",
|
|
47
|
+
"milliseconds"
|
|
48
|
+
];
|
|
49
|
+
var DURATION_STYLE_SUFFIX_MAP = {
|
|
50
|
+
days: {
|
|
51
|
+
short: "day",
|
|
52
|
+
shorts: "days",
|
|
53
|
+
long: "day",
|
|
54
|
+
longs: "days",
|
|
55
|
+
narrow: "d"
|
|
56
|
+
},
|
|
57
|
+
hours: {
|
|
58
|
+
short: "hr",
|
|
59
|
+
shorts: "hrs",
|
|
60
|
+
long: "hour",
|
|
61
|
+
longs: "hours",
|
|
62
|
+
narrow: "h"
|
|
63
|
+
},
|
|
64
|
+
minutes: {
|
|
65
|
+
short: "min",
|
|
66
|
+
shorts: "mins",
|
|
67
|
+
long: "minute",
|
|
68
|
+
longs: "minutes",
|
|
69
|
+
narrow: "m"
|
|
70
|
+
},
|
|
71
|
+
seconds: {
|
|
72
|
+
short: "sec",
|
|
73
|
+
shorts: "secs",
|
|
74
|
+
long: "second",
|
|
75
|
+
longs: "seconds",
|
|
76
|
+
narrow: "s"
|
|
77
|
+
},
|
|
78
|
+
milliseconds: {
|
|
79
|
+
short: "ms",
|
|
80
|
+
shorts: "ms",
|
|
81
|
+
long: "millisecond",
|
|
82
|
+
longs: "milliseconds",
|
|
83
|
+
narrow: "ms"
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
function getDurationStyleForPlural(style) {
|
|
87
|
+
return style == "short" ? "shorts" : style === "long" ? "longs" : style;
|
|
88
|
+
}
|
|
89
|
+
function getValueAndUnitSeparator(style) {
|
|
90
|
+
return style === "narrow" ? "" : " ";
|
|
91
|
+
}
|
|
92
|
+
function getDurationTypeSeparator(style) {
|
|
93
|
+
return style === "narrow" ? " " : ", ";
|
|
94
|
+
}
|
|
95
|
+
function msToDuration(ms, durationTypeForZero) {
|
|
96
|
+
if (ms === 0) {
|
|
97
|
+
durationTypeForZero = durationTypeForZero != null ? durationTypeForZero : "milliseconds";
|
|
98
|
+
return { [durationTypeForZero]: 0 };
|
|
99
|
+
}
|
|
100
|
+
const duration = {};
|
|
101
|
+
for (let i = 0; i < 1; i++) {
|
|
102
|
+
let seconds = Math.floor(ms / MS_PER_SECOND);
|
|
103
|
+
const millis = ms - seconds * MS_PER_SECOND;
|
|
104
|
+
if (millis > 0) {
|
|
105
|
+
duration["milliseconds"] = millis;
|
|
106
|
+
}
|
|
107
|
+
if (seconds === 0) {
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
let minutes = Math.floor(seconds / SECONDS_PER_MINUTE);
|
|
111
|
+
seconds -= minutes * SECONDS_PER_MINUTE;
|
|
112
|
+
if (seconds > 0) {
|
|
113
|
+
duration["seconds"] = seconds;
|
|
114
|
+
}
|
|
115
|
+
if (minutes === 0) {
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
let hours = Math.floor(minutes / MINUTES_PER_HOUR);
|
|
119
|
+
minutes -= hours * MINUTES_PER_HOUR;
|
|
120
|
+
if (minutes > 0) {
|
|
121
|
+
duration["minutes"] = minutes;
|
|
122
|
+
}
|
|
123
|
+
if (hours === 0) {
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
const days = Math.floor(hours / HOURS_PER_DAY);
|
|
127
|
+
hours -= days * HOURS_PER_DAY;
|
|
128
|
+
if (hours > 0) {
|
|
129
|
+
duration["hours"] = hours;
|
|
130
|
+
}
|
|
131
|
+
if (days > 0) {
|
|
132
|
+
duration["days"] = days;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return duration;
|
|
136
|
+
}
|
|
137
|
+
function durationToMs(duration) {
|
|
138
|
+
var _a, _b, _c, _d, _e;
|
|
139
|
+
const daysMs = ((_a = duration.days) != null ? _a : 0) * MS_PER_DAY;
|
|
140
|
+
const hoursMs = ((_b = duration.hours) != null ? _b : 0) * MS_PER_HOUR;
|
|
141
|
+
const minsMs = ((_c = duration.minutes) != null ? _c : 0) * MS_PER_MINUTE;
|
|
142
|
+
const secsMs = ((_d = duration.seconds) != null ? _d : 0) * MS_PER_SECOND;
|
|
143
|
+
const msMs = (_e = duration.milliseconds) != null ? _e : 0;
|
|
144
|
+
return daysMs + hoursMs + minsMs + secsMs + msMs;
|
|
145
|
+
}
|
|
146
|
+
function formatDuration(duration, style) {
|
|
147
|
+
style = style != null ? style : "short";
|
|
148
|
+
const stylePlural = getDurationStyleForPlural(style);
|
|
149
|
+
const space = getValueAndUnitSeparator(style);
|
|
150
|
+
const a = [];
|
|
151
|
+
for (const unit of DURATION_TYPE_SEQUENCE) {
|
|
152
|
+
const value = duration[unit];
|
|
153
|
+
if (value === void 0) continue;
|
|
154
|
+
const suffixMap = DURATION_STYLE_SUFFIX_MAP[unit];
|
|
155
|
+
const suffix = value === 1 ? suffixMap[style] : suffixMap[stylePlural];
|
|
156
|
+
a.push(value + space + suffix);
|
|
157
|
+
}
|
|
158
|
+
const separator = getDurationTypeSeparator(style);
|
|
159
|
+
return a.join(separator);
|
|
160
|
+
}
|
|
161
|
+
function readableDuration(ms, options) {
|
|
162
|
+
const duration = msToDuration(ms, options == null ? void 0 : options.durationTypeForZero);
|
|
163
|
+
return formatDuration(duration, options == null ? void 0 : options.style);
|
|
164
|
+
}
|
|
165
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
166
|
+
0 && (module.exports = {
|
|
167
|
+
DURATION_STYLE_SUFFIX_MAP,
|
|
168
|
+
DURATION_TYPE_SEQUENCE,
|
|
169
|
+
durationToMs,
|
|
170
|
+
formatDuration,
|
|
171
|
+
msToDuration,
|
|
172
|
+
readableDuration
|
|
173
|
+
});
|
|
174
|
+
//# sourceMappingURL=duration.js.map
|
package/duration.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/duration.ts","../src/timeConstants.ts"],"sourcesContent":["/**\n * Bunch of miscellaneous constants and utility functions related to handling\n * date and time durations.\n *\n * Note that month and year do not have fixed durations, and hence are excluded\n * from this file.\n */\n\nimport {\n MS_PER_SECOND,\n SECONDS_PER_MINUTE,\n MINUTES_PER_HOUR,\n HOURS_PER_DAY,\n MS_PER_DAY,\n MS_PER_MINUTE,\n MS_PER_HOUR,\n} from \"./timeConstants\";\n\nexport type Duration = {\n days?: number;\n hours?: number;\n minutes?: number;\n seconds?: number;\n milliseconds?: number;\n};\n\nexport type DurationType = keyof Duration;\n\nexport const DURATION_TYPE_SEQUENCE: DurationType[] = [\n \"days\",\n \"hours\",\n \"minutes\",\n \"seconds\",\n \"milliseconds\",\n];\n\n/**\n * Follows the same format as Intl.DurationFormat.prototype.format().\n *\n * Short: 1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns\n * Long: 1 year, 2 months, 3 weeks, 3 days, 4 hours, 5 minutes, 6 seconds,\n * 7 milliseconds, 8 microseconds, 9 nanoseconds\n * Narrow: 1y 2mo 3w 3d 4h 5m 6s 7ms 8μs 9ns\n */\nexport type DurationStyle = \"short\" | \"long\" | \"narrow\";\n\nexport type DurationSuffixMap = {\n short: string;\n shorts: string;\n long: string;\n longs: string;\n narrow: string;\n};\n\nexport type DurationSuffixType = keyof DurationSuffixMap;\n\nexport const DURATION_STYLE_SUFFIX_MAP: Record<\n DurationType,\n DurationSuffixMap\n> = {\n days: {\n short: \"day\",\n shorts: \"days\",\n long: \"day\",\n longs: \"days\",\n narrow: \"d\",\n },\n hours: {\n short: \"hr\",\n shorts: \"hrs\",\n long: \"hour\",\n longs: \"hours\",\n narrow: \"h\",\n },\n minutes: {\n short: \"min\",\n shorts: \"mins\",\n long: \"minute\",\n longs: \"minutes\",\n narrow: \"m\",\n },\n seconds: {\n short: \"sec\",\n shorts: \"secs\",\n long: \"second\",\n longs: \"seconds\",\n narrow: \"s\",\n },\n milliseconds: {\n short: \"ms\",\n shorts: \"ms\",\n long: \"millisecond\",\n longs: \"milliseconds\",\n narrow: \"ms\",\n },\n};\n\nfunction getDurationStyleForPlural(style: DurationStyle): DurationSuffixType {\n return style == \"short\" ? \"shorts\" : style === \"long\" ? \"longs\" : style;\n}\n\nfunction getValueAndUnitSeparator(style: DurationStyle): string {\n return style === \"narrow\" ? \"\" : \" \";\n}\n\nfunction getDurationTypeSeparator(style: DurationStyle): string {\n return style === \"narrow\" ? \" \" : \", \";\n}\n\n/**\n * Convert a milliseconds duration into a Duration object. If the given ms is\n * zero, then return an object with a single field of zero.\n *\n * durationTypeForZero - Defaults to 'milliseconds'\n */\nexport function msToDuration(\n ms: number,\n durationTypeForZero?: DurationType,\n): Duration {\n if (ms === 0) {\n durationTypeForZero = durationTypeForZero ?? \"milliseconds\";\n return { [durationTypeForZero]: 0 };\n }\n\n const duration: Duration = {};\n\n for (let i = 0; i < 1; i++) {\n let seconds = Math.floor(ms / MS_PER_SECOND);\n const millis = ms - seconds * MS_PER_SECOND;\n\n if (millis > 0) {\n duration[\"milliseconds\"] = millis;\n }\n\n if (seconds === 0) {\n break;\n }\n\n let minutes = Math.floor(seconds / SECONDS_PER_MINUTE);\n seconds -= minutes * SECONDS_PER_MINUTE;\n\n if (seconds > 0) {\n duration[\"seconds\"] = seconds;\n }\n\n if (minutes === 0) {\n break;\n }\n\n let hours = Math.floor(minutes / MINUTES_PER_HOUR);\n minutes -= hours * MINUTES_PER_HOUR;\n\n if (minutes > 0) {\n duration[\"minutes\"] = minutes;\n }\n\n if (hours === 0) {\n break;\n }\n\n const days = Math.floor(hours / HOURS_PER_DAY);\n hours -= days * HOURS_PER_DAY;\n\n if (hours > 0) {\n duration[\"hours\"] = hours;\n }\n\n if (days > 0) {\n duration[\"days\"] = days;\n }\n }\n\n return duration;\n}\n\n/**\n * Returns the number of milliseconds for the given duration.\n */\nexport function durationToMs(duration: Duration): number {\n const daysMs = (duration.days ?? 0) * MS_PER_DAY;\n const hoursMs = (duration.hours ?? 0) * MS_PER_HOUR;\n const minsMs = (duration.minutes ?? 0) * MS_PER_MINUTE;\n const secsMs = (duration.seconds ?? 0) * MS_PER_SECOND;\n const msMs = duration.milliseconds ?? 0;\n\n return daysMs + hoursMs + minsMs + secsMs + msMs;\n}\n\n/**\n * Format a given Duration object into a string. If the object has no fields,\n * then returns an empty string.\n *\n * style - Defaults to 'short'\n */\nexport function formatDuration(duration: Duration, style?: DurationStyle) {\n style = style ?? \"short\";\n const stylePlural = getDurationStyleForPlural(style);\n\n const space = getValueAndUnitSeparator(style);\n\n const a: string[] = [];\n\n for (const unit of DURATION_TYPE_SEQUENCE) {\n const value = duration[unit];\n if (value === undefined) continue;\n\n const suffixMap = DURATION_STYLE_SUFFIX_MAP[unit];\n const suffix = value === 1 ? suffixMap[style] : suffixMap[stylePlural];\n a.push(value + space + suffix);\n }\n\n const separator = getDurationTypeSeparator(style);\n return a.join(separator);\n}\n\n/**\n * Convert a millisecond duration into a human-readable duration string.\n *\n * options.durationTypeForZero - Defaults to 'milliseconds'\n * options.style - Defaults to 'short'\n */\nexport function readableDuration(\n ms: number,\n options?: { durationTypeForZero?: DurationType; style?: DurationStyle },\n): string {\n const duration = msToDuration(ms, options?.durationTypeForZero);\n\n return formatDuration(duration, options?.style);\n}\n","/**\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;;;ACKO,IAAM,gBAAgB;AACtB,IAAM,gBAAgB;AACtB,IAAM,cAAc;AACpB,IAAM,aAAa;AAGnB,IAAM,qBAAqB;AAK3B,IAAM,mBAAmB;AAIzB,IAAM,gBAAgB;;;ADQtB,IAAM,yBAAyC;AAAA,EACpD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAsBO,IAAM,4BAGT;AAAA,EACF,MAAM;AAAA,IACJ,OAAO;AAAA,IACP,QAAQ;AAAA,IACR,MAAM;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,UAAU,mCAAS,KAAK;AAChD;","names":[]}
|
package/localStorageCache.d.ts
CHANGED
|
@@ -1,18 +1,38 @@
|
|
|
1
|
+
import { Duration } from "./duration";
|
|
1
2
|
/**
|
|
2
3
|
* Simple local storage cache with support for auto-expiration.
|
|
3
4
|
* Note that this works in the browser context only because nodejs does not
|
|
4
5
|
* have local storage.
|
|
6
|
+
*
|
|
7
|
+
* Create a cache item accessor object with auto-expiration. The value will
|
|
8
|
+
* always be stored as a string by applying JSON.stringify(), and will be
|
|
9
|
+
* returned in the same object type by applying JSON.parse().
|
|
10
|
+
*
|
|
11
|
+
* In order to provide proper type-checking, please always specify the T
|
|
12
|
+
* type parameter. E.g. const item = storeItem<string>("name", 10_000);
|
|
13
|
+
*
|
|
14
|
+
* expires - Either a number in milliseconds, or a Duration object
|
|
5
15
|
*/
|
|
6
|
-
export declare
|
|
7
|
-
|
|
8
|
-
static getValue<T>(key: string, logError?: boolean): T | undefined;
|
|
9
|
-
}
|
|
10
|
-
/** Same as above, but saves some config for reuse. */
|
|
11
|
-
export declare class LocalStorageCacheItem<T> {
|
|
16
|
+
export declare function storeItem<T>(key: string, expires: number | Duration, logError?: boolean, defaultValue?: T): CacheItem<T>;
|
|
17
|
+
declare class CacheItem<T> {
|
|
12
18
|
readonly key: string;
|
|
13
19
|
readonly expireDeltaMs: number;
|
|
14
20
|
readonly logError: boolean;
|
|
15
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Create a cache item accessor object with auto-expiration.
|
|
23
|
+
*/
|
|
24
|
+
constructor(key: string, expireDeltaMs: number, logError: boolean, defaultValue: T | undefined);
|
|
25
|
+
/**
|
|
26
|
+
* Set the value of this item with auto-expiration.
|
|
27
|
+
*/
|
|
16
28
|
set(value: T): void;
|
|
29
|
+
/**
|
|
30
|
+
* Get the value of this item, or undefined if value is not set or expired.
|
|
31
|
+
*/
|
|
17
32
|
get(): T | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Remove the value of this item.
|
|
35
|
+
*/
|
|
36
|
+
remove(): void;
|
|
18
37
|
}
|
|
38
|
+
export {};
|
package/localStorageCache.js
CHANGED
|
@@ -20,58 +20,89 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/localStorageCache.ts
|
|
21
21
|
var localStorageCache_exports = {};
|
|
22
22
|
__export(localStorageCache_exports, {
|
|
23
|
-
|
|
24
|
-
LocalStorageCacheItem: () => LocalStorageCacheItem
|
|
23
|
+
storeItem: () => storeItem
|
|
25
24
|
});
|
|
26
25
|
module.exports = __toCommonJS(localStorageCache_exports);
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
|
|
27
|
+
// src/timeConstants.ts
|
|
28
|
+
var MS_PER_SECOND = 1e3;
|
|
29
|
+
var MS_PER_MINUTE = 6e4;
|
|
30
|
+
var MS_PER_HOUR = 36e5;
|
|
31
|
+
var MS_PER_DAY = 864e5;
|
|
32
|
+
|
|
33
|
+
// src/duration.ts
|
|
34
|
+
function durationToMs(duration) {
|
|
35
|
+
var _a, _b, _c, _d, _e;
|
|
36
|
+
const daysMs = ((_a = duration.days) != null ? _a : 0) * MS_PER_DAY;
|
|
37
|
+
const hoursMs = ((_b = duration.hours) != null ? _b : 0) * MS_PER_HOUR;
|
|
38
|
+
const minsMs = ((_c = duration.minutes) != null ? _c : 0) * MS_PER_MINUTE;
|
|
39
|
+
const secsMs = ((_d = duration.seconds) != null ? _d : 0) * MS_PER_SECOND;
|
|
40
|
+
const msMs = (_e = duration.milliseconds) != null ? _e : 0;
|
|
41
|
+
return daysMs + hoursMs + minsMs + secsMs + msMs;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// src/localStorageCache.ts
|
|
45
|
+
function storeItem(key, expires, logError = true, defaultValue) {
|
|
46
|
+
const expireDeltaMs = typeof expires === "number" ? expires : durationToMs(expires);
|
|
47
|
+
return new CacheItem(key, expireDeltaMs, logError, defaultValue);
|
|
48
|
+
}
|
|
49
|
+
var CacheItem = class {
|
|
50
|
+
/**
|
|
51
|
+
* Create a cache item accessor object with auto-expiration.
|
|
52
|
+
*/
|
|
53
|
+
constructor(key, expireDeltaMs, logError, defaultValue) {
|
|
54
|
+
this.key = key;
|
|
55
|
+
this.expireDeltaMs = expireDeltaMs;
|
|
56
|
+
this.logError = logError;
|
|
57
|
+
if (defaultValue !== void 0) {
|
|
58
|
+
if (this.get() === void 0) {
|
|
59
|
+
this.set(defaultValue);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Set the value of this item with auto-expiration.
|
|
65
|
+
*/
|
|
66
|
+
set(value) {
|
|
67
|
+
const expireMs = Date.now() + this.expireDeltaMs;
|
|
30
68
|
const valueStr = JSON.stringify({ value, expireMs });
|
|
31
|
-
globalThis.localStorage.setItem(key, valueStr);
|
|
69
|
+
globalThis.localStorage.setItem(this.key, valueStr);
|
|
32
70
|
}
|
|
33
|
-
|
|
34
|
-
|
|
71
|
+
/**
|
|
72
|
+
* Get the value of this item, or undefined if value is not set or expired.
|
|
73
|
+
*/
|
|
74
|
+
get() {
|
|
75
|
+
const jsonStr = globalThis.localStorage.getItem(this.key);
|
|
35
76
|
if (!jsonStr || typeof jsonStr !== "string") {
|
|
36
77
|
return void 0;
|
|
37
78
|
}
|
|
38
79
|
try {
|
|
39
80
|
const obj = JSON.parse(jsonStr);
|
|
40
81
|
if (!obj || typeof obj !== "object" || !("value" in obj) || !("expireMs" in obj) || typeof obj.expireMs !== "number" || Date.now() >= obj.expireMs) {
|
|
41
|
-
globalThis.localStorage.removeItem(key);
|
|
82
|
+
globalThis.localStorage.removeItem(this.key);
|
|
42
83
|
return void 0;
|
|
43
84
|
}
|
|
44
85
|
return obj.value;
|
|
45
86
|
} catch (e) {
|
|
46
|
-
if (logError) {
|
|
47
|
-
console.error(
|
|
87
|
+
if (this.logError) {
|
|
88
|
+
console.error(
|
|
89
|
+
`Found invalid storage value: ${this.key}=${jsonStr}:`,
|
|
90
|
+
e
|
|
91
|
+
);
|
|
48
92
|
}
|
|
49
|
-
globalThis.localStorage.removeItem(key);
|
|
93
|
+
globalThis.localStorage.removeItem(this.key);
|
|
50
94
|
return void 0;
|
|
51
95
|
}
|
|
52
96
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
this.
|
|
58
|
-
this.logError = logError;
|
|
59
|
-
if (defaultValue !== void 0) {
|
|
60
|
-
if (this.get() === void 0) {
|
|
61
|
-
this.set(defaultValue);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
set(value) {
|
|
66
|
-
LocalStorageCache.setValue(this.key, value, this.expireDeltaMs);
|
|
67
|
-
}
|
|
68
|
-
get() {
|
|
69
|
-
return LocalStorageCache.getValue(this.key, this.logError);
|
|
97
|
+
/**
|
|
98
|
+
* Remove the value of this item.
|
|
99
|
+
*/
|
|
100
|
+
remove() {
|
|
101
|
+
globalThis.localStorage.removeItem(this.key);
|
|
70
102
|
}
|
|
71
103
|
};
|
|
72
104
|
// Annotate the CommonJS export names for ESM import in node:
|
|
73
105
|
0 && (module.exports = {
|
|
74
|
-
|
|
75
|
-
LocalStorageCacheItem
|
|
106
|
+
storeItem
|
|
76
107
|
});
|
|
77
108
|
//# sourceMappingURL=localStorageCache.js.map
|