@choksheak/ts-utils 0.1.9 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dateTimeStr.d.ts +60 -0
- package/dateTimeStr.js +15 -0
- package/dateTimeStr.js.map +1 -1
- package/duration.d.ts +14 -6
- package/duration.js.map +1 -1
- package/kvStore.d.ts +18 -1
- package/kvStore.js +31 -4
- package/kvStore.js.map +1 -1
- package/localStorageCache.d.ts +11 -1
- package/localStorageCache.js +9 -12
- package/localStorageCache.js.map +1 -1
- package/nonEmpty.d.ts +3 -0
- package/nonEmpty.js.map +1 -1
- package/nonNil.d.ts +3 -0
- package/nonNil.js.map +1 -1
- package/package.json +1 -1
- package/safeParseInt.d.ts +3 -0
- package/safeParseInt.js.map +1 -1
- package/sleep.d.ts +4 -0
- package/sleep.js.map +1 -1
- package/src/dateTimeStr.ts +73 -0
- package/src/duration.ts +14 -6
- package/src/kvStore.ts +38 -2
- package/src/localStorageCache.ts +20 -18
- package/src/nonEmpty.ts +3 -0
- package/src/nonNil.ts +3 -0
- package/src/safeParseInt.ts +3 -0
- package/src/sleep.ts +4 -0
package/dateTimeStr.d.ts
CHANGED
|
@@ -1,8 +1,68 @@
|
|
|
1
|
+
export type AnyDateTime = number | Date | string;
|
|
2
|
+
/**
|
|
3
|
+
* Convert a number (epoch milliseconds), string (parseable date/time), or
|
|
4
|
+
* Date object (no conversion) into a Date object.
|
|
5
|
+
*/
|
|
6
|
+
export declare function toDate(ts: AnyDateTime): Date;
|
|
7
|
+
/**
|
|
8
|
+
* Returns a date in yyyy-MM-dd format. E.g. '2000-01-02'.
|
|
9
|
+
*
|
|
10
|
+
* @param dt Specify a date object or default to the current date.
|
|
11
|
+
* @param separator Defaults to '-'.
|
|
12
|
+
*/
|
|
1
13
|
export declare function yyyyMmDd(dt?: Date, separator?: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* Returns a date in hh:mm:ss format. E.g. '01:02:03'.
|
|
16
|
+
*
|
|
17
|
+
* @param dt Specify a date object or default to the current date/time.
|
|
18
|
+
* @param separator Defaults to ':'.
|
|
19
|
+
*/
|
|
2
20
|
export declare function hhMmSs(dt?: Date, separator?: string): string;
|
|
21
|
+
/**
|
|
22
|
+
* Returns a date in hh:mm:ss.SSS format. E.g. '01:02:03.004'.
|
|
23
|
+
*
|
|
24
|
+
* @param dt Specify a date object or default to the current date/time.
|
|
25
|
+
* @param timeSeparator Separator for hh/mm/ss. Defaults to ':'.
|
|
26
|
+
* @param msSeparator Separator before SSS. Defaults to '.'.
|
|
27
|
+
*/
|
|
3
28
|
export declare function hhMmSsMs(dt?: Date, timeSeparator?: string, msSeparator?: string): string;
|
|
29
|
+
/**
|
|
30
|
+
* Returns the timezone string for the given date. E.g. '+8', '-3.5'.
|
|
31
|
+
* Returns 'Z' for UTC.
|
|
32
|
+
*
|
|
33
|
+
* @param dt Specify a date object or default to the current date/time.
|
|
34
|
+
*/
|
|
4
35
|
export declare function tzShort(dt?: Date): string;
|
|
36
|
+
/**
|
|
37
|
+
* Returns the long month name, zero-indexed. E.g. 0 for 'January'.
|
|
38
|
+
*
|
|
39
|
+
* @param month Zero-indexed month.
|
|
40
|
+
* @param locales Specify the locale, e.g. 'en-US', new Intl.Locale("en-US").
|
|
41
|
+
*/
|
|
5
42
|
export declare function getLongMonthNameZeroIndexed(month: number, locales?: Intl.LocalesArgument): string;
|
|
43
|
+
/**
|
|
44
|
+
* Returns the long month name, one-indexed. E.g. 1 for 'January'.
|
|
45
|
+
*
|
|
46
|
+
* @param month One-indexed month.
|
|
47
|
+
* @param locales Specify the locale, e.g. 'en-US', new Intl.Locale("en-US").
|
|
48
|
+
*/
|
|
6
49
|
export declare function getLongMonthNameOneIndexed(month: number, locales?: Intl.LocalesArgument): string;
|
|
50
|
+
/**
|
|
51
|
+
* Returns the short month name, zero-indexed. E.g. 0 for 'Jan'.
|
|
52
|
+
*
|
|
53
|
+
* @param month Zero-indexed month.
|
|
54
|
+
* @param locales Specify the locale, e.g. 'en-US', new Intl.Locale("en-US").
|
|
55
|
+
*/
|
|
7
56
|
export declare function getShortMonthNameZeroIndexed(month: number, locales?: Intl.LocalesArgument): string;
|
|
57
|
+
/**
|
|
58
|
+
* Returns the short month name, one-indexed. E.g. 1 for 'Jan'.
|
|
59
|
+
*
|
|
60
|
+
* @param month One-indexed month.
|
|
61
|
+
* @param locales Specify the locale, e.g. 'en-US', new Intl.Locale("en-US").
|
|
62
|
+
*/
|
|
8
63
|
export declare function getShortMonthNameOneIndexed(month: number, locales?: Intl.LocalesArgument): string;
|
|
64
|
+
/**
|
|
65
|
+
* Returns a human-readable string date/time like '2025-01-01 22:31:16Z'.
|
|
66
|
+
* Excludes the milliseconds assuming it is not necessary for display.
|
|
67
|
+
*/
|
|
68
|
+
export declare function getDisplayDateTime(ts: AnyDateTime): string;
|
package/dateTimeStr.js
CHANGED
|
@@ -20,16 +20,24 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/dateTimeStr.ts
|
|
21
21
|
var dateTimeStr_exports = {};
|
|
22
22
|
__export(dateTimeStr_exports, {
|
|
23
|
+
getDisplayDateTime: () => getDisplayDateTime,
|
|
23
24
|
getLongMonthNameOneIndexed: () => getLongMonthNameOneIndexed,
|
|
24
25
|
getLongMonthNameZeroIndexed: () => getLongMonthNameZeroIndexed,
|
|
25
26
|
getShortMonthNameOneIndexed: () => getShortMonthNameOneIndexed,
|
|
26
27
|
getShortMonthNameZeroIndexed: () => getShortMonthNameZeroIndexed,
|
|
27
28
|
hhMmSs: () => hhMmSs,
|
|
28
29
|
hhMmSsMs: () => hhMmSsMs,
|
|
30
|
+
toDate: () => toDate,
|
|
29
31
|
tzShort: () => tzShort,
|
|
30
32
|
yyyyMmDd: () => yyyyMmDd
|
|
31
33
|
});
|
|
32
34
|
module.exports = __toCommonJS(dateTimeStr_exports);
|
|
35
|
+
function toDate(ts) {
|
|
36
|
+
if (typeof ts === "number" || typeof ts === "string") {
|
|
37
|
+
return new Date(ts);
|
|
38
|
+
}
|
|
39
|
+
return ts;
|
|
40
|
+
}
|
|
33
41
|
function yyyyMmDd(dt = /* @__PURE__ */ new Date(), separator = "-") {
|
|
34
42
|
const yr = dt.getFullYear();
|
|
35
43
|
const mth = dt.getMonth() + 1;
|
|
@@ -69,14 +77,21 @@ function getShortMonthNameZeroIndexed(month, locales = "default") {
|
|
|
69
77
|
function getShortMonthNameOneIndexed(month, locales = "default") {
|
|
70
78
|
return getShortMonthNameZeroIndexed(month - 1, locales);
|
|
71
79
|
}
|
|
80
|
+
function getDisplayDateTime(ts) {
|
|
81
|
+
const iso = toDate(ts).toISOString();
|
|
82
|
+
const noMs = iso.slice(0, 19) + "Z";
|
|
83
|
+
return noMs.replace("T", " ");
|
|
84
|
+
}
|
|
72
85
|
// Annotate the CommonJS export names for ESM import in node:
|
|
73
86
|
0 && (module.exports = {
|
|
87
|
+
getDisplayDateTime,
|
|
74
88
|
getLongMonthNameOneIndexed,
|
|
75
89
|
getLongMonthNameZeroIndexed,
|
|
76
90
|
getShortMonthNameOneIndexed,
|
|
77
91
|
getShortMonthNameZeroIndexed,
|
|
78
92
|
hhMmSs,
|
|
79
93
|
hhMmSsMs,
|
|
94
|
+
toDate,
|
|
80
95
|
tzShort,
|
|
81
96
|
yyyyMmDd
|
|
82
97
|
});
|
package/dateTimeStr.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;
|
|
1
|
+
{"version":3,"sources":["../src/dateTimeStr.ts"],"sourcesContent":["export type AnyDateTime = number | Date | string;\n\n/**\n * Convert a number (epoch milliseconds), string (parseable date/time), or\n * Date object (no conversion) into a Date object.\n */\nexport function toDate(ts: AnyDateTime): Date {\n if (typeof ts === \"number\" || typeof ts === \"string\") {\n return new Date(ts);\n }\n\n return ts;\n}\n\n/**\n * Returns a date in yyyy-MM-dd format. E.g. '2000-01-02'.\n *\n * @param dt Specify a date object or default to the current date.\n * @param separator Defaults to '-'.\n */\nexport 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\n/**\n * Returns a date in hh:mm:ss format. E.g. '01:02:03'.\n *\n * @param dt Specify a date object or default to the current date/time.\n * @param separator Defaults to ':'.\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\n/**\n * Returns a date in hh:mm:ss.SSS format. E.g. '01:02:03.004'.\n *\n * @param dt Specify a date object or default to the current date/time.\n * @param timeSeparator Separator for hh/mm/ss. Defaults to ':'.\n * @param msSeparator Separator before SSS. Defaults to '.'.\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\n/**\n * Returns the timezone string for the given date. E.g. '+8', '-3.5'.\n * Returns 'Z' for UTC.\n *\n * @param dt Specify a date object or default to the current date/time.\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\n/**\n * Returns the long month name, zero-indexed. E.g. 0 for 'January'.\n *\n * @param month Zero-indexed month.\n * @param locales Specify the locale, e.g. 'en-US', new Intl.Locale(\"en-US\").\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\n/**\n * Returns the long month name, one-indexed. E.g. 1 for 'January'.\n *\n * @param month One-indexed month.\n * @param locales Specify the locale, e.g. 'en-US', new Intl.Locale(\"en-US\").\n */\nexport function getLongMonthNameOneIndexed(\n month: number,\n locales: Intl.LocalesArgument = \"default\",\n): string {\n return getLongMonthNameZeroIndexed(month - 1, locales);\n}\n\n/**\n * Returns the short month name, zero-indexed. E.g. 0 for 'Jan'.\n *\n * @param month Zero-indexed month.\n * @param locales Specify the locale, e.g. 'en-US', new Intl.Locale(\"en-US\").\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\n/**\n * Returns the short month name, one-indexed. E.g. 1 for 'Jan'.\n *\n * @param month One-indexed month.\n * @param locales Specify the locale, e.g. 'en-US', new Intl.Locale(\"en-US\").\n */\nexport function getShortMonthNameOneIndexed(\n month: number,\n locales: Intl.LocalesArgument = \"default\",\n): string {\n return getShortMonthNameZeroIndexed(month - 1, locales);\n}\n\n/**\n * Returns a human-readable string date/time like '2025-01-01 22:31:16Z'.\n * Excludes the milliseconds assuming it is not necessary for display.\n */\nexport function getDisplayDateTime(ts: AnyDateTime) {\n const iso = toDate(ts).toISOString();\n const noMs = iso.slice(0, 19) + \"Z\";\n return noMs.replace(\"T\", \" \");\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAMO,SAAS,OAAO,IAAuB;AAC5C,MAAI,OAAO,OAAO,YAAY,OAAO,OAAO,UAAU;AACpD,WAAO,IAAI,KAAK,EAAE;AAAA,EACpB;AAEA,SAAO;AACT;AAQO,SAAS,SAAS,KAAK,o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,4BACd,OACA,UAAgC,WACxB;AACR,SAAO,6BAA6B,QAAQ,GAAG,OAAO;AACxD;AAMO,SAAS,mBAAmB,IAAiB;AAClD,QAAM,MAAM,OAAO,EAAE,EAAE,YAAY;AACnC,QAAM,OAAO,IAAI,MAAM,GAAG,EAAE,IAAI;AAChC,SAAO,KAAK,QAAQ,KAAK,GAAG;AAC9B;","names":[]}
|
package/duration.d.ts
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
* date and time durations.
|
|
4
4
|
*
|
|
5
5
|
* Note that month and year do not have fixed durations, and hence are excluded
|
|
6
|
-
* from this file.
|
|
6
|
+
* from this file. Weeks have fixed durations, but are excluded because we
|
|
7
|
+
* use days as the max duration supported.
|
|
7
8
|
*/
|
|
8
9
|
export type Duration = {
|
|
9
10
|
days?: number;
|
|
@@ -12,7 +13,13 @@ export type Duration = {
|
|
|
12
13
|
seconds?: number;
|
|
13
14
|
milliseconds?: number;
|
|
14
15
|
};
|
|
16
|
+
/**
|
|
17
|
+
* One of: days, hours, minutes, seconds, milliseconds
|
|
18
|
+
*/
|
|
15
19
|
export type DurationType = keyof Duration;
|
|
20
|
+
/**
|
|
21
|
+
* Order in which the duration type appears in the duration string.
|
|
22
|
+
*/
|
|
16
23
|
export declare const DURATION_TYPE_SEQUENCE: DurationType[];
|
|
17
24
|
/**
|
|
18
25
|
* Follows the same format as Intl.DurationFormat.prototype.format().
|
|
@@ -34,9 +41,10 @@ export type DurationSuffixType = keyof DurationSuffixMap;
|
|
|
34
41
|
export declare const DURATION_STYLE_SUFFIX_MAP: Record<DurationType, DurationSuffixMap>;
|
|
35
42
|
/**
|
|
36
43
|
* 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
|
|
44
|
+
* zero, then return an object with a single field of zero with duration type
|
|
45
|
+
* of durationTypeForZero.
|
|
38
46
|
*
|
|
39
|
-
* durationTypeForZero
|
|
47
|
+
* @param durationTypeForZero Defaults to 'milliseconds'
|
|
40
48
|
*/
|
|
41
49
|
export declare function msToDuration(ms: number, durationTypeForZero?: DurationType): Duration;
|
|
42
50
|
/**
|
|
@@ -47,14 +55,14 @@ export declare function durationToMs(duration: Duration): number;
|
|
|
47
55
|
* Format a given Duration object into a string. If the object has no fields,
|
|
48
56
|
* then returns an empty string.
|
|
49
57
|
*
|
|
50
|
-
* style
|
|
58
|
+
* @param style Defaults to 'short'
|
|
51
59
|
*/
|
|
52
60
|
export declare function formatDuration(duration: Duration, style?: DurationStyle): string;
|
|
53
61
|
/**
|
|
54
62
|
* Convert a millisecond duration into a human-readable duration string.
|
|
55
63
|
*
|
|
56
|
-
* options.durationTypeForZero - Defaults to 'milliseconds'
|
|
57
|
-
* options.style - Defaults to 'short'
|
|
64
|
+
* @param options.durationTypeForZero - Defaults to 'milliseconds'
|
|
65
|
+
* @param options.style - Defaults to 'short'
|
|
58
66
|
*/
|
|
59
67
|
export declare function readableDuration(ms: number, options?: {
|
|
60
68
|
durationTypeForZero?: DurationType;
|
package/duration.js.map
CHANGED
|
@@ -1 +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
|
|
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. Weeks have fixed durations, but are excluded because we\n * use days as the max duration supported.\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\n/**\n * One of: days, hours, minutes, seconds, milliseconds\n */\nexport type DurationType = keyof Duration;\n\n/**\n * Order in which the duration type appears in the duration string.\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 with duration type\n * of durationTypeForZero.\n *\n * @param 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 * @param 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 * @param options.durationTypeForZero - Defaults to 'milliseconds'\n * @param 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;\n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AQ,yBAAyB,KAAK;AAE5C,QAAM,IAAc,CAAC;AAErB,aAAW,QAAQ,wBAAwB;AACzC,UAAM,QAAQ,SAAS,IAAI;AAC3B,QAAI,UAAU,OAAW;AAEzB,UAAM,YAAY,0BAA0B,IAAI;AAChD,UAAM,SAAS,UAAU,IAAI,UAAU,KAAK,IAAI,UAAU,WAAW;AACrE,MAAE,KAAK,QAAQ,QAAQ,MAAM;AAAA,EAC/B;AAEA,QAAM,YAAY,yBAAyB,KAAK;AAChD,SAAO,EAAE,KAAK,SAAS;AACzB;AAQO,SAAS,iBACd,IACA,SACQ;AACR,QAAM,WAAW,aAAa,IAAI,mCAAS,mBAAmB;AAE9D,SAAO,eAAe,UAAU,mCAAS,KAAK;AAChD;","names":[]}
|
package/kvStore.d.ts
CHANGED
|
@@ -37,7 +37,7 @@ export declare class KVStore {
|
|
|
37
37
|
constructor(dbName: string, dbVersion: number, defaultExpiryDeltaMs: number);
|
|
38
38
|
private getOrCreateDb;
|
|
39
39
|
private transact;
|
|
40
|
-
set<T>(key: string, value: T,
|
|
40
|
+
set<T>(key: string, value: T, expiryDeltaMs?: number): Promise<T>;
|
|
41
41
|
/** Delete one or multiple keys. */
|
|
42
42
|
delete(key: string | string[]): Promise<void>;
|
|
43
43
|
get<T>(key: string): Promise<T | undefined>;
|
|
@@ -61,3 +61,20 @@ export declare class KVStore {
|
|
|
61
61
|
* you want, but most likely you will only need one store instance.
|
|
62
62
|
*/
|
|
63
63
|
export declare const kvStore: KVStore;
|
|
64
|
+
/**
|
|
65
|
+
* Class to represent one key in the store with a default expiration.
|
|
66
|
+
*/
|
|
67
|
+
declare class KvStoreItem<T> {
|
|
68
|
+
readonly key: string;
|
|
69
|
+
readonly defaultExpiryDeltaMs: number;
|
|
70
|
+
readonly store: KVStore;
|
|
71
|
+
constructor(key: string, defaultExpiryDeltaMs: number, store?: KVStore);
|
|
72
|
+
get(): Promise<Awaited<T> | undefined>;
|
|
73
|
+
set(value: T, expiryDeltaMs?: number): Promise<void>;
|
|
74
|
+
delete(): Promise<void>;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Create a KV store item with a key and a default expiration.
|
|
78
|
+
*/
|
|
79
|
+
export declare function kvStoreItem<T>(key: string, defaultExpiryDeltaMs: number): KvStoreItem<T>;
|
|
80
|
+
export {};
|
package/kvStore.js
CHANGED
|
@@ -45,7 +45,8 @@ __export(kvStore_exports, {
|
|
|
45
45
|
KVStore: () => KVStore,
|
|
46
46
|
KVStoreField: () => KVStoreField,
|
|
47
47
|
MILLIS_PER_DAY: () => MILLIS_PER_DAY,
|
|
48
|
-
kvStore: () => kvStore
|
|
48
|
+
kvStore: () => kvStore,
|
|
49
|
+
kvStoreItem: () => kvStoreItem
|
|
49
50
|
});
|
|
50
51
|
module.exports = __toCommonJS(kvStore_exports);
|
|
51
52
|
var DEFAULT_DB_NAME = "KVStore";
|
|
@@ -128,11 +129,11 @@ var KVStore = class {
|
|
|
128
129
|
});
|
|
129
130
|
}
|
|
130
131
|
set(_0, _1) {
|
|
131
|
-
return __async(this, arguments, function* (key, value,
|
|
132
|
+
return __async(this, arguments, function* (key, value, expiryDeltaMs = this.defaultExpiryDeltaMs) {
|
|
132
133
|
const obj = {
|
|
133
134
|
key,
|
|
134
135
|
value,
|
|
135
|
-
expireMs: Date.now() +
|
|
136
|
+
expireMs: Date.now() + expiryDeltaMs
|
|
136
137
|
};
|
|
137
138
|
return yield this.transact(
|
|
138
139
|
"readwrite",
|
|
@@ -303,6 +304,31 @@ var kvStore = new KVStore(
|
|
|
303
304
|
DEFAULT_DB_VERSION,
|
|
304
305
|
DEFAULT_EXPIRY_DELTA_MS
|
|
305
306
|
);
|
|
307
|
+
var KvStoreItem = class {
|
|
308
|
+
constructor(key, defaultExpiryDeltaMs, store = kvStore) {
|
|
309
|
+
this.key = key;
|
|
310
|
+
this.defaultExpiryDeltaMs = defaultExpiryDeltaMs;
|
|
311
|
+
this.store = store;
|
|
312
|
+
}
|
|
313
|
+
get() {
|
|
314
|
+
return __async(this, null, function* () {
|
|
315
|
+
return yield this.store.get(this.key);
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
set(_0) {
|
|
319
|
+
return __async(this, arguments, function* (value, expiryDeltaMs = this.defaultExpiryDeltaMs) {
|
|
320
|
+
yield this.store.set(this.key, value, expiryDeltaMs);
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
delete() {
|
|
324
|
+
return __async(this, null, function* () {
|
|
325
|
+
yield this.store.delete(this.key);
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
function kvStoreItem(key, defaultExpiryDeltaMs) {
|
|
330
|
+
return new KvStoreItem(key, defaultExpiryDeltaMs);
|
|
331
|
+
}
|
|
306
332
|
// Annotate the CommonJS export names for ESM import in node:
|
|
307
333
|
0 && (module.exports = {
|
|
308
334
|
DEFAULT_EXPIRY_DELTA_MS,
|
|
@@ -310,6 +336,7 @@ var kvStore = new KVStore(
|
|
|
310
336
|
KVStore,
|
|
311
337
|
KVStoreField,
|
|
312
338
|
MILLIS_PER_DAY,
|
|
313
|
-
kvStore
|
|
339
|
+
kvStore,
|
|
340
|
+
kvStoreItem
|
|
314
341
|
});
|
|
315
342
|
//# sourceMappingURL=kvStore.js.map
|
package/kvStore.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/kvStore.ts"],"sourcesContent":["/**\n * Indexed DB key-value store with support for auto-expirations.\n *\n * Why use this?\n * 1. No need to worry about running out of storage.\n * 2. Extremely simple interface to use indexed DBs.\n * 3. Auto-expirations frees you from worrying about data clean-up.\n * 4. Any serializable data type can be stored (except undefined).\n *\n * How to use?\n * Just use the `kvStore` global constant like the local storage.\n */\n\n// Updating the DB name will cause all old entries to be gone.\nconst DEFAULT_DB_NAME = \"KVStore\";\n\n// Updating the version will cause all old entries to be gone.\nconst DEFAULT_DB_VERSION = 1;\n\n// Use a constant store name to keep things simple.\nconst STORE_NAME = \"kvStore\";\n\n/** One day in milliseconds. */\nexport const MILLIS_PER_DAY = 86_400_000;\n\n/** 30 days in ms. */\nexport const DEFAULT_EXPIRY_DELTA_MS = MILLIS_PER_DAY * 30;\n\n/** Do GC once per day. */\nexport const GC_INTERVAL_MS = MILLIS_PER_DAY;\n\ntype StoredObject<T> = {\n key: string;\n value: T;\n expireMs: number;\n};\n\n/**\n * Parse a stored value string. Returns undefined if invalid or expired.\n * Throws an error if the string cannot be parsed as JSON.\n */\nfunction validateStoredObject<T>(\n obj: StoredObject<T>,\n): StoredObject<T> | undefined {\n if (\n !obj ||\n typeof obj !== \"object\" ||\n !(\"key\" in obj) ||\n typeof obj.key !== \"string\" ||\n !(\"value\" in obj) ||\n obj.value === undefined ||\n !(\"expireMs\" in obj) ||\n typeof obj.expireMs !== \"number\" ||\n Date.now() >= obj.expireMs\n ) {\n return undefined;\n }\n\n return obj;\n}\n\n/** Add an `onerror` handler to the request. */\nfunction withOnError<T extends IDBRequest | IDBTransaction>(\n request: T,\n reject: (reason?: unknown) => void,\n): T {\n request.onerror = (event) => {\n reject(event);\n };\n\n return request;\n}\n\nexport class KVStoreField<T> {\n public constructor(\n public readonly store: KVStore,\n public readonly key: string,\n ) {}\n\n public get(): Promise<T | undefined> {\n return this.store.get(this.key);\n }\n\n public set(t: T): Promise<T> {\n return this.store.set(this.key, t);\n }\n\n public delete(): Promise<void> {\n return this.store.delete(this.key);\n }\n}\n\n/**\n * You can create multiple KVStores if you want, but most likely you will only\n * need to use the default `kvStore` instance.\n */\nexport class KVStore {\n // We'll init the DB only on first use.\n private db: IDBDatabase | undefined;\n\n // Local storage key name for the last GC completed timestamp.\n public readonly gcMsStorageKey: string;\n\n public constructor(\n public readonly dbName: string,\n public readonly dbVersion: number,\n public readonly defaultExpiryDeltaMs: number,\n ) {\n this.gcMsStorageKey = `__kvStore:lastGcMs:${dbName}:v${dbVersion}:${STORE_NAME}`;\n }\n\n private async getOrCreateDb() {\n if (!this.db) {\n this.db = await new Promise<IDBDatabase>((resolve, reject) => {\n const request = withOnError(\n globalThis.indexedDB.open(this.dbName, this.dbVersion),\n reject,\n );\n\n request.onupgradeneeded = (event) => {\n const db = (event.target as unknown as { result: IDBDatabase })\n .result;\n\n // Create the store on DB init.\n const objectStore = db.createObjectStore(STORE_NAME, {\n keyPath: \"key\",\n });\n\n objectStore.createIndex(\"key\", \"key\", {\n unique: true,\n });\n };\n\n request.onsuccess = (event) => {\n const db = (event.target as unknown as { result: IDBDatabase })\n .result;\n resolve(db);\n };\n });\n }\n\n return this.db;\n }\n\n private async transact<T>(\n mode: IDBTransactionMode,\n callback: (\n objectStore: IDBObjectStore,\n resolve: (t: T) => void,\n reject: (reason?: unknown) => void,\n ) => void,\n ): Promise<T> {\n const db = await this.getOrCreateDb();\n\n return await new Promise<T>((resolve, reject) => {\n const transaction = withOnError(db.transaction(STORE_NAME, mode), reject);\n\n transaction.onabort = (event) => {\n reject(event);\n };\n\n const objectStore = transaction.objectStore(STORE_NAME);\n\n callback(objectStore, resolve, reject);\n });\n }\n\n public async set<T>(\n key: string,\n value: T,\n expireDeltaMs: number = this.defaultExpiryDeltaMs,\n ): Promise<T> {\n const obj = {\n key,\n value,\n expireMs: Date.now() + expireDeltaMs,\n };\n\n return await this.transact<T>(\n \"readwrite\",\n (objectStore, resolve, reject) => {\n const request = withOnError(objectStore.put(obj), reject);\n\n request.onsuccess = () => {\n resolve(value);\n\n this.gc(); // check GC on every write\n };\n },\n );\n }\n\n /** Delete one or multiple keys. */\n public async delete(key: string | string[]): Promise<void> {\n return await this.transact<void>(\n \"readwrite\",\n (objectStore, resolve, reject) => {\n objectStore.transaction.oncomplete = () => {\n resolve();\n };\n\n if (typeof key === \"string\") {\n withOnError(objectStore.delete(key), reject);\n } else {\n for (const k of key) {\n withOnError(objectStore.delete(k), reject);\n }\n }\n },\n );\n }\n\n public async get<T>(key: string): Promise<T | undefined> {\n const stored = await this.transact<StoredObject<T> | undefined>(\n \"readonly\",\n (objectStore, resolve, reject) => {\n const request = withOnError(objectStore.get(key), reject);\n\n request.onsuccess = () => {\n resolve(request.result);\n };\n },\n );\n\n if (!stored) {\n return undefined;\n }\n\n try {\n const obj = validateStoredObject(stored);\n if (!obj) {\n await this.delete(key);\n\n this.gc(); // check GC on every read of an expired key\n\n return undefined;\n }\n\n return obj.value;\n } catch (e) {\n console.error(`Invalid kv value: ${key}=${JSON.stringify(stored)}:`, e);\n await this.delete(key);\n\n this.gc(); // check GC on every read of an invalid key\n\n return undefined;\n }\n }\n\n public async forEach(\n callback: (\n key: string,\n value: unknown,\n expireMs: number,\n ) => void | Promise<void>,\n ): Promise<void> {\n await this.transact<void>(\"readonly\", (objectStore, resolve, reject) => {\n const request = withOnError(objectStore.openCursor(), reject);\n\n request.onsuccess = async (event) => {\n const cursor = (\n event.target as unknown as { result: IDBCursorWithValue }\n ).result;\n\n if (cursor) {\n if (cursor.key) {\n const obj = validateStoredObject(cursor.value);\n if (obj) {\n await callback(String(cursor.key), obj.value, obj.expireMs);\n } else {\n await callback(String(cursor.key), undefined, 0);\n }\n }\n cursor.continue();\n } else {\n resolve();\n }\n };\n });\n }\n\n /** Cannot be a getter because this needs to be async. */\n public async size() {\n let count = 0;\n await this.forEach(() => {\n count++;\n });\n return count;\n }\n\n public async clear() {\n const keys: string[] = [];\n await this.forEach((key) => {\n keys.push(key);\n });\n\n await this.delete(keys);\n }\n\n /** Mainly for debugging dumps. */\n public async asMap(): Promise<Map<string, unknown>> {\n const map = new Map<string, unknown>();\n await this.forEach((key, value, expireMs) => {\n map.set(key, { value, expireMs });\n });\n return map;\n }\n\n public get lastGcMs(): number {\n const lastGcMsStr = globalThis.localStorage.getItem(this.gcMsStorageKey);\n if (!lastGcMsStr) return 0;\n\n const ms = Number(lastGcMsStr);\n return isNaN(ms) ? 0 : ms;\n }\n\n public set lastGcMs(ms: number) {\n globalThis.localStorage.setItem(this.gcMsStorageKey, String(ms));\n }\n\n /** Perform garbage-collection if due. */\n public async gc() {\n const lastGcMs = this.lastGcMs;\n\n // Set initial timestamp - no need GC now.\n if (!lastGcMs) {\n this.lastGcMs = Date.now();\n return;\n }\n\n if (Date.now() < lastGcMs + GC_INTERVAL_MS) {\n return; // not due for next GC yet\n }\n\n // GC is due now, so run it.\n await this.gcNow();\n }\n\n /** Perform garbage-collection immediately without checking. */\n public async gcNow() {\n console.log(`Starting kvStore GC on ${this.dbName} v${this.dbVersion}...`);\n\n // Prevent concurrent GC runs.\n this.lastGcMs = Date.now();\n\n const keysToDelete: string[] = [];\n await this.forEach(\n async (key: string, value: unknown, expireMs: number) => {\n if (value === undefined || Date.now() >= expireMs) {\n keysToDelete.push(key);\n }\n },\n );\n\n if (keysToDelete.length) {\n await this.delete(keysToDelete);\n }\n\n console.log(\n `Finished kvStore GC on ${this.dbName} v${this.dbVersion} ` +\n `- deleted ${keysToDelete.length} keys`,\n );\n\n // Mark the end time as last GC time.\n this.lastGcMs = Date.now();\n }\n\n /** Get an independent store item with a locked key and value type. */\n public field<T>(key: string) {\n return new KVStoreField<T>(this, key);\n }\n}\n\n/**\n * Default KV store ready for immediate use. You can create new instances if\n * you want, but most likely you will only need one store instance.\n */\nexport const kvStore = new KVStore(\n DEFAULT_DB_NAME,\n DEFAULT_DB_VERSION,\n DEFAULT_EXPIRY_DELTA_MS,\n);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAcA,IAAM,kBAAkB;AAGxB,IAAM,qBAAqB;AAG3B,IAAM,aAAa;AAGZ,IAAM,iBAAiB;AAGvB,IAAM,0BAA0B,iBAAiB;AAGjD,IAAM,iBAAiB;AAY9B,SAAS,qBACP,KAC6B;AAC7B,MACE,CAAC,OACD,OAAO,QAAQ,YACf,EAAE,SAAS,QACX,OAAO,IAAI,QAAQ,YACnB,EAAE,WAAW,QACb,IAAI,UAAU,UACd,EAAE,cAAc,QAChB,OAAO,IAAI,aAAa,YACxB,KA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,QAAQ;AACvB,cAAM,KAAK,OAAO,YAAY;AAAA,MAChC;AAEA,cAAQ;AAAA,QACN,0BAA0B,KAAK,MAAM,KAAK,KAAK,SAAS,cACzC,aAAa,MAAM;AAAA,MACpC;AAGA,WAAK,WAAW,KAAK,IAAI;AAAA,IAC3B;AAAA;AAAA;AAAA,EAGO,MAAS,KAAa;AAC3B,WAAO,IAAI,aAAgB,MAAM,GAAG;AAAA,EACtC;AACF;AAMO,IAAM,UAAU,IAAI;AAAA,EACzB;AAAA,EACA;AAAA,EACA;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/kvStore.ts"],"sourcesContent":["/**\n * Indexed DB key-value store with support for auto-expirations.\n *\n * Why use this?\n * 1. No need to worry about running out of storage.\n * 2. Extremely simple interface to use indexed DBs.\n * 3. Auto-expirations frees you from worrying about data clean-up.\n * 4. Any serializable data type can be stored (except undefined).\n *\n * How to use?\n * Just use the `kvStore` global constant like the local storage.\n */\n\n// Updating the DB name will cause all old entries to be gone.\nconst DEFAULT_DB_NAME = \"KVStore\";\n\n// Updating the version will cause all old entries to be gone.\nconst DEFAULT_DB_VERSION = 1;\n\n// Use a constant store name to keep things simple.\nconst STORE_NAME = \"kvStore\";\n\n/** One day in milliseconds. */\nexport const MILLIS_PER_DAY = 86_400_000;\n\n/** 30 days in ms. */\nexport const DEFAULT_EXPIRY_DELTA_MS = MILLIS_PER_DAY * 30;\n\n/** Do GC once per day. */\nexport const GC_INTERVAL_MS = MILLIS_PER_DAY;\n\ntype StoredObject<T> = {\n key: string;\n value: T;\n expireMs: number;\n};\n\n/**\n * Parse a stored value string. Returns undefined if invalid or expired.\n * Throws an error if the string cannot be parsed as JSON.\n */\nfunction validateStoredObject<T>(\n obj: StoredObject<T>,\n): StoredObject<T> | undefined {\n if (\n !obj ||\n typeof obj !== \"object\" ||\n !(\"key\" in obj) ||\n typeof obj.key !== \"string\" ||\n !(\"value\" in obj) ||\n obj.value === undefined ||\n !(\"expireMs\" in obj) ||\n typeof obj.expireMs !== \"number\" ||\n Date.now() >= obj.expireMs\n ) {\n return undefined;\n }\n\n return obj;\n}\n\n/** Add an `onerror` handler to the request. */\nfunction withOnError<T extends IDBRequest | IDBTransaction>(\n request: T,\n reject: (reason?: unknown) => void,\n): T {\n request.onerror = (event) => {\n reject(event);\n };\n\n return request;\n}\n\nexport class KVStoreField<T> {\n public constructor(\n public readonly store: KVStore,\n public readonly key: string,\n ) {}\n\n public get(): Promise<T | undefined> {\n return this.store.get(this.key);\n }\n\n public set(t: T): Promise<T> {\n return this.store.set(this.key, t);\n }\n\n public delete(): Promise<void> {\n return this.store.delete(this.key);\n }\n}\n\n/**\n * You can create multiple KVStores if you want, but most likely you will only\n * need to use the default `kvStore` instance.\n */\nexport class KVStore {\n // We'll init the DB only on first use.\n private db: IDBDatabase | undefined;\n\n // Local storage key name for the last GC completed timestamp.\n public readonly gcMsStorageKey: string;\n\n public constructor(\n public readonly dbName: string,\n public readonly dbVersion: number,\n public readonly defaultExpiryDeltaMs: number,\n ) {\n this.gcMsStorageKey = `__kvStore:lastGcMs:${dbName}:v${dbVersion}:${STORE_NAME}`;\n }\n\n private async getOrCreateDb() {\n if (!this.db) {\n this.db = await new Promise<IDBDatabase>((resolve, reject) => {\n const request = withOnError(\n globalThis.indexedDB.open(this.dbName, this.dbVersion),\n reject,\n );\n\n request.onupgradeneeded = (event) => {\n const db = (event.target as unknown as { result: IDBDatabase })\n .result;\n\n // Create the store on DB init.\n const objectStore = db.createObjectStore(STORE_NAME, {\n keyPath: \"key\",\n });\n\n objectStore.createIndex(\"key\", \"key\", {\n unique: true,\n });\n };\n\n request.onsuccess = (event) => {\n const db = (event.target as unknown as { result: IDBDatabase })\n .result;\n resolve(db);\n };\n });\n }\n\n return this.db;\n }\n\n private async transact<T>(\n mode: IDBTransactionMode,\n callback: (\n objectStore: IDBObjectStore,\n resolve: (t: T) => void,\n reject: (reason?: unknown) => void,\n ) => void,\n ): Promise<T> {\n const db = await this.getOrCreateDb();\n\n return await new Promise<T>((resolve, reject) => {\n const transaction = withOnError(db.transaction(STORE_NAME, mode), reject);\n\n transaction.onabort = (event) => {\n reject(event);\n };\n\n const objectStore = transaction.objectStore(STORE_NAME);\n\n callback(objectStore, resolve, reject);\n });\n }\n\n public async set<T>(\n key: string,\n value: T,\n expiryDeltaMs: number = this.defaultExpiryDeltaMs,\n ): Promise<T> {\n const obj = {\n key,\n value,\n expireMs: Date.now() + expiryDeltaMs,\n };\n\n return await this.transact<T>(\n \"readwrite\",\n (objectStore, resolve, reject) => {\n const request = withOnError(objectStore.put(obj), reject);\n\n request.onsuccess = () => {\n resolve(value);\n\n this.gc(); // check GC on every write\n };\n },\n );\n }\n\n /** Delete one or multiple keys. */\n public async delete(key: string | string[]): Promise<void> {\n return await this.transact<void>(\n \"readwrite\",\n (objectStore, resolve, reject) => {\n objectStore.transaction.oncomplete = () => {\n resolve();\n };\n\n if (typeof key === \"string\") {\n withOnError(objectStore.delete(key), reject);\n } else {\n for (const k of key) {\n withOnError(objectStore.delete(k), reject);\n }\n }\n },\n );\n }\n\n public async get<T>(key: string): Promise<T | undefined> {\n const stored = await this.transact<StoredObject<T> | undefined>(\n \"readonly\",\n (objectStore, resolve, reject) => {\n const request = withOnError(objectStore.get(key), reject);\n\n request.onsuccess = () => {\n resolve(request.result);\n };\n },\n );\n\n if (!stored) {\n return undefined;\n }\n\n try {\n const obj = validateStoredObject(stored);\n if (!obj) {\n await this.delete(key);\n\n this.gc(); // check GC on every read of an expired key\n\n return undefined;\n }\n\n return obj.value;\n } catch (e) {\n console.error(`Invalid kv value: ${key}=${JSON.stringify(stored)}:`, e);\n await this.delete(key);\n\n this.gc(); // check GC on every read of an invalid key\n\n return undefined;\n }\n }\n\n public async forEach(\n callback: (\n key: string,\n value: unknown,\n expireMs: number,\n ) => void | Promise<void>,\n ): Promise<void> {\n await this.transact<void>(\"readonly\", (objectStore, resolve, reject) => {\n const request = withOnError(objectStore.openCursor(), reject);\n\n request.onsuccess = async (event) => {\n const cursor = (\n event.target as unknown as { result: IDBCursorWithValue }\n ).result;\n\n if (cursor) {\n if (cursor.key) {\n const obj = validateStoredObject(cursor.value);\n if (obj) {\n await callback(String(cursor.key), obj.value, obj.expireMs);\n } else {\n await callback(String(cursor.key), undefined, 0);\n }\n }\n cursor.continue();\n } else {\n resolve();\n }\n };\n });\n }\n\n /** Cannot be a getter because this needs to be async. */\n public async size() {\n let count = 0;\n await this.forEach(() => {\n count++;\n });\n return count;\n }\n\n public async clear() {\n const keys: string[] = [];\n await this.forEach((key) => {\n keys.push(key);\n });\n\n await this.delete(keys);\n }\n\n /** Mainly for debugging dumps. */\n public async asMap(): Promise<Map<string, unknown>> {\n const map = new Map<string, unknown>();\n await this.forEach((key, value, expireMs) => {\n map.set(key, { value, expireMs });\n });\n return map;\n }\n\n public get lastGcMs(): number {\n const lastGcMsStr = globalThis.localStorage.getItem(this.gcMsStorageKey);\n if (!lastGcMsStr) return 0;\n\n const ms = Number(lastGcMsStr);\n return isNaN(ms) ? 0 : ms;\n }\n\n public set lastGcMs(ms: number) {\n globalThis.localStorage.setItem(this.gcMsStorageKey, String(ms));\n }\n\n /** Perform garbage-collection if due. */\n public async gc() {\n const lastGcMs = this.lastGcMs;\n\n // Set initial timestamp - no need GC now.\n if (!lastGcMs) {\n this.lastGcMs = Date.now();\n return;\n }\n\n if (Date.now() < lastGcMs + GC_INTERVAL_MS) {\n return; // not due for next GC yet\n }\n\n // GC is due now, so run it.\n await this.gcNow();\n }\n\n /** Perform garbage-collection immediately without checking. */\n public async gcNow() {\n console.log(`Starting kvStore GC on ${this.dbName} v${this.dbVersion}...`);\n\n // Prevent concurrent GC runs.\n this.lastGcMs = Date.now();\n\n const keysToDelete: string[] = [];\n await this.forEach(\n async (key: string, value: unknown, expireMs: number) => {\n if (value === undefined || Date.now() >= expireMs) {\n keysToDelete.push(key);\n }\n },\n );\n\n if (keysToDelete.length) {\n await this.delete(keysToDelete);\n }\n\n console.log(\n `Finished kvStore GC on ${this.dbName} v${this.dbVersion} ` +\n `- deleted ${keysToDelete.length} keys`,\n );\n\n // Mark the end time as last GC time.\n this.lastGcMs = Date.now();\n }\n\n /** Get an independent store item with a locked key and value type. */\n public field<T>(key: string) {\n return new KVStoreField<T>(this, key);\n }\n}\n\n/**\n * Default KV store ready for immediate use. You can create new instances if\n * you want, but most likely you will only need one store instance.\n */\nexport const kvStore = new KVStore(\n DEFAULT_DB_NAME,\n DEFAULT_DB_VERSION,\n DEFAULT_EXPIRY_DELTA_MS,\n);\n\n/**\n * Class to represent one key in the store with a default expiration.\n */\nclass KvStoreItem<T> {\n public constructor(\n public readonly key: string,\n public readonly defaultExpiryDeltaMs: number,\n public readonly store = kvStore,\n ) {}\n\n public async get(): Promise<Awaited<T> | undefined> {\n return await this.store.get(this.key);\n }\n\n public async set(\n value: T,\n expiryDeltaMs: number = this.defaultExpiryDeltaMs,\n ): Promise<void> {\n await this.store.set(this.key, value, expiryDeltaMs);\n }\n\n public async delete(): Promise<void> {\n await this.store.delete(this.key);\n }\n}\n\n/**\n * Create a KV store item with a key and a default expiration.\n */\nexport function kvStoreItem<T>(\n key: string,\n defaultExpiryDeltaMs: number,\n): KvStoreItem<T> {\n return new KvStoreItem<T>(key, defaultExpiryDeltaMs);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAcA,IAAM,kBAAkB;AAGxB,IAAM,qBAAqB;AAG3B,IAAM,aAAa;AAGZ,IAAM,iBAAiB;AAGvB,IAAM,0BAA0B,iBAAiB;AAGjD,IAAM,iBAAiB;AAY9B,SAAS,qBACP,KAC6B;AAC7B,MACE,CAAC,OACD,OAAO,QAAQ,YACf,EAAE,SAAS,QACX,OAAO,IAAI,QAAQ,YACnB,EAAE,WAAW,QACb,IAAI,UAAU,UACd,EAAE,cAAc,QAChB,OAAO,IAAI,aAAa,YACxB,KAAK,IAAI,KA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B,cAAM,KAAK,OAAO,YAAY;AAAA,MAChC;AAEA,cAAQ;AAAA,QACN,0BAA0B,KAAK,MAAM,KAAK,KAAK,SAAS,cACzC,aAAa,MAAM;AAAA,MACpC;AAGA,WAAK,WAAW,KAAK,IAAI;AAAA,IAC3B;AAAA;AAAA;AAAA,EAGO,MAAS,KAAa;AAC3B,WAAO,IAAI,aAAgB,MAAM,GAAG;AAAA,EACtC;AACF;AAMO,IAAM,UAAU,IAAI;AAAA,EACzB;AAAA,EACA;AAAA,EACA;AACF;AAKA,IAAM,cAAN,MAAqB;AAAA,EACZ,YACW,KACA,sBACA,QAAQ,SACxB;AAHgB;AACA;AACA;AAAA,EACf;AAAA,EAEU,MAAuC;AAAA;AAClD,aAAO,MAAM,KAAK,MAAM,IAAI,KAAK,GAAG;AAAA,IACtC;AAAA;AAAA,EAEa,IACX,IAEe;AAAA,+CAFf,OACA,gBAAwB,KAAK,sBACd;AACf,YAAM,KAAK,MAAM,IAAI,KAAK,KAAK,OAAO,aAAa;AAAA,IACrD;AAAA;AAAA,EAEa,SAAwB;AAAA;AACnC,YAAM,KAAK,MAAM,OAAO,KAAK,GAAG;AAAA,IAClC;AAAA;AACF;AAKO,SAAS,YACd,KACA,sBACgB;AAChB,SAAO,IAAI,YAAe,KAAK,oBAAoB;AACrD;","names":[]}
|
package/localStorageCache.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { Duration } from "./duration";
|
|
2
|
+
export type StoredItem<T> = {
|
|
3
|
+
value: T;
|
|
4
|
+
expireMs: number;
|
|
5
|
+
};
|
|
2
6
|
/**
|
|
3
7
|
* Simple local storage cache with support for auto-expiration.
|
|
4
8
|
* Note that this works in the browser context only because nodejs does not
|
|
@@ -11,13 +15,19 @@ import { Duration } from "./duration";
|
|
|
11
15
|
* In order to provide proper type-checking, please always specify the T
|
|
12
16
|
* type parameter. E.g. const item = storeItem<string>("name", 10_000);
|
|
13
17
|
*
|
|
14
|
-
*
|
|
18
|
+
* @param key The store key in local storage.
|
|
19
|
+
* @param expires Either a number in milliseconds, or a Duration object
|
|
20
|
+
* @param logError Log an error if we found an invalid object in the store.
|
|
21
|
+
* The invalid object is usually a string that cannot be parsed as JSON.
|
|
22
|
+
* @param defaultValue Specify a default value to use for the object. Defaults
|
|
23
|
+
* to undefined.
|
|
15
24
|
*/
|
|
16
25
|
export declare function storeItem<T>(key: string, expires: number | Duration, logError?: boolean, defaultValue?: T): CacheItem<T>;
|
|
17
26
|
declare class CacheItem<T> {
|
|
18
27
|
readonly key: string;
|
|
19
28
|
readonly expireDeltaMs: number;
|
|
20
29
|
readonly logError: boolean;
|
|
30
|
+
readonly defaultValue: T | undefined;
|
|
21
31
|
/**
|
|
22
32
|
* Create a cache item accessor object with auto-expiration.
|
|
23
33
|
*/
|
package/localStorageCache.js
CHANGED
|
@@ -54,18 +54,15 @@ var CacheItem = class {
|
|
|
54
54
|
this.key = key;
|
|
55
55
|
this.expireDeltaMs = expireDeltaMs;
|
|
56
56
|
this.logError = logError;
|
|
57
|
-
|
|
58
|
-
if (this.get() === void 0) {
|
|
59
|
-
this.set(defaultValue);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
57
|
+
this.defaultValue = defaultValue;
|
|
62
58
|
}
|
|
63
59
|
/**
|
|
64
60
|
* Set the value of this item with auto-expiration.
|
|
65
61
|
*/
|
|
66
62
|
set(value) {
|
|
67
63
|
const expireMs = Date.now() + this.expireDeltaMs;
|
|
68
|
-
const
|
|
64
|
+
const toStore = { value, expireMs };
|
|
65
|
+
const valueStr = JSON.stringify(toStore);
|
|
69
66
|
globalThis.localStorage.setItem(this.key, valueStr);
|
|
70
67
|
}
|
|
71
68
|
/**
|
|
@@ -73,14 +70,14 @@ var CacheItem = class {
|
|
|
73
70
|
*/
|
|
74
71
|
get() {
|
|
75
72
|
const jsonStr = globalThis.localStorage.getItem(this.key);
|
|
76
|
-
if (!jsonStr
|
|
77
|
-
return
|
|
73
|
+
if (!jsonStr) {
|
|
74
|
+
return this.defaultValue;
|
|
78
75
|
}
|
|
79
76
|
try {
|
|
80
77
|
const obj = JSON.parse(jsonStr);
|
|
81
78
|
if (!obj || typeof obj !== "object" || !("value" in obj) || !("expireMs" in obj) || typeof obj.expireMs !== "number" || Date.now() >= obj.expireMs) {
|
|
82
|
-
|
|
83
|
-
return
|
|
79
|
+
this.remove();
|
|
80
|
+
return this.defaultValue;
|
|
84
81
|
}
|
|
85
82
|
return obj.value;
|
|
86
83
|
} catch (e) {
|
|
@@ -90,8 +87,8 @@ var CacheItem = class {
|
|
|
90
87
|
e
|
|
91
88
|
);
|
|
92
89
|
}
|
|
93
|
-
|
|
94
|
-
return
|
|
90
|
+
this.remove();
|
|
91
|
+
return this.defaultValue;
|
|
95
92
|
}
|
|
96
93
|
}
|
|
97
94
|
/**
|
package/localStorageCache.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/localStorageCache.ts","../src/timeConstants.ts","../src/duration.ts"],"sourcesContent":["import { Duration, durationToMs } from \"./duration\";\n\n/**\n * Simple local storage cache with support for auto-expiration.\n * Note that this works in the browser context only because nodejs does not\n * have local storage.\n *\n * Create a cache item accessor object with auto-expiration. The value will\n * always be stored as a string by applying JSON.stringify(), and will be\n * returned in the same object type by applying JSON.parse().\n *\n * In order to provide proper type-checking, please always specify the T\n * type parameter. E.g. const item = storeItem<string>(\"name\", 10_000);\n *\n * expires - Either a number in milliseconds, or a Duration object\n */\nexport function storeItem<T>(\n key: string,\n expires: number | Duration,\n logError = true,\n defaultValue?: T,\n) {\n const expireDeltaMs =\n typeof expires === \"number\" ? expires : durationToMs(expires);\n\n return new CacheItem<T>(key, expireDeltaMs, logError, defaultValue);\n}\n\nclass CacheItem<T> {\n /**\n * Create a cache item accessor object with auto-expiration.\n */\n public constructor(\n public readonly key: string,\n public readonly expireDeltaMs: number,\n public readonly logError: boolean,\n defaultValue: T | undefined,\n ) {\n if (defaultValue !== undefined) {\n if (this.get() === undefined) {\n this.set(defaultValue);\n }\n }\n }\n\n /**\n * Set the value of this item with auto-expiration.\n */\n public set(value: T): void {\n const expireMs = Date.now() + this.expireDeltaMs;\n const valueStr = JSON.stringify({ value, expireMs });\n\n globalThis.localStorage.setItem(this.key, valueStr);\n }\n\n /**\n * Get the value of this item, or undefined if value is not set or expired.\n */\n public get(): T | undefined {\n const jsonStr = globalThis.localStorage.getItem(this.key);\n\n if (!jsonStr || typeof jsonStr !== \"string\") {\n return undefined;\n }\n\n try {\n const obj: { value: T; expireMs: number } | undefined =\n JSON.parse(jsonStr);\n if (\n !obj ||\n typeof obj !== \"object\" ||\n !(\"value\" in obj) ||\n !(\"expireMs\" in obj) ||\n typeof obj.expireMs !== \"number\" ||\n Date.now() >= obj.expireMs\n ) {\n globalThis.localStorage.removeItem(this.key);\n return undefined;\n }\n\n return obj.value;\n } catch (e) {\n if (this.logError) {\n console.error(\n `Found invalid storage value: ${this.key}=${jsonStr}:`,\n e,\n );\n }\n globalThis.localStorage.removeItem(this.key);\n return undefined;\n }\n }\n\n /**\n * Remove the value of this item.\n */\n public remove(): void {\n globalThis.localStorage.removeItem(this.key);\n }\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","/**\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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKO,IAAM,gBAAgB;AACtB,IAAM,gBAAgB;AACtB,IAAM,cAAc;AACpB,IAAM,aAAa;;;AC0KnB,SAAS,aAAa,UAA4B;AAlLzD;AAmLE,QAAM,WAAU,cAAS,SAAT,YAAiB,KAAK;AACtC,QAAM,YAAW,cAAS,UAAT,YAAkB,KAAK;AACxC,QAAM,WAAU,cAAS,YAAT,YAAoB,KAAK;AACzC,QAAM,WAAU,cAAS,YAAT,YAAoB,KAAK;AACzC,QAAM,QAAO,cAAS,iBAAT,YAAyB;AAEtC,SAAO,SAAS,UAAU,SAAS,SAAS;AAC9C;;;AF1KO,SAAS,UACd,KACA,SACA,WAAW,MACX,cACA;AACA,QAAM,gBACJ,OAAO,YAAY,WAAW,UAAU,aAAa,OAAO;AAE9D,SAAO,I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/duration.ts"],"sourcesContent":["import { Duration, durationToMs } from \"./duration\";\n\nexport type StoredItem<T> = { value: T; expireMs: number };\n\n/**\n * Simple local storage cache with support for auto-expiration.\n * Note that this works in the browser context only because nodejs does not\n * have local storage.\n *\n * Create a cache item accessor object with auto-expiration. The value will\n * always be stored as a string by applying JSON.stringify(), and will be\n * returned in the same object type by applying JSON.parse().\n *\n * In order to provide proper type-checking, please always specify the T\n * type parameter. E.g. const item = storeItem<string>(\"name\", 10_000);\n *\n * @param key The store key in local storage.\n * @param expires Either a number in milliseconds, or a Duration object\n * @param logError Log an error if we found an invalid object in the store.\n * The invalid object is usually a string that cannot be parsed as JSON.\n * @param defaultValue Specify a default value to use for the object. Defaults\n * to undefined.\n */\nexport function storeItem<T>(\n key: string,\n expires: number | Duration,\n logError = true,\n defaultValue?: T,\n) {\n const expireDeltaMs =\n typeof expires === \"number\" ? expires : durationToMs(expires);\n\n return new CacheItem<T>(key, expireDeltaMs, logError, defaultValue);\n}\n\nclass CacheItem<T> {\n /**\n * Create a cache item accessor object with auto-expiration.\n */\n public constructor(\n public readonly key: string,\n public readonly expireDeltaMs: number,\n public readonly logError: boolean,\n public readonly defaultValue: T | undefined,\n ) {}\n\n /**\n * Set the value of this item with auto-expiration.\n */\n public set(value: T): void {\n const expireMs = Date.now() + this.expireDeltaMs;\n const toStore: StoredItem<T> = { value, expireMs };\n const valueStr = JSON.stringify(toStore);\n\n globalThis.localStorage.setItem(this.key, valueStr);\n }\n\n /**\n * Get the value of this item, or undefined if value is not set or expired.\n */\n public get(): T | undefined {\n const jsonStr = globalThis.localStorage.getItem(this.key);\n\n if (!jsonStr) {\n return this.defaultValue;\n }\n\n try {\n const obj: StoredItem<T> | undefined = JSON.parse(jsonStr);\n\n if (\n !obj ||\n typeof obj !== \"object\" ||\n !(\"value\" in obj) ||\n !(\"expireMs\" in obj) ||\n typeof obj.expireMs !== \"number\" ||\n Date.now() >= obj.expireMs\n ) {\n this.remove();\n return this.defaultValue;\n }\n\n return obj.value;\n } catch (e) {\n if (this.logError) {\n console.error(\n `Found invalid storage value: ${this.key}=${jsonStr}:`,\n e,\n );\n }\n this.remove();\n return this.defaultValue;\n }\n }\n\n /**\n * Remove the value of this item.\n */\n public remove(): void {\n globalThis.localStorage.removeItem(this.key);\n }\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","/**\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. Weeks have fixed durations, but are excluded because we\n * use days as the max duration supported.\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\n/**\n * One of: days, hours, minutes, seconds, milliseconds\n */\nexport type DurationType = keyof Duration;\n\n/**\n * Order in which the duration type appears in the duration string.\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 with duration type\n * of durationTypeForZero.\n *\n * @param 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 * @param 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 * @param options.durationTypeForZero - Defaults to 'milliseconds'\n * @param 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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKO,IAAM,gBAAgB;AACtB,IAAM,gBAAgB;AACtB,IAAM,cAAc;AACpB,IAAM,aAAa;;;ACkLnB,SAAS,aAAa,UAA4B;AA1LzD;AA2LE,QAAM,WAAU,cAAS,SAAT,YAAiB,KAAK;AACtC,QAAM,YAAW,cAAS,UAAT,YAAkB,KAAK;AACxC,QAAM,WAAU,cAAS,YAAT,YAAoB,KAAK;AACzC,QAAM,WAAU,cAAS,YAAT,YAAoB,KAAK;AACzC,QAAM,QAAO,cAAS,iBAAT,YAAyB;AAEtC,SAAO,SAAS,UAAU,SAAS,SAAS;AAC9C;;;AF3KO,SAAS,UACd,KACA,SACA,WAAW,MACX,cACA;AACA,QAAM,gBACJ,OAAO,YAAY,WAAW,UAAU,aAAa,OAAO;AAE9D,SAAO,IAAI,UAAa,KAAK,eAAe,UAAU,YAAY;AACpE;AAEA,IAAM,YAAN,MAAmB;AAAA;AAAA;AAAA;AAAA,EAIV,YACW,KACA,eACA,UACA,cAChB;AAJgB;AACA;AACA;AACA;AAAA,EACf;AAAA;AAAA;AAAA;AAAA,EAKI,IAAI,OAAgB;AACzB,UAAM,WAAW,KAAK,IAAI,IAAI,KAAK;AACnC,UAAM,UAAyB,EAAE,OAAO,SAAS;AACjD,UAAM,WAAW,KAAK,UAAU,OAAO;AAEvC,eAAW,aAAa,QAAQ,KAAK,KAAK,QAAQ;AAAA,EACpD;AAAA;AAAA;AAAA;AAAA,EAKO,MAAqB;AAC1B,UAAM,UAAU,WAAW,aAAa,QAAQ,KAAK,GAAG;AAExD,QAAI,CAAC,SAAS;AACZ,aAAO,KAAK;AAAA,IACd;AAEA,QAAI;AACF,YAAM,MAAiC,KAAK,MAAM,OAAO;AAEzD,UACE,CAAC,OACD,OAAO,QAAQ,YACf,EAAE,WAAW,QACb,EAAE,cAAc,QAChB,OAAO,IAAI,aAAa,YACxB,KAAK,IAAI,KAAK,IAAI,UAClB;AACA,aAAK,OAAO;AACZ,eAAO,KAAK;AAAA,MACd;AAEA,aAAO,IAAI;AAAA,IACb,SAAS,GAAG;AACV,UAAI,KAAK,UAAU;AACjB,gBAAQ;AAAA,UACN,gCAAgC,KAAK,GAAG,IAAI,OAAO;AAAA,UACnD;AAAA,QACF;AAAA,MACF;AACA,WAAK,OAAO;AACZ,aAAO,KAAK;AAAA,IACd;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKO,SAAe;AACpB,eAAW,aAAa,WAAW,KAAK,GAAG;AAAA,EAC7C;AACF;","names":[]}
|
package/nonEmpty.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Type asserts that `t` is truthy.
|
|
3
3
|
* Throws an error if `t` is null or undefined.
|
|
4
|
+
*
|
|
5
|
+
* @param varName The variable name to include in the error to throw when t is
|
|
6
|
+
* empty. Defaults to 'value'.
|
|
4
7
|
*/
|
|
5
8
|
export declare function nonEmpty<T>(t: T | null | undefined | "" | 0 | -0 | 0n | false | typeof NaN, varName?: string): T;
|
package/nonEmpty.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/nonEmpty.ts","../src/isEmpty.ts"],"sourcesContent":["import { isEmpty } from \"./isEmpty\";\n\n/**\n * Type asserts that `t` is truthy.\n * Throws an error if `t` is null or undefined.\n */\nexport function nonEmpty<T>(\n t: T | null | undefined | \"\" | 0 | -0 | 0n | false | typeof NaN,\n varName = \"value\",\n): T {\n if (isEmpty(t)) {\n throw new Error(`Empty ${varName}: ${t}`);\n }\n return t as T;\n}\n","/**\n * Returns true if `t` is empty.\n */\nexport function isEmpty(t: unknown): boolean {\n // Anything falsy is considered empty.\n if (!t) {\n return true;\n }\n\n // Arrays are also of type `object`.\n if (typeof t !== \"object\") {\n return false;\n }\n\n // `length` includes arrays as well.\n if (\"length\" in t) {\n return t.length === 0;\n }\n\n // `size` is for Set, Map, Blob etc.\n if (\"size\" in t) {\n return t.size === 0;\n }\n\n // Super fast check for object emptiness.\n // https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object\n for (const k in t) {\n return false;\n }\n\n return true;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGO,SAAS,QAAQ,GAAqB;AAE3C,MAAI,CAAC,GAAG;AACN,WAAO;AAAA,EACT;AAGA,MAAI,OAAO,MAAM,UAAU;AACzB,WAAO;AAAA,EACT;AAGA,MAAI,YAAY,GAAG;AACjB,WAAO,EAAE,WAAW;AAAA,EACtB;AAGA,MAAI,UAAU,GAAG;AACf,WAAO,EAAE,SAAS;AAAA,EACpB;AAIA,aAAW,KAAK,GAAG;AACjB,WAAO;AAAA,EACT;AAEA,SAAO;AACT;;;
|
|
1
|
+
{"version":3,"sources":["../src/nonEmpty.ts","../src/isEmpty.ts"],"sourcesContent":["import { isEmpty } from \"./isEmpty\";\n\n/**\n * Type asserts that `t` is truthy.\n * Throws an error if `t` is null or undefined.\n *\n * @param varName The variable name to include in the error to throw when t is\n * empty. Defaults to 'value'.\n */\nexport function nonEmpty<T>(\n t: T | null | undefined | \"\" | 0 | -0 | 0n | false | typeof NaN,\n varName = \"value\",\n): T {\n if (isEmpty(t)) {\n throw new Error(`Empty ${varName}: ${t}`);\n }\n return t as T;\n}\n","/**\n * Returns true if `t` is empty.\n */\nexport function isEmpty(t: unknown): boolean {\n // Anything falsy is considered empty.\n if (!t) {\n return true;\n }\n\n // Arrays are also of type `object`.\n if (typeof t !== \"object\") {\n return false;\n }\n\n // `length` includes arrays as well.\n if (\"length\" in t) {\n return t.length === 0;\n }\n\n // `size` is for Set, Map, Blob etc.\n if (\"size\" in t) {\n return t.size === 0;\n }\n\n // Super fast check for object emptiness.\n // https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object\n for (const k in t) {\n return false;\n }\n\n return true;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGO,SAAS,QAAQ,GAAqB;AAE3C,MAAI,CAAC,GAAG;AACN,WAAO;AAAA,EACT;AAGA,MAAI,OAAO,MAAM,UAAU;AACzB,WAAO;AAAA,EACT;AAGA,MAAI,YAAY,GAAG;AACjB,WAAO,EAAE,WAAW;AAAA,EACtB;AAGA,MAAI,UAAU,GAAG;AACf,WAAO,EAAE,SAAS;AAAA,EACpB;AAIA,aAAW,KAAK,GAAG;AACjB,WAAO;AAAA,EACT;AAEA,SAAO;AACT;;;ADtBO,SAAS,SACd,GACA,UAAU,SACP;AACH,MAAI,QAAQ,CAAC,GAAG;AACd,UAAM,IAAI,MAAM,SAAS,OAAO,KAAK,CAAC,EAAE;AAAA,EAC1C;AACA,SAAO;AACT;","names":[]}
|
package/nonNil.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Type asserts that `t` is neither null nor undefined.
|
|
3
3
|
* Throws an error if `t` is null or undefined.
|
|
4
|
+
*
|
|
5
|
+
* @param varName The variable name to include in the error to throw when t is
|
|
6
|
+
* nil. Defaults to 'value'.
|
|
4
7
|
*/
|
|
5
8
|
export declare function nonNil<T>(t: T | null | undefined, varName?: string): T;
|
package/nonNil.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/nonNil.ts"],"sourcesContent":["/**\n * Type asserts that `t` is neither null nor undefined.\n * Throws an error if `t` is null or undefined.\n */\nexport function nonNil<T>(t: T | null | undefined, varName = \"value\"): T {\n if (t === null || t === undefined) {\n throw new Error(`Missing ${varName}: ${t}`);\n }\n return t;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;
|
|
1
|
+
{"version":3,"sources":["../src/nonNil.ts"],"sourcesContent":["/**\n * Type asserts that `t` is neither null nor undefined.\n * Throws an error if `t` is null or undefined.\n *\n * @param varName The variable name to include in the error to throw when t is\n * nil. Defaults to 'value'.\n */\nexport function nonNil<T>(t: T | null | undefined, varName = \"value\"): T {\n if (t === null || t === undefined) {\n throw new Error(`Missing ${varName}: ${t}`);\n }\n return t;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAOO,SAAS,OAAU,GAAyB,UAAU,SAAY;AACvE,MAAI,MAAM,QAAQ,MAAM,QAAW;AACjC,UAAM,IAAI,MAAM,WAAW,OAAO,KAAK,CAAC,EAAE;AAAA,EAC5C;AACA,SAAO;AACT;","names":[]}
|
package/package.json
CHANGED
package/safeParseInt.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Returns 0 if the string is not a valid number.
|
|
3
|
+
*
|
|
4
|
+
* @param logError Log a console error if the given string is not a valid
|
|
5
|
+
* number. Defaults to false (don't log anything).
|
|
3
6
|
*/
|
|
4
7
|
export declare function safeParseInt(s: string, logError?: boolean): number;
|
package/safeParseInt.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/safeParseInt.ts"],"sourcesContent":["/**\n * Returns 0 if the string is not a valid number.\n */\nexport function safeParseInt(s: string, logError = false): number {\n const i = Number(s);\n\n if (isNaN(i)) {\n if (logError) {\n console.error(`Not a number: \"${s}\"`);\n }\n return 0;\n }\n\n return Math.floor(i);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;
|
|
1
|
+
{"version":3,"sources":["../src/safeParseInt.ts"],"sourcesContent":["/**\n * Returns 0 if the string is not a valid number.\n *\n * @param logError Log a console error if the given string is not a valid\n * number. Defaults to false (don't log anything).\n */\nexport function safeParseInt(s: string, logError = false): number {\n const i = Number(s);\n\n if (isNaN(i)) {\n if (logError) {\n console.error(`Not a number: \"${s}\"`);\n }\n return 0;\n }\n\n return Math.floor(i);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAMO,SAAS,aAAa,GAAW,WAAW,OAAe;AAChE,QAAM,IAAI,OAAO,CAAC;AAElB,MAAI,MAAM,CAAC,GAAG;AACZ,QAAI,UAAU;AACZ,cAAQ,MAAM,kBAAkB,CAAC,GAAG;AAAA,IACtC;AACA,WAAO;AAAA,EACT;AAEA,SAAO,KAAK,MAAM,CAAC;AACrB;","names":[]}
|
package/sleep.d.ts
CHANGED
package/sleep.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/sleep.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"sources":["../src/sleep.ts"],"sourcesContent":["/**\n * Sleep for a given number of milliseconds. Note that this method is async,\n * so please remember to call it with await, like `await sleep(1000);`.\n */\nexport function sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIO,SAAS,MAAM,IAA2B;AAC/C,SAAO,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,EAAE,CAAC;AACzD;","names":[]}
|
package/src/dateTimeStr.ts
CHANGED
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
export type AnyDateTime = number | Date | string;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Convert a number (epoch milliseconds), string (parseable date/time), or
|
|
5
|
+
* Date object (no conversion) into a Date object.
|
|
6
|
+
*/
|
|
7
|
+
export function toDate(ts: AnyDateTime): Date {
|
|
8
|
+
if (typeof ts === "number" || typeof ts === "string") {
|
|
9
|
+
return new Date(ts);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return ts;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Returns a date in yyyy-MM-dd format. E.g. '2000-01-02'.
|
|
17
|
+
*
|
|
18
|
+
* @param dt Specify a date object or default to the current date.
|
|
19
|
+
* @param separator Defaults to '-'.
|
|
20
|
+
*/
|
|
1
21
|
export function yyyyMmDd(dt = new Date(), separator = "-"): string {
|
|
2
22
|
const yr = dt.getFullYear();
|
|
3
23
|
const mth = dt.getMonth() + 1;
|
|
@@ -12,6 +32,12 @@ export function yyyyMmDd(dt = new Date(), separator = "-"): string {
|
|
|
12
32
|
);
|
|
13
33
|
}
|
|
14
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Returns a date in hh:mm:ss format. E.g. '01:02:03'.
|
|
37
|
+
*
|
|
38
|
+
* @param dt Specify a date object or default to the current date/time.
|
|
39
|
+
* @param separator Defaults to ':'.
|
|
40
|
+
*/
|
|
15
41
|
export function hhMmSs(dt = new Date(), separator = ":"): string {
|
|
16
42
|
const hr = dt.getHours();
|
|
17
43
|
const min = dt.getMinutes();
|
|
@@ -26,6 +52,13 @@ export function hhMmSs(dt = new Date(), separator = ":"): string {
|
|
|
26
52
|
);
|
|
27
53
|
}
|
|
28
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Returns a date in hh:mm:ss.SSS format. E.g. '01:02:03.004'.
|
|
57
|
+
*
|
|
58
|
+
* @param dt Specify a date object or default to the current date/time.
|
|
59
|
+
* @param timeSeparator Separator for hh/mm/ss. Defaults to ':'.
|
|
60
|
+
* @param msSeparator Separator before SSS. Defaults to '.'.
|
|
61
|
+
*/
|
|
29
62
|
export function hhMmSsMs(
|
|
30
63
|
dt = new Date(),
|
|
31
64
|
timeSeparator = ":",
|
|
@@ -40,6 +73,12 @@ export function hhMmSsMs(
|
|
|
40
73
|
);
|
|
41
74
|
}
|
|
42
75
|
|
|
76
|
+
/**
|
|
77
|
+
* Returns the timezone string for the given date. E.g. '+8', '-3.5'.
|
|
78
|
+
* Returns 'Z' for UTC.
|
|
79
|
+
*
|
|
80
|
+
* @param dt Specify a date object or default to the current date/time.
|
|
81
|
+
*/
|
|
43
82
|
export function tzShort(dt = new Date()): string {
|
|
44
83
|
if (dt.getTimezoneOffset() === 0) {
|
|
45
84
|
return "Z";
|
|
@@ -49,6 +88,12 @@ export function tzShort(dt = new Date()): string {
|
|
|
49
88
|
return tzHours >= 0 ? "+" + tzHours : String(tzHours);
|
|
50
89
|
}
|
|
51
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Returns the long month name, zero-indexed. E.g. 0 for 'January'.
|
|
93
|
+
*
|
|
94
|
+
* @param month Zero-indexed month.
|
|
95
|
+
* @param locales Specify the locale, e.g. 'en-US', new Intl.Locale("en-US").
|
|
96
|
+
*/
|
|
52
97
|
export function getLongMonthNameZeroIndexed(
|
|
53
98
|
month: number,
|
|
54
99
|
locales: Intl.LocalesArgument = "default",
|
|
@@ -58,6 +103,12 @@ export function getLongMonthNameZeroIndexed(
|
|
|
58
103
|
});
|
|
59
104
|
}
|
|
60
105
|
|
|
106
|
+
/**
|
|
107
|
+
* Returns the long month name, one-indexed. E.g. 1 for 'January'.
|
|
108
|
+
*
|
|
109
|
+
* @param month One-indexed month.
|
|
110
|
+
* @param locales Specify the locale, e.g. 'en-US', new Intl.Locale("en-US").
|
|
111
|
+
*/
|
|
61
112
|
export function getLongMonthNameOneIndexed(
|
|
62
113
|
month: number,
|
|
63
114
|
locales: Intl.LocalesArgument = "default",
|
|
@@ -65,6 +116,12 @@ export function getLongMonthNameOneIndexed(
|
|
|
65
116
|
return getLongMonthNameZeroIndexed(month - 1, locales);
|
|
66
117
|
}
|
|
67
118
|
|
|
119
|
+
/**
|
|
120
|
+
* Returns the short month name, zero-indexed. E.g. 0 for 'Jan'.
|
|
121
|
+
*
|
|
122
|
+
* @param month Zero-indexed month.
|
|
123
|
+
* @param locales Specify the locale, e.g. 'en-US', new Intl.Locale("en-US").
|
|
124
|
+
*/
|
|
68
125
|
export function getShortMonthNameZeroIndexed(
|
|
69
126
|
month: number,
|
|
70
127
|
locales: Intl.LocalesArgument = "default",
|
|
@@ -74,9 +131,25 @@ export function getShortMonthNameZeroIndexed(
|
|
|
74
131
|
});
|
|
75
132
|
}
|
|
76
133
|
|
|
134
|
+
/**
|
|
135
|
+
* Returns the short month name, one-indexed. E.g. 1 for 'Jan'.
|
|
136
|
+
*
|
|
137
|
+
* @param month One-indexed month.
|
|
138
|
+
* @param locales Specify the locale, e.g. 'en-US', new Intl.Locale("en-US").
|
|
139
|
+
*/
|
|
77
140
|
export function getShortMonthNameOneIndexed(
|
|
78
141
|
month: number,
|
|
79
142
|
locales: Intl.LocalesArgument = "default",
|
|
80
143
|
): string {
|
|
81
144
|
return getShortMonthNameZeroIndexed(month - 1, locales);
|
|
82
145
|
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Returns a human-readable string date/time like '2025-01-01 22:31:16Z'.
|
|
149
|
+
* Excludes the milliseconds assuming it is not necessary for display.
|
|
150
|
+
*/
|
|
151
|
+
export function getDisplayDateTime(ts: AnyDateTime) {
|
|
152
|
+
const iso = toDate(ts).toISOString();
|
|
153
|
+
const noMs = iso.slice(0, 19) + "Z";
|
|
154
|
+
return noMs.replace("T", " ");
|
|
155
|
+
}
|
package/src/duration.ts
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
* date and time durations.
|
|
4
4
|
*
|
|
5
5
|
* Note that month and year do not have fixed durations, and hence are excluded
|
|
6
|
-
* from this file.
|
|
6
|
+
* from this file. Weeks have fixed durations, but are excluded because we
|
|
7
|
+
* use days as the max duration supported.
|
|
7
8
|
*/
|
|
8
9
|
|
|
9
10
|
import {
|
|
@@ -24,8 +25,14 @@ export type Duration = {
|
|
|
24
25
|
milliseconds?: number;
|
|
25
26
|
};
|
|
26
27
|
|
|
28
|
+
/**
|
|
29
|
+
* One of: days, hours, minutes, seconds, milliseconds
|
|
30
|
+
*/
|
|
27
31
|
export type DurationType = keyof Duration;
|
|
28
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Order in which the duration type appears in the duration string.
|
|
35
|
+
*/
|
|
29
36
|
export const DURATION_TYPE_SEQUENCE: DurationType[] = [
|
|
30
37
|
"days",
|
|
31
38
|
"hours",
|
|
@@ -109,9 +116,10 @@ function getDurationTypeSeparator(style: DurationStyle): string {
|
|
|
109
116
|
|
|
110
117
|
/**
|
|
111
118
|
* Convert a milliseconds duration into a Duration object. If the given ms is
|
|
112
|
-
* zero, then return an object with a single field of zero
|
|
119
|
+
* zero, then return an object with a single field of zero with duration type
|
|
120
|
+
* of durationTypeForZero.
|
|
113
121
|
*
|
|
114
|
-
* durationTypeForZero
|
|
122
|
+
* @param durationTypeForZero Defaults to 'milliseconds'
|
|
115
123
|
*/
|
|
116
124
|
export function msToDuration(
|
|
117
125
|
ms: number,
|
|
@@ -190,7 +198,7 @@ export function durationToMs(duration: Duration): number {
|
|
|
190
198
|
* Format a given Duration object into a string. If the object has no fields,
|
|
191
199
|
* then returns an empty string.
|
|
192
200
|
*
|
|
193
|
-
* style
|
|
201
|
+
* @param style Defaults to 'short'
|
|
194
202
|
*/
|
|
195
203
|
export function formatDuration(duration: Duration, style?: DurationStyle) {
|
|
196
204
|
style = style ?? "short";
|
|
@@ -216,8 +224,8 @@ export function formatDuration(duration: Duration, style?: DurationStyle) {
|
|
|
216
224
|
/**
|
|
217
225
|
* Convert a millisecond duration into a human-readable duration string.
|
|
218
226
|
*
|
|
219
|
-
* options.durationTypeForZero - Defaults to 'milliseconds'
|
|
220
|
-
* options.style - Defaults to 'short'
|
|
227
|
+
* @param options.durationTypeForZero - Defaults to 'milliseconds'
|
|
228
|
+
* @param options.style - Defaults to 'short'
|
|
221
229
|
*/
|
|
222
230
|
export function readableDuration(
|
|
223
231
|
ms: number,
|
package/src/kvStore.ts
CHANGED
|
@@ -168,12 +168,12 @@ export class KVStore {
|
|
|
168
168
|
public async set<T>(
|
|
169
169
|
key: string,
|
|
170
170
|
value: T,
|
|
171
|
-
|
|
171
|
+
expiryDeltaMs: number = this.defaultExpiryDeltaMs,
|
|
172
172
|
): Promise<T> {
|
|
173
173
|
const obj = {
|
|
174
174
|
key,
|
|
175
175
|
value,
|
|
176
|
-
expireMs: Date.now() +
|
|
176
|
+
expireMs: Date.now() + expiryDeltaMs,
|
|
177
177
|
};
|
|
178
178
|
|
|
179
179
|
return await this.transact<T>(
|
|
@@ -380,3 +380,39 @@ export const kvStore = new KVStore(
|
|
|
380
380
|
DEFAULT_DB_VERSION,
|
|
381
381
|
DEFAULT_EXPIRY_DELTA_MS,
|
|
382
382
|
);
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Class to represent one key in the store with a default expiration.
|
|
386
|
+
*/
|
|
387
|
+
class KvStoreItem<T> {
|
|
388
|
+
public constructor(
|
|
389
|
+
public readonly key: string,
|
|
390
|
+
public readonly defaultExpiryDeltaMs: number,
|
|
391
|
+
public readonly store = kvStore,
|
|
392
|
+
) {}
|
|
393
|
+
|
|
394
|
+
public async get(): Promise<Awaited<T> | undefined> {
|
|
395
|
+
return await this.store.get(this.key);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
public async set(
|
|
399
|
+
value: T,
|
|
400
|
+
expiryDeltaMs: number = this.defaultExpiryDeltaMs,
|
|
401
|
+
): Promise<void> {
|
|
402
|
+
await this.store.set(this.key, value, expiryDeltaMs);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
public async delete(): Promise<void> {
|
|
406
|
+
await this.store.delete(this.key);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Create a KV store item with a key and a default expiration.
|
|
412
|
+
*/
|
|
413
|
+
export function kvStoreItem<T>(
|
|
414
|
+
key: string,
|
|
415
|
+
defaultExpiryDeltaMs: number,
|
|
416
|
+
): KvStoreItem<T> {
|
|
417
|
+
return new KvStoreItem<T>(key, defaultExpiryDeltaMs);
|
|
418
|
+
}
|
package/src/localStorageCache.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { Duration, durationToMs } from "./duration";
|
|
2
2
|
|
|
3
|
+
export type StoredItem<T> = { value: T; expireMs: number };
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* Simple local storage cache with support for auto-expiration.
|
|
5
7
|
* Note that this works in the browser context only because nodejs does not
|
|
@@ -12,7 +14,12 @@ import { Duration, durationToMs } from "./duration";
|
|
|
12
14
|
* In order to provide proper type-checking, please always specify the T
|
|
13
15
|
* type parameter. E.g. const item = storeItem<string>("name", 10_000);
|
|
14
16
|
*
|
|
15
|
-
*
|
|
17
|
+
* @param key The store key in local storage.
|
|
18
|
+
* @param expires Either a number in milliseconds, or a Duration object
|
|
19
|
+
* @param logError Log an error if we found an invalid object in the store.
|
|
20
|
+
* The invalid object is usually a string that cannot be parsed as JSON.
|
|
21
|
+
* @param defaultValue Specify a default value to use for the object. Defaults
|
|
22
|
+
* to undefined.
|
|
16
23
|
*/
|
|
17
24
|
export function storeItem<T>(
|
|
18
25
|
key: string,
|
|
@@ -34,21 +41,16 @@ class CacheItem<T> {
|
|
|
34
41
|
public readonly key: string,
|
|
35
42
|
public readonly expireDeltaMs: number,
|
|
36
43
|
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
|
-
}
|
|
44
|
+
public readonly defaultValue: T | undefined,
|
|
45
|
+
) {}
|
|
45
46
|
|
|
46
47
|
/**
|
|
47
48
|
* Set the value of this item with auto-expiration.
|
|
48
49
|
*/
|
|
49
50
|
public set(value: T): void {
|
|
50
51
|
const expireMs = Date.now() + this.expireDeltaMs;
|
|
51
|
-
const
|
|
52
|
+
const toStore: StoredItem<T> = { value, expireMs };
|
|
53
|
+
const valueStr = JSON.stringify(toStore);
|
|
52
54
|
|
|
53
55
|
globalThis.localStorage.setItem(this.key, valueStr);
|
|
54
56
|
}
|
|
@@ -59,13 +61,13 @@ class CacheItem<T> {
|
|
|
59
61
|
public get(): T | undefined {
|
|
60
62
|
const jsonStr = globalThis.localStorage.getItem(this.key);
|
|
61
63
|
|
|
62
|
-
if (!jsonStr
|
|
63
|
-
return
|
|
64
|
+
if (!jsonStr) {
|
|
65
|
+
return this.defaultValue;
|
|
64
66
|
}
|
|
65
67
|
|
|
66
68
|
try {
|
|
67
|
-
const obj:
|
|
68
|
-
|
|
69
|
+
const obj: StoredItem<T> | undefined = JSON.parse(jsonStr);
|
|
70
|
+
|
|
69
71
|
if (
|
|
70
72
|
!obj ||
|
|
71
73
|
typeof obj !== "object" ||
|
|
@@ -74,8 +76,8 @@ class CacheItem<T> {
|
|
|
74
76
|
typeof obj.expireMs !== "number" ||
|
|
75
77
|
Date.now() >= obj.expireMs
|
|
76
78
|
) {
|
|
77
|
-
|
|
78
|
-
return
|
|
79
|
+
this.remove();
|
|
80
|
+
return this.defaultValue;
|
|
79
81
|
}
|
|
80
82
|
|
|
81
83
|
return obj.value;
|
|
@@ -86,8 +88,8 @@ class CacheItem<T> {
|
|
|
86
88
|
e,
|
|
87
89
|
);
|
|
88
90
|
}
|
|
89
|
-
|
|
90
|
-
return
|
|
91
|
+
this.remove();
|
|
92
|
+
return this.defaultValue;
|
|
91
93
|
}
|
|
92
94
|
}
|
|
93
95
|
|
package/src/nonEmpty.ts
CHANGED
|
@@ -3,6 +3,9 @@ import { isEmpty } from "./isEmpty";
|
|
|
3
3
|
/**
|
|
4
4
|
* Type asserts that `t` is truthy.
|
|
5
5
|
* Throws an error if `t` is null or undefined.
|
|
6
|
+
*
|
|
7
|
+
* @param varName The variable name to include in the error to throw when t is
|
|
8
|
+
* empty. Defaults to 'value'.
|
|
6
9
|
*/
|
|
7
10
|
export function nonEmpty<T>(
|
|
8
11
|
t: T | null | undefined | "" | 0 | -0 | 0n | false | typeof NaN,
|
package/src/nonNil.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Type asserts that `t` is neither null nor undefined.
|
|
3
3
|
* Throws an error if `t` is null or undefined.
|
|
4
|
+
*
|
|
5
|
+
* @param varName The variable name to include in the error to throw when t is
|
|
6
|
+
* nil. Defaults to 'value'.
|
|
4
7
|
*/
|
|
5
8
|
export function nonNil<T>(t: T | null | undefined, varName = "value"): T {
|
|
6
9
|
if (t === null || t === undefined) {
|
package/src/safeParseInt.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Returns 0 if the string is not a valid number.
|
|
3
|
+
*
|
|
4
|
+
* @param logError Log a console error if the given string is not a valid
|
|
5
|
+
* number. Defaults to false (don't log anything).
|
|
3
6
|
*/
|
|
4
7
|
export function safeParseInt(s: string, logError = false): number {
|
|
5
8
|
const i = Number(s);
|
package/src/sleep.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sleep for a given number of milliseconds. Note that this method is async,
|
|
3
|
+
* so please remember to call it with await, like `await sleep(1000);`.
|
|
4
|
+
*/
|
|
1
5
|
export function sleep(ms: number): Promise<void> {
|
|
2
6
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
3
7
|
}
|