@choksheak/ts-utils 0.1.8 → 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 CHANGED
@@ -1,14 +1,68 @@
1
- export declare class DateTimeStr {
2
- static yyyyMmDd(dt?: Date, separator?: string): string;
3
- static hhMmSs(dt?: Date, separator?: string): string;
4
- static hhMmSsMs(dt?: Date, timeSeparator?: string, msSeparator?: string): string;
5
- static tz(dt?: Date): string;
6
- /** Full local date/time string. */
7
- static local(dt?: Date, dateSeparator?: string, dtSeparator?: string, timeSeparator?: string, msSeparator?: string): string;
8
- /** Use the default ISO string function to keep things simple here. */
9
- static utc(dt?: Date): string;
10
- /** Default full local date+time string with full & concise info. */
11
- static get now(): string;
12
- /** Default full UTC date+time string with full & concise info. */
13
- static get utcNow(): string;
14
- }
1
+ export 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
+ */
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
+ */
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
+ */
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
+ */
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
+ */
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
+ */
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
+ */
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
+ */
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,52 +20,79 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/dateTimeStr.ts
21
21
  var dateTimeStr_exports = {};
22
22
  __export(dateTimeStr_exports, {
23
- DateTimeStr: () => DateTimeStr
23
+ getDisplayDateTime: () => getDisplayDateTime,
24
+ getLongMonthNameOneIndexed: () => getLongMonthNameOneIndexed,
25
+ getLongMonthNameZeroIndexed: () => getLongMonthNameZeroIndexed,
26
+ getShortMonthNameOneIndexed: () => getShortMonthNameOneIndexed,
27
+ getShortMonthNameZeroIndexed: () => getShortMonthNameZeroIndexed,
28
+ hhMmSs: () => hhMmSs,
29
+ hhMmSsMs: () => hhMmSsMs,
30
+ toDate: () => toDate,
31
+ tzShort: () => tzShort,
32
+ yyyyMmDd: () => yyyyMmDd
24
33
  });
25
34
  module.exports = __toCommonJS(dateTimeStr_exports);
26
- var DateTimeStr = class _DateTimeStr {
27
- static yyyyMmDd(dt = /* @__PURE__ */ new Date(), separator = "-") {
28
- const yr = dt.getFullYear();
29
- const mth = dt.getMonth() + 1;
30
- const day = dt.getDate();
31
- return yr + separator + (mth < 10 ? "0" + mth : mth) + separator + (day < 10 ? "0" + day : day);
35
+ function toDate(ts) {
36
+ if (typeof ts === "number" || typeof ts === "string") {
37
+ return new Date(ts);
32
38
  }
33
- static hhMmSs(dt = /* @__PURE__ */ new Date(), separator = ":") {
34
- const hr = dt.getHours();
35
- const min = dt.getMinutes();
36
- const sec = dt.getSeconds();
37
- return (hr < 10 ? "0" + hr : hr) + separator + (min < 10 ? "0" + min : min) + separator + (sec < 10 ? "0" + sec : sec);
39
+ return ts;
40
+ }
41
+ function yyyyMmDd(dt = /* @__PURE__ */ new Date(), separator = "-") {
42
+ const yr = dt.getFullYear();
43
+ const mth = dt.getMonth() + 1;
44
+ const day = dt.getDate();
45
+ return yr + separator + (mth < 10 ? "0" + mth : mth) + separator + (day < 10 ? "0" + day : day);
46
+ }
47
+ function hhMmSs(dt = /* @__PURE__ */ new Date(), separator = ":") {
48
+ const hr = dt.getHours();
49
+ const min = dt.getMinutes();
50
+ const sec = dt.getSeconds();
51
+ return (hr < 10 ? "0" + hr : hr) + separator + (min < 10 ? "0" + min : min) + separator + (sec < 10 ? "0" + sec : sec);
52
+ }
53
+ function hhMmSsMs(dt = /* @__PURE__ */ new Date(), timeSeparator = ":", msSeparator = ".") {
54
+ const ms = dt.getMilliseconds();
55
+ return hhMmSs(dt, timeSeparator) + msSeparator + (ms < 10 ? "00" + ms : ms < 100 ? "0" + ms : ms);
56
+ }
57
+ function tzShort(dt = /* @__PURE__ */ new Date()) {
58
+ if (dt.getTimezoneOffset() === 0) {
59
+ return "Z";
38
60
  }
39
- static hhMmSsMs(dt = /* @__PURE__ */ new Date(), timeSeparator = ":", msSeparator = ".") {
40
- const ms = dt.getMilliseconds();
41
- return _DateTimeStr.hhMmSs(dt, timeSeparator) + msSeparator + (ms < 10 ? "00" + ms : ms < 100 ? "0" + ms : ms);
42
- }
43
- static tz(dt = /* @__PURE__ */ new Date()) {
44
- if (dt.getTimezoneOffset() === 0) {
45
- return "Z";
46
- }
47
- const tzHours = dt.getTimezoneOffset() / 60;
48
- return tzHours >= 0 ? "+" + tzHours : String(tzHours);
49
- }
50
- /** Full local date/time string. */
51
- static local(dt = /* @__PURE__ */ new Date(), dateSeparator = "-", dtSeparator = " ", timeSeparator = ":", msSeparator = ".") {
52
- return _DateTimeStr.yyyyMmDd(dt, dateSeparator) + dtSeparator + _DateTimeStr.hhMmSsMs(dt, timeSeparator, msSeparator) + _DateTimeStr.tz(dt);
53
- }
54
- /** Use the default ISO string function to keep things simple here. */
55
- static utc(dt = /* @__PURE__ */ new Date()) {
56
- return dt.toISOString();
57
- }
58
- /** Default full local date+time string with full & concise info. */
59
- static get now() {
60
- return _DateTimeStr.local();
61
- }
62
- /** Default full UTC date+time string with full & concise info. */
63
- static get utcNow() {
64
- return _DateTimeStr.utc();
65
- }
66
- };
61
+ const tzHours = dt.getTimezoneOffset() / 60;
62
+ return tzHours >= 0 ? "+" + tzHours : String(tzHours);
63
+ }
64
+ function getLongMonthNameZeroIndexed(month, locales = "default") {
65
+ return new Date(2024, month, 15).toLocaleString(locales, {
66
+ month: "long"
67
+ });
68
+ }
69
+ function getLongMonthNameOneIndexed(month, locales = "default") {
70
+ return getLongMonthNameZeroIndexed(month - 1, locales);
71
+ }
72
+ function getShortMonthNameZeroIndexed(month, locales = "default") {
73
+ return new Date(2e3, month, 15).toLocaleString(locales, {
74
+ month: "short"
75
+ });
76
+ }
77
+ function getShortMonthNameOneIndexed(month, locales = "default") {
78
+ return getShortMonthNameZeroIndexed(month - 1, locales);
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
+ }
67
85
  // Annotate the CommonJS export names for ESM import in node:
68
86
  0 && (module.exports = {
69
- DateTimeStr
87
+ getDisplayDateTime,
88
+ getLongMonthNameOneIndexed,
89
+ getLongMonthNameZeroIndexed,
90
+ getShortMonthNameOneIndexed,
91
+ getShortMonthNameZeroIndexed,
92
+ hhMmSs,
93
+ hhMmSsMs,
94
+ toDate,
95
+ tzShort,
96
+ yyyyMmDd
70
97
  });
71
98
  //# sourceMappingURL=dateTimeStr.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/dateTimeStr.ts"],"sourcesContent":["export class DateTimeStr {\n public static 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 public static 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 public static hhMmSsMs(\n dt = new Date(),\n timeSeparator = \":\",\n msSeparator = \".\",\n ): string {\n const ms = dt.getMilliseconds();\n\n return (\n DateTimeStr.hhMmSs(dt, timeSeparator) +\n msSeparator +\n (ms < 10 ? \"00\" + ms : ms < 100 ? \"0\" + ms : ms)\n );\n }\n\n public static tz(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 /** Full local date/time string. */\n public static local(\n dt = new Date(),\n dateSeparator = \"-\",\n dtSeparator = \" \",\n timeSeparator = \":\",\n msSeparator = \".\",\n ): string {\n return (\n DateTimeStr.yyyyMmDd(dt, dateSeparator) +\n dtSeparator +\n DateTimeStr.hhMmSsMs(dt, timeSeparator, msSeparator) +\n DateTimeStr.tz(dt)\n );\n }\n\n /** Use the default ISO string function to keep things simple here. */\n public static utc(dt = new Date()) {\n return dt.toISOString();\n }\n\n /** Default full local date+time string with full & concise info. */\n public static get now() {\n return DateTimeStr.local();\n }\n\n /** Default full UTC date+time string with full & concise info. */\n public static get utcNow() {\n return DateTimeStr.utc();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAO,IAAM,cAAN,MAAM,aAAY;AAAA,EACvB,OAAc,SAAS,KAAK,oBAAI,KAAK,GAAG,YAAY,KAAa;AAC/D,UAAM,KAAK,GAAG,YAAY;AAC1B,UAAM,MAAM,GAAG,SAAS,IAAI;AAC5B,UAAM,MAAM,GAAG,QAAQ;AAEvB,WACE,KACA,aACC,MAAM,KAAK,MAAM,MAAM,OACxB,aACC,MAAM,KAAK,MAAM,MAAM;AAAA,EAE5B;AAAA,EAEA,OAAc,OAAO,KAAK,oBAAI,KAAK,GAAG,YAAY,KAAa;AAC7D,UAAM,KAAK,GAAG,SAAS;AACvB,UAAM,MAAM,GAAG,WAAW;AAC1B,UAAM,MAAM,GAAG,WAAW;AAE1B,YACG,KAAK,KAAK,MAAM,KAAK,MACtB,aACC,MAAM,KAAK,MAAM,MAAM,OACxB,aACC,MAAM,KAAK,MAAM,MAAM;AAAA,EAE5B;AAAA,EAEA,OAAc,SACZ,KAAK,oBAAI,KAAK,GACd,gBAAgB,KAChB,cAAc,KACN;AACR,UAAM,KAAK,GAAG,gBAAgB;AAE9B,WACE,aAAY,OAAO,IAAI,aAAa,IACpC,eACC,KAAK,KAAK,OAAO,KAAK,KAAK,MAAM,MAAM,KAAK;AAAA,EAEjD;AAAA,EAEA,OAAc,GAAG,KAAK,oBAAI,KAAK,GAAW;AACxC,QAAI,GAAG,kBAAkB,MAAM,GAAG;AAChC,aAAO;AAAA,IACT;AAEA,UAAM,UAAU,GAAG,kBAAkB,IAAI;AACzC,WAAO,WAAW,IAAI,MAAM,UAAU,OAAO,OAAO;AAAA,EACtD;AAAA;AAAA,EAGA,OAAc,MACZ,KAAK,oBAAI,KAAK,GACd,gBAAgB,KAChB,cAAc,KACd,gBAAgB,KAChB,cAAc,KACN;AACR,WACE,aAAY,SAAS,IAAI,aAAa,IACtC,cACA,aAAY,SAAS,IAAI,eAAe,WAAW,IACnD,aAAY,GAAG,EAAE;AAAA,EAErB;AAAA;AAAA,EAGA,OAAc,IAAI,KAAK,oBAAI,KAAK,GAAG;AACjC,WAAO,GAAG,YAAY;AAAA,EACxB;AAAA;AAAA,EAGA,WAAkB,MAAM;AACtB,WAAO,aAAY,MAAM;AAAA,EAC3B;AAAA;AAAA,EAGA,WAAkB,SAAS;AACzB,WAAO,aAAY,IAAI;AAAA,EACzB;AACF;","names":[]}
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,oBAAI,KAAK,GAAG,YAAY,KAAa;AACjE,QAAM,KAAK,GAAG,YAAY;AAC1B,QAAM,MAAM,GAAG,SAAS,IAAI;AAC5B,QAAM,MAAM,GAAG,QAAQ;AAEvB,SACE,KACA,aACC,MAAM,KAAK,MAAM,MAAM,OACxB,aACC,MAAM,KAAK,MAAM,MAAM;AAE5B;AAQO,SAAS,OAAO,KAAK,oBAAI,KAAK,GAAG,YAAY,KAAa;AAC/D,QAAM,KAAK,GAAG,SAAS;AACvB,QAAM,MAAM,GAAG,WAAW;AAC1B,QAAM,MAAM,GAAG,WAAW;AAE1B,UACG,KAAK,KAAK,MAAM,KAAK,MACtB,aACC,MAAM,KAAK,MAAM,MAAM,OACxB,aACC,MAAM,KAAK,MAAM,MAAM;AAE5B;AASO,SAAS,SACd,KAAK,oBAAI,KAAK,GACd,gBAAgB,KAChB,cAAc,KACN;AACR,QAAM,KAAK,GAAG,gBAAgB;AAE9B,SACE,OAAO,IAAI,aAAa,IACxB,eACC,KAAK,KAAK,OAAO,KAAK,KAAK,MAAM,MAAM,KAAK;AAEjD;AAQO,SAAS,QAAQ,KAAK,oBAAI,KAAK,GAAW;AAC/C,MAAI,GAAG,kBAAkB,MAAM,GAAG;AAChC,WAAO;AAAA,EACT;AAEA,QAAM,UAAU,GAAG,kBAAkB,IAAI;AACzC,SAAO,WAAW,IAAI,MAAM,UAAU,OAAO,OAAO;AACtD;AAQO,SAAS,4BACd,OACA,UAAgC,WACxB;AACR,SAAO,IAAI,KAAK,MAAM,OAAO,EAAE,EAAE,eAAe,SAAS;AAAA,IACvD,OAAO;AAAA,EACT,CAAC;AACH;AAQO,SAAS,2BACd,OACA,UAAgC,WACxB;AACR,SAAO,4BAA4B,QAAQ,GAAG,OAAO;AACvD;AAQO,SAAS,6BACd,OACA,UAAgC,WACxB;AACR,SAAO,IAAI,KAAK,KAAM,OAAO,EAAE,EAAE,eAAe,SAAS;AAAA,IACvD,OAAO;AAAA,EACT,CAAC;AACH;AAQO,SAAS,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 ADDED
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Bunch of miscellaneous constants and utility functions related to handling
3
+ * date and time durations.
4
+ *
5
+ * Note that month and year do not have fixed durations, and hence are excluded
6
+ * from this file. Weeks have fixed durations, but are excluded because we
7
+ * use days as the max duration supported.
8
+ */
9
+ export type Duration = {
10
+ days?: number;
11
+ hours?: number;
12
+ minutes?: number;
13
+ seconds?: number;
14
+ milliseconds?: number;
15
+ };
16
+ /**
17
+ * One of: days, hours, minutes, seconds, milliseconds
18
+ */
19
+ export type DurationType = keyof Duration;
20
+ /**
21
+ * Order in which the duration type appears in the duration string.
22
+ */
23
+ export declare const DURATION_TYPE_SEQUENCE: DurationType[];
24
+ /**
25
+ * Follows the same format as Intl.DurationFormat.prototype.format().
26
+ *
27
+ * Short: 1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns
28
+ * Long: 1 year, 2 months, 3 weeks, 3 days, 4 hours, 5 minutes, 6 seconds,
29
+ * 7 milliseconds, 8 microseconds, 9 nanoseconds
30
+ * Narrow: 1y 2mo 3w 3d 4h 5m 6s 7ms 8μs 9ns
31
+ */
32
+ export type DurationStyle = "short" | "long" | "narrow";
33
+ export type DurationSuffixMap = {
34
+ short: string;
35
+ shorts: string;
36
+ long: string;
37
+ longs: string;
38
+ narrow: string;
39
+ };
40
+ export type DurationSuffixType = keyof DurationSuffixMap;
41
+ export declare const DURATION_STYLE_SUFFIX_MAP: Record<DurationType, DurationSuffixMap>;
42
+ /**
43
+ * Convert a milliseconds duration into a Duration object. If the given ms is
44
+ * zero, then return an object with a single field of zero with duration type
45
+ * of durationTypeForZero.
46
+ *
47
+ * @param durationTypeForZero Defaults to 'milliseconds'
48
+ */
49
+ export declare function msToDuration(ms: number, durationTypeForZero?: DurationType): Duration;
50
+ /**
51
+ * Returns the number of milliseconds for the given duration.
52
+ */
53
+ export declare function durationToMs(duration: Duration): number;
54
+ /**
55
+ * Format a given Duration object into a string. If the object has no fields,
56
+ * then returns an empty string.
57
+ *
58
+ * @param style Defaults to 'short'
59
+ */
60
+ export declare function formatDuration(duration: Duration, style?: DurationStyle): string;
61
+ /**
62
+ * Convert a millisecond duration into a human-readable duration string.
63
+ *
64
+ * @param options.durationTypeForZero - Defaults to 'milliseconds'
65
+ * @param options.style - Defaults to 'short'
66
+ */
67
+ export declare function readableDuration(ms: number, options?: {
68
+ durationTypeForZero?: DurationType;
69
+ style?: DurationStyle;
70
+ }): string;
package/duration.js ADDED
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/duration.ts
21
+ var duration_exports = {};
22
+ __export(duration_exports, {
23
+ DURATION_STYLE_SUFFIX_MAP: () => DURATION_STYLE_SUFFIX_MAP,
24
+ DURATION_TYPE_SEQUENCE: () => DURATION_TYPE_SEQUENCE,
25
+ durationToMs: () => durationToMs,
26
+ formatDuration: () => formatDuration,
27
+ msToDuration: () => msToDuration,
28
+ readableDuration: () => readableDuration
29
+ });
30
+ module.exports = __toCommonJS(duration_exports);
31
+
32
+ // src/timeConstants.ts
33
+ var MS_PER_SECOND = 1e3;
34
+ var MS_PER_MINUTE = 6e4;
35
+ var MS_PER_HOUR = 36e5;
36
+ var MS_PER_DAY = 864e5;
37
+ var SECONDS_PER_MINUTE = 60;
38
+ var MINUTES_PER_HOUR = 60;
39
+ var HOURS_PER_DAY = 24;
40
+
41
+ // src/duration.ts
42
+ var DURATION_TYPE_SEQUENCE = [
43
+ "days",
44
+ "hours",
45
+ "minutes",
46
+ "seconds",
47
+ "milliseconds"
48
+ ];
49
+ var DURATION_STYLE_SUFFIX_MAP = {
50
+ days: {
51
+ short: "day",
52
+ shorts: "days",
53
+ long: "day",
54
+ longs: "days",
55
+ narrow: "d"
56
+ },
57
+ hours: {
58
+ short: "hr",
59
+ shorts: "hrs",
60
+ long: "hour",
61
+ longs: "hours",
62
+ narrow: "h"
63
+ },
64
+ minutes: {
65
+ short: "min",
66
+ shorts: "mins",
67
+ long: "minute",
68
+ longs: "minutes",
69
+ narrow: "m"
70
+ },
71
+ seconds: {
72
+ short: "sec",
73
+ shorts: "secs",
74
+ long: "second",
75
+ longs: "seconds",
76
+ narrow: "s"
77
+ },
78
+ milliseconds: {
79
+ short: "ms",
80
+ shorts: "ms",
81
+ long: "millisecond",
82
+ longs: "milliseconds",
83
+ narrow: "ms"
84
+ }
85
+ };
86
+ function getDurationStyleForPlural(style) {
87
+ return style == "short" ? "shorts" : style === "long" ? "longs" : style;
88
+ }
89
+ function getValueAndUnitSeparator(style) {
90
+ return style === "narrow" ? "" : " ";
91
+ }
92
+ function getDurationTypeSeparator(style) {
93
+ return style === "narrow" ? " " : ", ";
94
+ }
95
+ function msToDuration(ms, durationTypeForZero) {
96
+ if (ms === 0) {
97
+ durationTypeForZero = durationTypeForZero != null ? durationTypeForZero : "milliseconds";
98
+ return { [durationTypeForZero]: 0 };
99
+ }
100
+ const duration = {};
101
+ for (let i = 0; i < 1; i++) {
102
+ let seconds = Math.floor(ms / MS_PER_SECOND);
103
+ const millis = ms - seconds * MS_PER_SECOND;
104
+ if (millis > 0) {
105
+ duration["milliseconds"] = millis;
106
+ }
107
+ if (seconds === 0) {
108
+ break;
109
+ }
110
+ let minutes = Math.floor(seconds / SECONDS_PER_MINUTE);
111
+ seconds -= minutes * SECONDS_PER_MINUTE;
112
+ if (seconds > 0) {
113
+ duration["seconds"] = seconds;
114
+ }
115
+ if (minutes === 0) {
116
+ break;
117
+ }
118
+ let hours = Math.floor(minutes / MINUTES_PER_HOUR);
119
+ minutes -= hours * MINUTES_PER_HOUR;
120
+ if (minutes > 0) {
121
+ duration["minutes"] = minutes;
122
+ }
123
+ if (hours === 0) {
124
+ break;
125
+ }
126
+ const days = Math.floor(hours / HOURS_PER_DAY);
127
+ hours -= days * HOURS_PER_DAY;
128
+ if (hours > 0) {
129
+ duration["hours"] = hours;
130
+ }
131
+ if (days > 0) {
132
+ duration["days"] = days;
133
+ }
134
+ }
135
+ return duration;
136
+ }
137
+ function durationToMs(duration) {
138
+ var _a, _b, _c, _d, _e;
139
+ const daysMs = ((_a = duration.days) != null ? _a : 0) * MS_PER_DAY;
140
+ const hoursMs = ((_b = duration.hours) != null ? _b : 0) * MS_PER_HOUR;
141
+ const minsMs = ((_c = duration.minutes) != null ? _c : 0) * MS_PER_MINUTE;
142
+ const secsMs = ((_d = duration.seconds) != null ? _d : 0) * MS_PER_SECOND;
143
+ const msMs = (_e = duration.milliseconds) != null ? _e : 0;
144
+ return daysMs + hoursMs + minsMs + secsMs + msMs;
145
+ }
146
+ function formatDuration(duration, style) {
147
+ style = style != null ? style : "short";
148
+ const stylePlural = getDurationStyleForPlural(style);
149
+ const space = getValueAndUnitSeparator(style);
150
+ const a = [];
151
+ for (const unit of DURATION_TYPE_SEQUENCE) {
152
+ const value = duration[unit];
153
+ if (value === void 0) continue;
154
+ const suffixMap = DURATION_STYLE_SUFFIX_MAP[unit];
155
+ const suffix = value === 1 ? suffixMap[style] : suffixMap[stylePlural];
156
+ a.push(value + space + suffix);
157
+ }
158
+ const separator = getDurationTypeSeparator(style);
159
+ return a.join(separator);
160
+ }
161
+ function readableDuration(ms, options) {
162
+ const duration = msToDuration(ms, options == null ? void 0 : options.durationTypeForZero);
163
+ return formatDuration(duration, options == null ? void 0 : options.style);
164
+ }
165
+ // Annotate the CommonJS export names for ESM import in node:
166
+ 0 && (module.exports = {
167
+ DURATION_STYLE_SUFFIX_MAP,
168
+ DURATION_TYPE_SEQUENCE,
169
+ durationToMs,
170
+ formatDuration,
171
+ msToDuration,
172
+ readableDuration
173
+ });
174
+ //# sourceMappingURL=duration.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/duration.ts","../src/timeConstants.ts"],"sourcesContent":["/**\n * Bunch of miscellaneous constants and utility functions related to handling\n * date and time durations.\n *\n * Note that month and year do not have fixed durations, and hence are excluded\n * from this file. 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;\nexport const HOURS_PER_WEEK = 168;\n"],"mappings":"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","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, expireDeltaMs?: number): Promise<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, expireDeltaMs = this.defaultExpiryDeltaMs) {
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() + expireDeltaMs
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