@choksheak/ts-utils 0.3.6 → 0.3.7

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.
Files changed (256) hide show
  1. package/arrayBuffer.cjs +39 -10
  2. package/arrayBuffer.min.cjs +1 -1
  3. package/arrayBuffer.min.cjs.map +1 -1
  4. package/arrayBuffer.min.mjs +1 -1
  5. package/arrayBuffer.min.mjs.map +1 -1
  6. package/arrayBuffer.mjs +10 -7
  7. package/asNumber.cjs +38 -6
  8. package/asNumber.min.cjs +1 -1
  9. package/asNumber.min.cjs.map +1 -1
  10. package/asNumber.min.mjs +1 -1
  11. package/asNumber.min.mjs.map +1 -1
  12. package/asNumber.mjs +11 -4
  13. package/assert.cjs +27 -4
  14. package/assert.min.cjs +1 -1
  15. package/assert.min.cjs.map +1 -1
  16. package/assert.min.mjs +1 -1
  17. package/assert.min.mjs.map +1 -1
  18. package/assert.mjs +0 -2
  19. package/base64Url.cjs +29 -5
  20. package/base64Url.min.cjs +1 -1
  21. package/base64Url.min.cjs.map +1 -1
  22. package/base64Url.min.mjs +1 -1
  23. package/base64Url.min.mjs.map +1 -1
  24. package/base64Url.mjs +0 -2
  25. package/capLength.cjs +71 -7
  26. package/capLength.min.cjs +5 -1
  27. package/capLength.min.cjs.map +1 -1
  28. package/capLength.min.mjs +5 -1
  29. package/capLength.min.mjs.map +1 -1
  30. package/capLength.mjs +41 -4
  31. package/concatIterators.cjs +27 -4
  32. package/concatIterators.min.cjs +1 -1
  33. package/concatIterators.min.cjs.map +1 -1
  34. package/concatIterators.min.mjs +1 -1
  35. package/concatIterators.min.mjs.map +1 -1
  36. package/concatIterators.mjs +0 -2
  37. package/dateTimeStr.cjs +119 -31
  38. package/dateTimeStr.min.cjs +1 -1
  39. package/dateTimeStr.min.cjs.map +1 -1
  40. package/dateTimeStr.min.mjs +1 -1
  41. package/dateTimeStr.min.mjs.map +1 -1
  42. package/dateTimeStr.mjs +69 -17
  43. package/duration.cjs +188 -24
  44. package/duration.min.cjs +1 -1
  45. package/duration.min.cjs.map +1 -1
  46. package/duration.min.mjs +1 -1
  47. package/duration.min.mjs.map +1 -1
  48. package/duration.mjs +144 -14
  49. package/getEnv.cjs +31 -7
  50. package/getEnv.min.cjs +1 -1
  51. package/getEnv.min.cjs.map +1 -1
  52. package/getEnv.min.mjs +1 -1
  53. package/getEnv.min.mjs.map +1 -1
  54. package/getEnv.mjs +0 -2
  55. package/isEmpty.cjs +46 -8
  56. package/isEmpty.min.cjs +1 -1
  57. package/isEmpty.min.cjs.map +1 -1
  58. package/isEmpty.min.mjs +1 -1
  59. package/isEmpty.min.mjs.map +1 -1
  60. package/isEmpty.mjs +19 -6
  61. package/isPromise.cjs +29 -5
  62. package/isPromise.min.cjs +1 -1
  63. package/isPromise.min.cjs.map +1 -1
  64. package/isPromise.min.mjs +1 -1
  65. package/isPromise.min.mjs.map +1 -1
  66. package/isPromise.mjs +0 -2
  67. package/kvStore.cjs +353 -15
  68. package/kvStore.min.cjs +1 -1
  69. package/kvStore.min.cjs.map +1 -1
  70. package/kvStore.min.mjs +1 -1
  71. package/kvStore.min.mjs.map +1 -1
  72. package/kvStore.mjs +319 -12
  73. package/localStore.cjs +264 -15
  74. package/localStore.min.cjs +1 -1
  75. package/localStore.min.cjs.map +1 -1
  76. package/localStore.min.mjs +1 -1
  77. package/localStore.min.mjs.map +1 -1
  78. package/localStore.mjs +230 -12
  79. package/mean.cjs +48 -8
  80. package/mean.min.cjs +1 -1
  81. package/mean.min.cjs.map +1 -1
  82. package/mean.min.mjs +1 -1
  83. package/mean.min.mjs.map +1 -1
  84. package/mean.mjs +18 -5
  85. package/median.cjs +41 -7
  86. package/median.min.cjs +1 -1
  87. package/median.min.cjs.map +1 -1
  88. package/median.min.mjs +1 -1
  89. package/median.min.mjs.map +1 -1
  90. package/median.mjs +11 -4
  91. package/nonEmpty.cjs +48 -8
  92. package/nonEmpty.min.cjs +1 -1
  93. package/nonEmpty.min.cjs.map +1 -1
  94. package/nonEmpty.min.mjs +1 -1
  95. package/nonEmpty.min.mjs.map +1 -1
  96. package/nonEmpty.mjs +19 -6
  97. package/nonNil.cjs +27 -4
  98. package/nonNil.min.cjs +1 -1
  99. package/nonNil.min.cjs.map +1 -1
  100. package/nonNil.min.mjs +1 -1
  101. package/nonNil.min.mjs.map +1 -1
  102. package/nonNil.mjs +0 -2
  103. package/package.json +1 -257
  104. package/round.cjs +32 -6
  105. package/round.min.cjs +1 -1
  106. package/round.min.cjs.map +1 -1
  107. package/round.min.mjs +1 -1
  108. package/round.min.mjs.map +1 -1
  109. package/round.mjs +5 -4
  110. package/roundToString.cjs +35 -7
  111. package/roundToString.min.cjs +1 -1
  112. package/roundToString.min.cjs.map +1 -1
  113. package/roundToString.min.mjs +1 -1
  114. package/roundToString.min.mjs.map +1 -1
  115. package/roundToString.mjs +5 -4
  116. package/safeBtoa.cjs +27 -4
  117. package/safeBtoa.min.cjs +1 -1
  118. package/safeBtoa.min.cjs.map +1 -1
  119. package/safeBtoa.min.mjs +1 -1
  120. package/safeBtoa.min.mjs.map +1 -1
  121. package/safeBtoa.mjs +0 -2
  122. package/sha256.cjs +34 -8
  123. package/sha256.min.cjs +1 -1
  124. package/sha256.min.cjs.map +1 -1
  125. package/sha256.min.mjs +1 -1
  126. package/sha256.min.mjs.map +1 -1
  127. package/sha256.mjs +7 -6
  128. package/sleep.cjs +27 -4
  129. package/sleep.min.cjs +1 -1
  130. package/sleep.min.cjs.map +1 -1
  131. package/sleep.min.mjs +1 -1
  132. package/sleep.min.mjs.map +1 -1
  133. package/sleep.mjs +0 -2
  134. package/storageAdapter.cjs +18 -1
  135. package/storageAdapter.min.cjs +2 -1
  136. package/storageAdapter.min.cjs.map +1 -1
  137. package/sum.cjs +45 -5
  138. package/sum.min.cjs +1 -1
  139. package/sum.min.cjs.map +1 -1
  140. package/sum.min.mjs +1 -1
  141. package/sum.min.mjs.map +1 -1
  142. package/sum.mjs +18 -5
  143. package/timeConstants.cjs +69 -35
  144. package/timeConstants.min.cjs +1 -1
  145. package/timeConstants.min.cjs.map +1 -1
  146. package/timeConstants.min.mjs +1 -1
  147. package/timeConstants.min.mjs.map +1 -1
  148. package/timeConstants.mjs +15 -19
  149. package/timer.cjs +180 -7
  150. package/timer.min.cjs +1 -1
  151. package/timer.min.cjs.map +1 -1
  152. package/timer.min.mjs +1 -1
  153. package/timer.min.mjs.map +1 -1
  154. package/timer.mjs +154 -8
  155. package/toReadableString.cjs +68 -6
  156. package/toReadableString.min.cjs +5 -1
  157. package/toReadableString.min.cjs.map +1 -1
  158. package/toReadableString.min.mjs +5 -1
  159. package/toReadableString.min.mjs.map +1 -1
  160. package/toReadableString.mjs +41 -4
  161. package/chunk-22JDWCOS.min.cjs +0 -2
  162. package/chunk-22JDWCOS.min.cjs.map +0 -1
  163. package/chunk-244NBT4Q.min.mjs +0 -2
  164. package/chunk-244NBT4Q.min.mjs.map +0 -1
  165. package/chunk-27MBP3ES.min.cjs +0 -2
  166. package/chunk-27MBP3ES.min.cjs.map +0 -1
  167. package/chunk-34ENP2B3.min.mjs +0 -438
  168. package/chunk-34ENP2B3.min.mjs.map +0 -1
  169. package/chunk-3EBQ2GO2.min.cjs +0 -2
  170. package/chunk-3EBQ2GO2.min.cjs.map +0 -1
  171. package/chunk-4GLOLTJ5.cjs +0 -167
  172. package/chunk-6TQBMXTL.min.cjs +0 -2
  173. package/chunk-6TQBMXTL.min.cjs.map +0 -1
  174. package/chunk-AWZBCFMG.min.cjs +0 -2
  175. package/chunk-AWZBCFMG.min.cjs.map +0 -1
  176. package/chunk-AXNETZET.cjs +0 -20
  177. package/chunk-BKZM6NWX.min.cjs +0 -2
  178. package/chunk-BKZM6NWX.min.cjs.map +0 -1
  179. package/chunk-BZKQXX3A.cjs +0 -40
  180. package/chunk-CERFUU5I.cjs +0 -93
  181. package/chunk-CSTKFJ6G.mjs +0 -239
  182. package/chunk-DUN4UY2Q.cjs +0 -239
  183. package/chunk-EDJJUP4K.min.cjs +0 -6
  184. package/chunk-EDJJUP4K.min.cjs.map +0 -1
  185. package/chunk-FFNQBZH5.cjs +0 -14
  186. package/chunk-FZZRSHSH.min.mjs +0 -6
  187. package/chunk-FZZRSHSH.min.mjs.map +0 -1
  188. package/chunk-GAP2HS5M.min.cjs +0 -2
  189. package/chunk-GAP2HS5M.min.cjs.map +0 -1
  190. package/chunk-GLYMAPS4.min.mjs +0 -2
  191. package/chunk-GLYMAPS4.min.mjs.map +0 -1
  192. package/chunk-I65KPVMO.min.cjs +0 -438
  193. package/chunk-I65KPVMO.min.cjs.map +0 -1
  194. package/chunk-ILGTU46T.cjs +0 -39
  195. package/chunk-IOKRXHKE.min.mjs +0 -2
  196. package/chunk-IOKRXHKE.min.mjs.map +0 -1
  197. package/chunk-ISP4SMRC.min.cjs +0 -2
  198. package/chunk-ISP4SMRC.min.cjs.map +0 -1
  199. package/chunk-KETP5HBF.cjs +0 -24
  200. package/chunk-KLHNGJ4Y.min.mjs +0 -2
  201. package/chunk-KLHNGJ4Y.min.mjs.map +0 -1
  202. package/chunk-KMNTU3DJ.mjs +0 -45
  203. package/chunk-LDVEXETI.cjs +0 -45
  204. package/chunk-LZENHV35.min.cjs +0 -2
  205. package/chunk-LZENHV35.min.cjs.map +0 -1
  206. package/chunk-M5PIM2HB.mjs +0 -42
  207. package/chunk-M6PYQYOX.mjs +0 -39
  208. package/chunk-MN5EYNZN.min.mjs +0 -2
  209. package/chunk-MN5EYNZN.min.mjs.map +0 -1
  210. package/chunk-NRIBJ2OJ.mjs +0 -16031
  211. package/chunk-NVLYXWLX.cjs +0 -32
  212. package/chunk-O352F2MT.mjs +0 -24
  213. package/chunk-O6B2TKFK.min.mjs +0 -2
  214. package/chunk-O6B2TKFK.min.mjs.map +0 -1
  215. package/chunk-OGF34H4T.min.cjs +0 -2
  216. package/chunk-OGF34H4T.min.cjs.map +0 -1
  217. package/chunk-OXSWWWOM.min.mjs +0 -2
  218. package/chunk-OXSWWWOM.min.mjs.map +0 -1
  219. package/chunk-PRUGHVRT.mjs +0 -14
  220. package/chunk-Q4WCMSOW.min.mjs +0 -2
  221. package/chunk-Q4WCMSOW.min.mjs.map +0 -1
  222. package/chunk-QH74JZCK.mjs +0 -9
  223. package/chunk-QNNJNLGK.min.cjs +0 -2
  224. package/chunk-QNNJNLGK.min.cjs.map +0 -1
  225. package/chunk-QWCFN7ZM.mjs +0 -15
  226. package/chunk-SDM2OPT6.mjs +0 -32
  227. package/chunk-SHKRZCF2.cjs +0 -16031
  228. package/chunk-SJDJDNDN.min.cjs +0 -2
  229. package/chunk-SJDJDNDN.min.cjs.map +0 -1
  230. package/chunk-SULNMALN.cjs +0 -328
  231. package/chunk-SV35RBZ7.min.mjs +0 -2
  232. package/chunk-SV35RBZ7.min.mjs.map +0 -1
  233. package/chunk-TJFYGFOW.mjs +0 -328
  234. package/chunk-UHRYBUUX.min.mjs +0 -2
  235. package/chunk-UHRYBUUX.min.mjs.map +0 -1
  236. package/chunk-UOU6IP3S.min.mjs +0 -2
  237. package/chunk-UOU6IP3S.min.mjs.map +0 -1
  238. package/chunk-V76XVNZ5.mjs +0 -20
  239. package/chunk-VFYQ7I5V.min.mjs +0 -2
  240. package/chunk-VFYQ7I5V.min.mjs.map +0 -1
  241. package/chunk-VG7VV7J2.mjs +0 -167
  242. package/chunk-VJCV56DO.cjs +0 -15
  243. package/chunk-VREA6AKG.cjs +0 -9
  244. package/chunk-W5I6Y5FK.min.mjs +0 -2
  245. package/chunk-W5I6Y5FK.min.mjs.map +0 -1
  246. package/chunk-Y7MC73BZ.cjs +0 -42
  247. package/chunk-YVWROQKU.mjs +0 -40
  248. package/chunk-ZKIZ5SWK.min.cjs +0 -2
  249. package/chunk-ZKIZ5SWK.min.cjs.map +0 -1
  250. package/chunk-ZZZB7NJX.mjs +0 -93
  251. package/magic-string.es-4UY437WL.min.mjs +0 -14
  252. package/magic-string.es-4UY437WL.min.mjs.map +0 -1
  253. package/magic-string.es-OAQWZQTY.mjs +0 -1323
  254. package/magic-string.es-UE2UVQEL.cjs +0 -1323
  255. package/magic-string.es-X6DNUB6Y.min.cjs +0 -14
  256. package/magic-string.es-X6DNUB6Y.min.cjs.map +0 -1
package/dateTimeStr.cjs CHANGED
@@ -1,31 +1,119 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
- var _chunkCERFUU5Icjs = require('./chunk-CERFUU5I.cjs');
16
- require('./chunk-ILGTU46T.cjs');
17
- _chunkCERFUU5Icjs.init_dateTimeStr.call(void 0, );
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
- exports.getDisplayDateTime = _chunkCERFUU5Icjs.getDisplayDateTime; exports.getLongMonthNameOneIndexed = _chunkCERFUU5Icjs.getLongMonthNameOneIndexed; exports.getLongMonthNameZeroIndexed = _chunkCERFUU5Icjs.getLongMonthNameZeroIndexed; exports.getShortMonthNameOneIndexed = _chunkCERFUU5Icjs.getShortMonthNameOneIndexed; exports.getShortMonthNameZeroIndexed = _chunkCERFUU5Icjs.getShortMonthNameZeroIndexed; exports.hhMm = _chunkCERFUU5Icjs.hhMm; exports.hhMmSs = _chunkCERFUU5Icjs.hhMmSs; exports.hhMmSsMs = _chunkCERFUU5Icjs.hhMmSsMs; exports.toDate = _chunkCERFUU5Icjs.toDate; exports.tzShort = _chunkCERFUU5Icjs.tzShort; exports.yyyyMm = _chunkCERFUU5Icjs.yyyyMm; exports.yyyyMmDd = _chunkCERFUU5Icjs.yyyyMmDd;
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/dateTimeStr.ts
21
+ var dateTimeStr_exports = {};
22
+ __export(dateTimeStr_exports, {
23
+ getDisplayDateTime: () => getDisplayDateTime,
24
+ getLongMonthNameOneIndexed: () => getLongMonthNameOneIndexed,
25
+ getLongMonthNameZeroIndexed: () => getLongMonthNameZeroIndexed,
26
+ getShortMonthNameOneIndexed: () => getShortMonthNameOneIndexed,
27
+ getShortMonthNameZeroIndexed: () => getShortMonthNameZeroIndexed,
28
+ hhMm: () => hhMm,
29
+ hhMmSs: () => hhMmSs,
30
+ hhMmSsMs: () => hhMmSsMs,
31
+ toDate: () => toDate,
32
+ tzShort: () => tzShort,
33
+ yyyyMm: () => yyyyMm,
34
+ yyyyMmDd: () => yyyyMmDd
35
+ });
36
+ module.exports = __toCommonJS(dateTimeStr_exports);
37
+ function isAllDigits(str) {
38
+ return /^\d+$/.test(str);
39
+ }
40
+ function toDate(ts) {
41
+ if (typeof ts === "number") {
42
+ if (ts < 1e11) {
43
+ return new Date(ts * 1e3);
44
+ }
45
+ return new Date(ts);
46
+ }
47
+ if (typeof ts === "string") {
48
+ if (isAllDigits(ts)) {
49
+ return toDate(Number(ts));
50
+ }
51
+ return new Date(ts);
52
+ }
53
+ return ts;
54
+ }
55
+ function yyyyMm(dt = /* @__PURE__ */ new Date(), separator = "-") {
56
+ const yr = dt.getFullYear();
57
+ const mth = dt.getMonth() + 1;
58
+ return yr + separator + (mth < 10 ? "0" + mth : mth);
59
+ }
60
+ function yyyyMmDd(dt = /* @__PURE__ */ new Date(), separator = "-") {
61
+ const day = dt.getDate();
62
+ return yyyyMm(dt, separator) + separator + (day < 10 ? "0" + day : day);
63
+ }
64
+ function hhMm(dt = /* @__PURE__ */ new Date(), separator = ":") {
65
+ const hr = dt.getHours();
66
+ const min = dt.getMinutes();
67
+ return (hr < 10 ? "0" + hr : hr) + separator + (min < 10 ? "0" + min : min);
68
+ }
69
+ function hhMmSs(dt = /* @__PURE__ */ new Date(), separator = ":") {
70
+ const sec = dt.getSeconds();
71
+ return hhMm(dt, separator) + separator + (sec < 10 ? "0" + sec : sec);
72
+ }
73
+ function hhMmSsMs(dt = /* @__PURE__ */ new Date(), timeSeparator = ":", msSeparator = ".") {
74
+ const ms = dt.getMilliseconds();
75
+ return hhMmSs(dt, timeSeparator) + msSeparator + (ms < 10 ? "00" + ms : ms < 100 ? "0" + ms : ms);
76
+ }
77
+ function tzShort(dt = /* @__PURE__ */ new Date()) {
78
+ if (dt.getTimezoneOffset() === 0) {
79
+ return "Z";
80
+ }
81
+ const tzHours = dt.getTimezoneOffset() / 60;
82
+ return tzHours >= 0 ? "+" + tzHours : String(tzHours);
83
+ }
84
+ function getLongMonthNameZeroIndexed(month, locales = "default") {
85
+ return new Date(2024, month, 15).toLocaleString(locales, {
86
+ month: "long"
87
+ });
88
+ }
89
+ function getLongMonthNameOneIndexed(month, locales = "default") {
90
+ return getLongMonthNameZeroIndexed(month - 1, locales);
91
+ }
92
+ function getShortMonthNameZeroIndexed(month, locales = "default") {
93
+ return new Date(2e3, month, 15).toLocaleString(locales, {
94
+ month: "short"
95
+ });
96
+ }
97
+ function getShortMonthNameOneIndexed(month, locales = "default") {
98
+ return getShortMonthNameZeroIndexed(month - 1, locales);
99
+ }
100
+ function getDisplayDateTime(ts) {
101
+ const iso = toDate(ts).toISOString();
102
+ const noMs = iso.slice(0, 19) + "Z";
103
+ return noMs.replace("T", " ");
104
+ }
105
+ // Annotate the CommonJS export names for ESM import in node:
106
+ 0 && (module.exports = {
107
+ getDisplayDateTime,
108
+ getLongMonthNameOneIndexed,
109
+ getLongMonthNameZeroIndexed,
110
+ getShortMonthNameOneIndexed,
111
+ getShortMonthNameZeroIndexed,
112
+ hhMm,
113
+ hhMmSs,
114
+ hhMmSsMs,
115
+ toDate,
116
+ tzShort,
117
+ yyyyMm,
118
+ yyyyMmDd
119
+ });
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkSJDJDNDNmincjs = require('./chunk-SJDJDNDN.min.cjs');require('./chunk-QNNJNLGK.min.cjs');_chunkSJDJDNDNmincjs.m.call(void 0, );exports.getDisplayDateTime = _chunkSJDJDNDNmincjs.l; exports.getLongMonthNameOneIndexed = _chunkSJDJDNDNmincjs.i; exports.getLongMonthNameZeroIndexed = _chunkSJDJDNDNmincjs.h; exports.getShortMonthNameOneIndexed = _chunkSJDJDNDNmincjs.k; exports.getShortMonthNameZeroIndexed = _chunkSJDJDNDNmincjs.j; exports.hhMm = _chunkSJDJDNDNmincjs.d; exports.hhMmSs = _chunkSJDJDNDNmincjs.e; exports.hhMmSsMs = _chunkSJDJDNDNmincjs.f; exports.toDate = _chunkSJDJDNDNmincjs.a; exports.tzShort = _chunkSJDJDNDNmincjs.g; exports.yyyyMm = _chunkSJDJDNDNmincjs.b; exports.yyyyMmDd = _chunkSJDJDNDNmincjs.c;
1
+ "use strict";var i=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var l=Object.getOwnPropertyNames;var D=Object.prototype.hasOwnProperty;var h=(e,t)=>{for(var n in t)i(e,n,{get:t[n],enumerable:!0})},x=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of l(t))!D.call(e,o)&&o!==n&&i(e,o,{get:()=>t[o],enumerable:!(r=f(t,o))||r.enumerable});return e};var y=e=>x(i({},"__esModule",{value:!0}),e);var A={};h(A,{getDisplayDateTime:()=>b,getLongMonthNameOneIndexed:()=>S,getLongMonthNameZeroIndexed:()=>c,getShortMonthNameOneIndexed:()=>I,getShortMonthNameZeroIndexed:()=>m,hhMm:()=>g,hhMmSs:()=>a,hhMmSsMs:()=>d,toDate:()=>u,tzShort:()=>w,yyyyMm:()=>s,yyyyMmDd:()=>M});module.exports=y(A);function p(e){return/^\d+$/.test(e)}function u(e){return typeof e=="number"?e<1e11?new Date(e*1e3):new Date(e):typeof e=="string"?p(e)?u(Number(e)):new Date(e):e}function s(e=new Date,t="-"){let n=e.getFullYear(),r=e.getMonth()+1;return n+t+(r<10?"0"+r:r)}function M(e=new Date,t="-"){let n=e.getDate();return s(e,t)+t+(n<10?"0"+n:n)}function g(e=new Date,t=":"){let n=e.getHours(),r=e.getMinutes();return(n<10?"0"+n:n)+t+(r<10?"0"+r:r)}function a(e=new Date,t=":"){let n=e.getSeconds();return g(e,t)+t+(n<10?"0"+n:n)}function d(e=new Date,t=":",n="."){let r=e.getMilliseconds();return a(e,t)+n+(r<10?"00"+r:r<100?"0"+r:r)}function w(e=new Date){if(e.getTimezoneOffset()===0)return"Z";let t=e.getTimezoneOffset()/60;return t>=0?"+"+t:String(t)}function c(e,t="default"){return new Date(2024,e,15).toLocaleString(t,{month:"long"})}function S(e,t="default"){return c(e-1,t)}function m(e,t="default"){return new Date(2e3,e,15).toLocaleString(t,{month:"short"})}function I(e,t="default"){return m(e-1,t)}function b(e){return(u(e).toISOString().slice(0,19)+"Z").replace("T"," ")}0&&(module.exports={getDisplayDateTime,getLongMonthNameOneIndexed,getLongMonthNameZeroIndexed,getShortMonthNameOneIndexed,getShortMonthNameZeroIndexed,hhMm,hhMmSs,hhMmSsMs,toDate,tzShort,yyyyMm,yyyyMmDd});
2
2
  //# sourceMappingURL=dateTimeStr.min.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["/Users/choksheaklau/src/ts-utils/dist/dateTimeStr.min.cjs"],"names":[],"mappings":"AAAA,wIAA+D,oCAAiC,oCAAE,CAAE,CAAC,8kBAA0P","file":"/Users/choksheaklau/src/ts-utils/dist/dateTimeStr.min.cjs"}
1
+ {"version":3,"sources":["../src/dateTimeStr.ts"],"sourcesContent":["export type AnyDateTime = number | Date | string;\n\nfunction isAllDigits(str: string): boolean {\n return /^\\d+$/.test(str);\n}\n\n/**\n * Convert a number (epoch seconds or milliseconds), string (parseable\n * date/time or epoch seconds or milliseconds), or Date object (no conversion)\n * into a Date object.\n */\nexport function toDate(ts: AnyDateTime): Date {\n if (typeof ts === \"number\") {\n // Handle timestamp in seconds (less than 12 digits long).\n if (ts < 1_000_000_000_00) {\n return new Date(ts * 1000);\n }\n\n // Handle timestamp in milliseconds.\n return new Date(ts);\n }\n\n if (typeof ts === \"string\") {\n if (isAllDigits(ts)) {\n return toDate(Number(ts));\n }\n\n return new Date(ts);\n }\n\n return ts;\n}\n\n/**\n * Returns a date in yyyy-MM format. E.g. '2000-01'.\n *\n * @param dt Specify a date object or default to the current date.\n * @param separator Defaults to '-'.\n */\nexport function yyyyMm(dt = new Date(), separator = \"-\"): string {\n const yr = dt.getFullYear();\n const mth = dt.getMonth() + 1;\n\n return yr + separator + (mth < 10 ? \"0\" + mth : mth);\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 day = dt.getDate();\n\n return yyyyMm(dt, separator) + separator + (day < 10 ? \"0\" + day : day);\n}\n\n/**\n * Returns a date in hh:mm format. E.g. '01:02'.\n *\n * @param dt Specify a date object or default to the current date/time.\n * @param separator Defaults to ':'.\n */\nexport function hhMm(dt = new Date(), separator = \":\"): string {\n const hr = dt.getHours();\n const min = dt.getMinutes();\n\n return (hr < 10 ? \"0\" + hr : hr) + separator + (min < 10 ? \"0\" + min : min);\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 sec = dt.getSeconds();\n\n return hhMm(dt, separator) + separator + (sec < 10 ? \"0\" + sec : sec);\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":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,wBAAAE,EAAA,+BAAAC,EAAA,gCAAAC,EAAA,gCAAAC,EAAA,iCAAAC,EAAA,SAAAC,EAAA,WAAAC,EAAA,aAAAC,EAAA,WAAAC,EAAA,YAAAC,EAAA,WAAAC,EAAA,aAAAC,IAAA,eAAAC,EAAAd,GAEA,SAASe,EAAYC,EAAsB,CACzC,MAAO,QAAQ,KAAKA,CAAG,CACzB,CAOO,SAASN,EAAOO,EAAuB,CAC5C,OAAI,OAAOA,GAAO,SAEZA,EAAK,KACA,IAAI,KAAKA,EAAK,GAAI,EAIpB,IAAI,KAAKA,CAAE,EAGhB,OAAOA,GAAO,SACZF,EAAYE,CAAE,EACTP,EAAO,OAAOO,CAAE,CAAC,EAGnB,IAAI,KAAKA,CAAE,EAGbA,CACT,CAQO,SAASL,EAAOM,EAAK,IAAI,KAAQC,EAAY,IAAa,CAC/D,IAAMC,EAAKF,EAAG,YAAY,EACpBG,EAAMH,EAAG,SAAS,EAAI,EAE5B,OAAOE,EAAKD,GAAaE,EAAM,GAAK,IAAMA,EAAMA,EAClD,CAQO,SAASR,EAASK,EAAK,IAAI,KAAQC,EAAY,IAAa,CACjE,IAAMG,EAAMJ,EAAG,QAAQ,EAEvB,OA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AA6BwB,EAAQ,EAAGC,CAAO,CACxD,CAMO,SAAS7B,EAAmBe,EAAiB,CAGlD,OAFYP,EAAOO,CAAE,EAAE,YAAY,EAClB,MAAM,EAAG,EAAE,EAAI,KACpB,QAAQ,IAAK,GAAG,CAC9B","names":["dateTimeStr_exports","__export","getDisplayDateTime","getLongMonthNameOneIndexed","getLongMonthNameZeroIndexed","getShortMonthNameOneIndexed","getShortMonthNameZeroIndexed","hhMm","hhMmSs","hhMmSsMs","toDate","tzShort","yyyyMm","yyyyMmDd","__toCommonJS","isAllDigits","str","ts","dt","separator","yr","mth","day","hr","min","sec","timeSeparator","msSeparator","ms","tzHours","month","locales"]}
@@ -1,2 +1,2 @@
1
- import{a,b,c,d,e,f,g,h,i,j,k,l,m}from"./chunk-SV35RBZ7.min.mjs";import"./chunk-UOU6IP3S.min.mjs";m();export{l as getDisplayDateTime,i as getLongMonthNameOneIndexed,h as getLongMonthNameZeroIndexed,k as getShortMonthNameOneIndexed,j as getShortMonthNameZeroIndexed,d as hhMm,e as hhMmSs,f as hhMmSsMs,a as toDate,g as tzShort,b as yyyyMm,c as yyyyMmDd};
1
+ function i(e){return/^\d+$/.test(e)}function o(e){return typeof e=="number"?e<1e11?new Date(e*1e3):new Date(e):typeof e=="string"?i(e)?o(Number(e)):new Date(e):e}function u(e=new Date,t="-"){let n=e.getFullYear(),r=e.getMonth()+1;return n+t+(r<10?"0"+r:r)}function m(e=new Date,t="-"){let n=e.getDate();return u(e,t)+t+(n<10?"0"+n:n)}function s(e=new Date,t=":"){let n=e.getHours(),r=e.getMinutes();return(n<10?"0"+n:n)+t+(r<10?"0"+r:r)}function g(e=new Date,t=":"){let n=e.getSeconds();return s(e,t)+t+(n<10?"0"+n:n)}function f(e=new Date,t=":",n="."){let r=e.getMilliseconds();return g(e,t)+n+(r<10?"00"+r:r<100?"0"+r:r)}function l(e=new Date){if(e.getTimezoneOffset()===0)return"Z";let t=e.getTimezoneOffset()/60;return t>=0?"+"+t:String(t)}function a(e,t="default"){return new Date(2024,e,15).toLocaleString(t,{month:"long"})}function D(e,t="default"){return a(e-1,t)}function c(e,t="default"){return new Date(2e3,e,15).toLocaleString(t,{month:"short"})}function h(e,t="default"){return c(e-1,t)}function x(e){return(o(e).toISOString().slice(0,19)+"Z").replace("T"," ")}export{x as getDisplayDateTime,D as getLongMonthNameOneIndexed,a as getLongMonthNameZeroIndexed,h as getShortMonthNameOneIndexed,c as getShortMonthNameZeroIndexed,s as hhMm,g as hhMmSs,f as hhMmSsMs,o as toDate,l as tzShort,u as yyyyMm,m as yyyyMmDd};
2
2
  //# sourceMappingURL=dateTimeStr.min.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
1
+ {"version":3,"sources":["../src/dateTimeStr.ts"],"sourcesContent":["export type AnyDateTime = number | Date | string;\n\nfunction isAllDigits(str: string): boolean {\n return /^\\d+$/.test(str);\n}\n\n/**\n * Convert a number (epoch seconds or milliseconds), string (parseable\n * date/time or epoch seconds or milliseconds), or Date object (no conversion)\n * into a Date object.\n */\nexport function toDate(ts: AnyDateTime): Date {\n if (typeof ts === \"number\") {\n // Handle timestamp in seconds (less than 12 digits long).\n if (ts < 1_000_000_000_00) {\n return new Date(ts * 1000);\n }\n\n // Handle timestamp in milliseconds.\n return new Date(ts);\n }\n\n if (typeof ts === \"string\") {\n if (isAllDigits(ts)) {\n return toDate(Number(ts));\n }\n\n return new Date(ts);\n }\n\n return ts;\n}\n\n/**\n * Returns a date in yyyy-MM format. E.g. '2000-01'.\n *\n * @param dt Specify a date object or default to the current date.\n * @param separator Defaults to '-'.\n */\nexport function yyyyMm(dt = new Date(), separator = \"-\"): string {\n const yr = dt.getFullYear();\n const mth = dt.getMonth() + 1;\n\n return yr + separator + (mth < 10 ? \"0\" + mth : mth);\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 day = dt.getDate();\n\n return yyyyMm(dt, separator) + separator + (day < 10 ? \"0\" + day : day);\n}\n\n/**\n * Returns a date in hh:mm format. E.g. '01:02'.\n *\n * @param dt Specify a date object or default to the current date/time.\n * @param separator Defaults to ':'.\n */\nexport function hhMm(dt = new Date(), separator = \":\"): string {\n const hr = dt.getHours();\n const min = dt.getMinutes();\n\n return (hr < 10 ? \"0\" + hr : hr) + separator + (min < 10 ? \"0\" + min : min);\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 sec = dt.getSeconds();\n\n return hhMm(dt, separator) + separator + (sec < 10 ? \"0\" + sec : sec);\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":"AAEA,SAASA,EAAYC,EAAsB,CACzC,MAAO,QAAQ,KAAKA,CAAG,CACzB,CAOO,SAASC,EAAOC,EAAuB,CAC5C,OAAI,OAAOA,GAAO,SAEZA,EAAK,KACA,IAAI,KAAKA,EAAK,GAAI,EAIpB,IAAI,KAAKA,CAAE,EAGhB,OAAOA,GAAO,SACZH,EAAYG,CAAE,EACTD,EAAO,OAAOC,CAAE,CAAC,EAGnB,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,MAAO,MACT,CAAC,CACH,CAQO,SAASC,EACdF,EACAC,EAAgC,UACxB,CACR,OAAOF,EAA4BC,EAAQ,EAAGC,CAAO,CACvD,CAQO,SAASE,EACdH,EACAC,EAAgC,UACxB,CACR,OAAO,IAAI,KAAK,IAAMD,EAAO,EAAE,EAAE,eAAeC,EAAS,CACvD,MAAO,OACT,CAAC,CACH,CAQO,SAASG,EACdJ,EACAC,EAAgC,UACxB,CACR,OAAOE,EAA6BH,EAAQ,EAAGC,CAAO,CACxD,CAMO,SAASI,EAAmBzB,EAAiB,CAGlD,OAFYD,EAAOC,CAAE,EAAE,YAAY,EAClB,MAAM,EAAG,EAAE,EAAI,KACpB,QAAQ,IAAK,GAAG,CAC9B","names":["isAllDigits","str","toDate","ts","yyyyMm","dt","separator","yr","mth","yyyyMmDd","day","hhMm","hr","min","hhMmSs","sec","hhMmSsMs","timeSeparator","msSeparator","ms","tzShort","tzHours","getLongMonthNameZeroIndexed","month","locales","getLongMonthNameOneIndexed","getShortMonthNameZeroIndexed","getShortMonthNameOneIndexed","getDisplayDateTime"]}
package/dateTimeStr.mjs CHANGED
@@ -1,20 +1,72 @@
1
- import {
2
- getDisplayDateTime,
3
- getLongMonthNameOneIndexed,
4
- getLongMonthNameZeroIndexed,
5
- getShortMonthNameOneIndexed,
6
- getShortMonthNameZeroIndexed,
7
- hhMm,
8
- hhMmSs,
9
- hhMmSsMs,
10
- init_dateTimeStr,
11
- toDate,
12
- tzShort,
13
- yyyyMm,
14
- yyyyMmDd
15
- } from "./chunk-ZZZB7NJX.mjs";
16
- import "./chunk-M6PYQYOX.mjs";
17
- init_dateTimeStr();
1
+ // src/dateTimeStr.ts
2
+ function isAllDigits(str) {
3
+ return /^\d+$/.test(str);
4
+ }
5
+ function toDate(ts) {
6
+ if (typeof ts === "number") {
7
+ if (ts < 1e11) {
8
+ return new Date(ts * 1e3);
9
+ }
10
+ return new Date(ts);
11
+ }
12
+ if (typeof ts === "string") {
13
+ if (isAllDigits(ts)) {
14
+ return toDate(Number(ts));
15
+ }
16
+ return new Date(ts);
17
+ }
18
+ return ts;
19
+ }
20
+ function yyyyMm(dt = /* @__PURE__ */ new Date(), separator = "-") {
21
+ const yr = dt.getFullYear();
22
+ const mth = dt.getMonth() + 1;
23
+ return yr + separator + (mth < 10 ? "0" + mth : mth);
24
+ }
25
+ function yyyyMmDd(dt = /* @__PURE__ */ new Date(), separator = "-") {
26
+ const day = dt.getDate();
27
+ return yyyyMm(dt, separator) + separator + (day < 10 ? "0" + day : day);
28
+ }
29
+ function hhMm(dt = /* @__PURE__ */ new Date(), separator = ":") {
30
+ const hr = dt.getHours();
31
+ const min = dt.getMinutes();
32
+ return (hr < 10 ? "0" + hr : hr) + separator + (min < 10 ? "0" + min : min);
33
+ }
34
+ function hhMmSs(dt = /* @__PURE__ */ new Date(), separator = ":") {
35
+ const sec = dt.getSeconds();
36
+ return hhMm(dt, separator) + separator + (sec < 10 ? "0" + sec : sec);
37
+ }
38
+ function hhMmSsMs(dt = /* @__PURE__ */ new Date(), timeSeparator = ":", msSeparator = ".") {
39
+ const ms = dt.getMilliseconds();
40
+ return hhMmSs(dt, timeSeparator) + msSeparator + (ms < 10 ? "00" + ms : ms < 100 ? "0" + ms : ms);
41
+ }
42
+ function tzShort(dt = /* @__PURE__ */ new Date()) {
43
+ if (dt.getTimezoneOffset() === 0) {
44
+ return "Z";
45
+ }
46
+ const tzHours = dt.getTimezoneOffset() / 60;
47
+ return tzHours >= 0 ? "+" + tzHours : String(tzHours);
48
+ }
49
+ function getLongMonthNameZeroIndexed(month, locales = "default") {
50
+ return new Date(2024, month, 15).toLocaleString(locales, {
51
+ month: "long"
52
+ });
53
+ }
54
+ function getLongMonthNameOneIndexed(month, locales = "default") {
55
+ return getLongMonthNameZeroIndexed(month - 1, locales);
56
+ }
57
+ function getShortMonthNameZeroIndexed(month, locales = "default") {
58
+ return new Date(2e3, month, 15).toLocaleString(locales, {
59
+ month: "short"
60
+ });
61
+ }
62
+ function getShortMonthNameOneIndexed(month, locales = "default") {
63
+ return getShortMonthNameZeroIndexed(month - 1, locales);
64
+ }
65
+ function getDisplayDateTime(ts) {
66
+ const iso = toDate(ts).toISOString();
67
+ const noMs = iso.slice(0, 19) + "Z";
68
+ return noMs.replace("T", " ");
69
+ }
18
70
  export {
19
71
  getDisplayDateTime,
20
72
  getLongMonthNameOneIndexed,
package/duration.cjs CHANGED
@@ -1,24 +1,188 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
- var _chunk4GLOLTJ5cjs = require('./chunk-4GLOLTJ5.cjs');
12
- require('./chunk-Y7MC73BZ.cjs');
13
- require('./chunk-ILGTU46T.cjs');
14
- _chunk4GLOLTJ5cjs.init_duration.call(void 0, );
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
- exports.DURATION_STYLE_SUFFIX_MAP = _chunk4GLOLTJ5cjs.DURATION_STYLE_SUFFIX_MAP; exports.DURATION_TYPE_SEQUENCE = _chunk4GLOLTJ5cjs.DURATION_TYPE_SEQUENCE; exports.durationOrMsToMs = _chunk4GLOLTJ5cjs.durationOrMsToMs; exports.durationToMs = _chunk4GLOLTJ5cjs.durationToMs; exports.elapsed = _chunk4GLOLTJ5cjs.elapsed; exports.formatDuration = _chunk4GLOLTJ5cjs.formatDuration; exports.msToDuration = _chunk4GLOLTJ5cjs.msToDuration; exports.readableDuration = _chunk4GLOLTJ5cjs.readableDuration;
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
+ durationOrMsToMs: () => durationOrMsToMs,
26
+ durationToMs: () => durationToMs,
27
+ elapsed: () => elapsed,
28
+ formatDuration: () => formatDuration,
29
+ msToDuration: () => msToDuration,
30
+ readableDuration: () => readableDuration
31
+ });
32
+ module.exports = __toCommonJS(duration_exports);
33
+
34
+ // src/timeConstants.ts
35
+ var MS_PER_SECOND = 1e3;
36
+ var MS_PER_MINUTE = 6e4;
37
+ var MS_PER_HOUR = 36e5;
38
+ var MS_PER_DAY = 864e5;
39
+ var SECONDS_PER_MINUTE = 60;
40
+ var MINUTES_PER_HOUR = 60;
41
+ var HOURS_PER_DAY = 24;
42
+
43
+ // src/duration.ts
44
+ var DURATION_TYPE_SEQUENCE = [
45
+ "days",
46
+ "hours",
47
+ "minutes",
48
+ "seconds",
49
+ "milliseconds"
50
+ ];
51
+ var DURATION_STYLE_SUFFIX_MAP = {
52
+ days: {
53
+ short: "day",
54
+ shorts: "days",
55
+ long: "day",
56
+ longs: "days",
57
+ narrow: "d"
58
+ },
59
+ hours: {
60
+ short: "hr",
61
+ shorts: "hrs",
62
+ long: "hour",
63
+ longs: "hours",
64
+ narrow: "h"
65
+ },
66
+ minutes: {
67
+ short: "min",
68
+ shorts: "mins",
69
+ long: "minute",
70
+ longs: "minutes",
71
+ narrow: "m"
72
+ },
73
+ seconds: {
74
+ short: "sec",
75
+ shorts: "secs",
76
+ long: "second",
77
+ longs: "seconds",
78
+ narrow: "s"
79
+ },
80
+ milliseconds: {
81
+ short: "ms",
82
+ shorts: "ms",
83
+ long: "millisecond",
84
+ longs: "milliseconds",
85
+ narrow: "ms"
86
+ }
87
+ };
88
+ function getDurationStyleForPlural(style) {
89
+ return style == "short" ? "shorts" : style === "long" ? "longs" : style;
90
+ }
91
+ function getValueAndUnitSeparator(style) {
92
+ return style === "narrow" ? "" : " ";
93
+ }
94
+ function getDurationTypeSeparator(style) {
95
+ return style === "narrow" ? " " : ", ";
96
+ }
97
+ function msToDuration(ms, durationTypeForZero) {
98
+ if (ms === 0) {
99
+ durationTypeForZero = durationTypeForZero ?? "milliseconds";
100
+ return { [durationTypeForZero]: 0 };
101
+ }
102
+ const duration = {};
103
+ for (let i = 0; i < 1; i++) {
104
+ let seconds = Math.floor(ms / MS_PER_SECOND);
105
+ const millis = ms - seconds * MS_PER_SECOND;
106
+ if (millis > 0) {
107
+ duration["milliseconds"] = millis;
108
+ }
109
+ if (seconds === 0) {
110
+ break;
111
+ }
112
+ let minutes = Math.floor(seconds / SECONDS_PER_MINUTE);
113
+ seconds -= minutes * SECONDS_PER_MINUTE;
114
+ if (seconds > 0) {
115
+ duration["seconds"] = seconds;
116
+ }
117
+ if (minutes === 0) {
118
+ break;
119
+ }
120
+ let hours = Math.floor(minutes / MINUTES_PER_HOUR);
121
+ minutes -= hours * MINUTES_PER_HOUR;
122
+ if (minutes > 0) {
123
+ duration["minutes"] = minutes;
124
+ }
125
+ if (hours === 0) {
126
+ break;
127
+ }
128
+ const days = Math.floor(hours / HOURS_PER_DAY);
129
+ hours -= days * HOURS_PER_DAY;
130
+ if (hours > 0) {
131
+ duration["hours"] = hours;
132
+ }
133
+ if (days > 0) {
134
+ duration["days"] = days;
135
+ }
136
+ }
137
+ return duration;
138
+ }
139
+ function durationToMs(duration) {
140
+ const daysMs = (duration.days ?? 0) * MS_PER_DAY;
141
+ const hoursMs = (duration.hours ?? 0) * MS_PER_HOUR;
142
+ const minsMs = (duration.minutes ?? 0) * MS_PER_MINUTE;
143
+ const secsMs = (duration.seconds ?? 0) * MS_PER_SECOND;
144
+ const msMs = duration.milliseconds ?? 0;
145
+ return daysMs + hoursMs + minsMs + secsMs + msMs;
146
+ }
147
+ function durationOrMsToMs(duration) {
148
+ return typeof duration === "number" ? duration : durationToMs(duration);
149
+ }
150
+ function formatDuration(duration, style) {
151
+ style = style ?? "short";
152
+ const stylePlural = getDurationStyleForPlural(style);
153
+ const space = getValueAndUnitSeparator(style);
154
+ const a = [];
155
+ for (const unit of DURATION_TYPE_SEQUENCE) {
156
+ const value = duration[unit];
157
+ if (value === void 0) continue;
158
+ const suffixMap = DURATION_STYLE_SUFFIX_MAP[unit];
159
+ const suffix = value === 1 ? suffixMap[style] : suffixMap[stylePlural];
160
+ a.push(value + space + suffix);
161
+ }
162
+ const separator = getDurationTypeSeparator(style);
163
+ return a.join(separator);
164
+ }
165
+ function readableDuration(ms, options) {
166
+ const duration = msToDuration(ms, options?.durationTypeForZero);
167
+ return formatDuration(duration, options?.style);
168
+ }
169
+ function elapsed(ms) {
170
+ if (ms > MS_PER_MINUTE) {
171
+ return readableDuration(ms);
172
+ }
173
+ if (ms > 100) {
174
+ return `${(ms / 1e3).toFixed(3)}s`;
175
+ }
176
+ return ms + "ms";
177
+ }
178
+ // Annotate the CommonJS export names for ESM import in node:
179
+ 0 && (module.exports = {
180
+ DURATION_STYLE_SUFFIX_MAP,
181
+ DURATION_TYPE_SEQUENCE,
182
+ durationOrMsToMs,
183
+ durationToMs,
184
+ elapsed,
185
+ formatDuration,
186
+ msToDuration,
187
+ readableDuration
188
+ });
package/duration.min.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunk27MBP3ESmincjs = require('./chunk-27MBP3ES.min.cjs');require('./chunk-GAP2HS5M.min.cjs');require('./chunk-QNNJNLGK.min.cjs');_chunk27MBP3ESmincjs.i.call(void 0, );exports.DURATION_STYLE_SUFFIX_MAP = _chunk27MBP3ESmincjs.b; exports.DURATION_TYPE_SEQUENCE = _chunk27MBP3ESmincjs.a; exports.durationOrMsToMs = _chunk27MBP3ESmincjs.e; exports.durationToMs = _chunk27MBP3ESmincjs.d; exports.elapsed = _chunk27MBP3ESmincjs.h; exports.formatDuration = _chunk27MBP3ESmincjs.f; exports.msToDuration = _chunk27MBP3ESmincjs.c; exports.readableDuration = _chunk27MBP3ESmincjs.g;
1
+ "use strict";var l=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var M=Object.prototype.hasOwnProperty;var x=(o,n)=>{for(var r in n)l(o,r,{get:n[r],enumerable:!0})},_=(o,n,r,e)=>{if(n&&typeof n=="object"||typeof n=="function")for(let t of S(n))!M.call(o,t)&&t!==r&&l(o,t,{get:()=>n[t],enumerable:!(e=g(n,t))||e.enumerable});return o};var T=o=>_(l({},"__esModule",{value:!0}),o);var I={};x(I,{DURATION_STYLE_SUFFIX_MAP:()=>p,DURATION_TYPE_SEQUENCE:()=>f,durationOrMsToMs:()=>O,durationToMs:()=>D,elapsed:()=>A,formatDuration:()=>d,msToDuration:()=>m,readableDuration:()=>h});module.exports=T(I);var f=["days","hours","minutes","seconds","milliseconds"],p={days:{short:"day",shorts:"days",long:"day",longs:"days",narrow:"d"},hours:{short:"hr",shorts:"hrs",long:"hour",longs:"hours",narrow:"h"},minutes:{short:"min",shorts:"mins",long:"minute",longs:"minutes",narrow:"m"},seconds:{short:"sec",shorts:"secs",long:"second",longs:"seconds",narrow:"s"},milliseconds:{short:"ms",shorts:"ms",long:"millisecond",longs:"milliseconds",narrow:"ms"}};function U(o){return o=="short"?"shorts":o==="long"?"longs":o}function w(o){return o==="narrow"?"":" "}function N(o){return o==="narrow"?" ":", "}function m(o,n){if(o===0)return n=n??"milliseconds",{[n]:0};let r={};for(let e=0;e<1;e++){let t=Math.floor(o/1e3),u=o-t*1e3;if(u>0&&(r.milliseconds=u),t===0)break;let i=Math.floor(t/60);if(t-=i*60,t>0&&(r.seconds=t),i===0)break;let s=Math.floor(i/60);if(i-=s*60,i>0&&(r.minutes=i),s===0)break;let a=Math.floor(s/24);s-=a*24,s>0&&(r.hours=s),a>0&&(r.days=a)}return r}function D(o){let n=(o.days??0)*864e5,r=(o.hours??0)*36e5,e=(o.minutes??0)*6e4,t=(o.seconds??0)*1e3,u=o.milliseconds??0;return n+r+e+t+u}function O(o){return typeof o=="number"?o:D(o)}function d(o,n){n=n??"short";let r=U(n),e=w(n),t=[];for(let i of f){let s=o[i];if(s===void 0)continue;let a=p[i],y=s===1?a[n]:a[r];t.push(s+e+y)}let u=N(n);return t.join(u)}function h(o,n){let r=m(o,n?.durationTypeForZero);return d(r,n?.style)}function A(o){return o>6e4?h(o):o>100?`${(o/1e3).toFixed(3)}s`:o+"ms"}0&&(module.exports={DURATION_STYLE_SUFFIX_MAP,DURATION_TYPE_SEQUENCE,durationOrMsToMs,durationToMs,elapsed,formatDuration,msToDuration,readableDuration});
2
2
  //# sourceMappingURL=duration.min.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["/Users/choksheaklau/src/ts-utils/dist/duration.min.cjs"],"names":[],"mappings":"AAAA,wIAAuD,oCAAiC,oCAAiC,oCAAE,CAAE,CAAC,mZAAmL","file":"/Users/choksheaklau/src/ts-utils/dist/duration.min.cjs"}
1
+ {"version":3,"sources":["../src/duration.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 HOURS_PER_DAY,\n MINUTES_PER_HOUR,\n MS_PER_DAY,\n MS_PER_HOUR,\n MS_PER_MINUTE,\n MS_PER_SECOND,\n SECONDS_PER_MINUTE,\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 * Convenience function to return a duration given an ms or Duration.\n */\nexport function durationOrMsToMs(duration: number | Duration): number {\n return typeof duration === \"number\" ? duration : durationToMs(duration);\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/** A shortened duration string useful for logging timings. */\nexport function elapsed(ms: number): string {\n // Use long format for 1 minute or over.\n if (ms > MS_PER_MINUTE) {\n return readableDuration(ms);\n }\n\n // Use seconds format for over 100ms.\n if (ms > 100) {\n return `${(ms / 1000).toFixed(3)}s`;\n }\n\n // Use milliseconds format.\n return ms + \"ms\";\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,+BAAAE,EAAA,2BAAAC,EAAA,qBAAAC,EAAA,iBAAAC,EAAA,YAAAC,EAAA,mBAAAC,EAAA,iBAAAC,EAAA,qBAAAC,IAAA,eAAAC,EAAAV,GAmCO,IAAMW,EAAyC,CACpD,OACA,QACA,UACA,UACA,cACF,EAsBaC,EAGT,CACF,KAAM,CACJ,MAAO,MACP,OAAQ,OACR,KAAM,MACN,MAAO,OACP,OAAQ,GACV,EACA,MAAO,CACL,MAAO,KACP,OAAQ,MACR,KAAM,OACN,MAAO,QACP,OAAQ,GACV,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AAK,MAChCS,GAAWT,EAAS,OAAS,GAAK,KAClCU,GAAUV,EAAS,SAAW,GAAK,IACnCW,GAAUX,EAAS,SAAW,GAAK,IACnCY,EAAOZ,EAAS,cAAgB,EAEtC,OAAOQ,EAASC,EAAUC,EAASC,EAASC,CAC9C,CAKO,SAASC,EAAiBb,EAAqC,CACpE,OAAO,OAAOA,GAAa,SAAWA,EAAWO,EAAaP,CAAQ,CACxE,CAQO,SAASc,EAAed,EAAoBN,EAAuB,CACxEA,EAAQA,GAAS,QACjB,IAAMqB,EAActB,EAA0BC,CAAK,EAE7CsB,EAAQrB,EAAyBD,CAAK,EAEtCuB,EAAc,CAAC,EAErB,QAAWC,KAAQ3B,EAAwB,CACzC,IAAM4B,EAAQnB,EAASkB,CAAI,EAC3B,GAAIC,IAAU,OAAW,SAEzB,IAAMC,EAAY5B,EAA0B0B,CAAI,EAC1CG,EAASF,IAAU,EAAIC,EAAU1B,CAAK,EAAI0B,EAAUL,CAAW,EACrEE,EAAE,KAAKE,EAAQH,EAAQK,CAAM,CAC/B,CAEA,IAAMC,EAAY1B,EAAyBF,CAAK,EAChD,OAAOuB,EAAE,KAAKK,CAAS,CACzB,CAQO,SAASC,EACdzB,EACA0B,EACQ,CACR,IAAMxB,EAAWH,EAAaC,EAAI0B,GAAS,mBAAmB,EAE9D,OAAOV,EAAed,EAAUwB,GAAS,KAAK,CAChD,CAGO,SAASC,EAAQ3B,EAAoB,CAE1C,OAAIA,EAAK,IACAyB,EAAiBzB,CAAE,EAIxBA,EAAK,IACA,IAAIA,EAAK,KAAM,QAAQ,CAAC,CAAC,IAI3BA,EAAK,IACd","names":["duration_exports","__export","DURATION_STYLE_SUFFIX_MAP","DURATION_TYPE_SEQUENCE","durationOrMsToMs","durationToMs","elapsed","formatDuration","msToDuration","readableDuration","__toCommonJS","DURATION_TYPE_SEQUENCE","DURATION_STYLE_SUFFIX_MAP","getDurationStyleForPlural","style","getValueAndUnitSeparator","getDurationTypeSeparator","msToDuration","ms","durationTypeForZero","duration","i","seconds","millis","minutes","hours","days","durationToMs","daysMs","hoursMs","minsMs","secsMs","msMs","durationOrMsToMs","formatDuration","stylePlural","space","a","unit","value","suffixMap","suffix","separator","readableDuration","options","elapsed"]}
package/duration.min.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import{a,b,c,d,e,f,g,h,i}from"./chunk-Q4WCMSOW.min.mjs";import"./chunk-MN5EYNZN.min.mjs";import"./chunk-UOU6IP3S.min.mjs";i();export{b as DURATION_STYLE_SUFFIX_MAP,a as DURATION_TYPE_SEQUENCE,e as durationOrMsToMs,d as durationToMs,h as elapsed,f as formatDuration,c as msToDuration,g as readableDuration};
1
+ var d=["days","hours","minutes","seconds","milliseconds"],h={days:{short:"day",shorts:"days",long:"day",longs:"days",narrow:"d"},hours:{short:"hr",shorts:"hrs",long:"hour",longs:"hours",narrow:"h"},minutes:{short:"min",shorts:"mins",long:"minute",longs:"minutes",narrow:"m"},seconds:{short:"sec",shorts:"secs",long:"second",longs:"seconds",narrow:"s"},milliseconds:{short:"ms",shorts:"ms",long:"millisecond",longs:"milliseconds",narrow:"ms"}};function y(o){return o=="short"?"shorts":o==="long"?"longs":o}function g(o){return o==="narrow"?"":" "}function S(o){return o==="narrow"?" ":", "}function M(o,n){if(o===0)return n=n??"milliseconds",{[n]:0};let r={};for(let i=0;i<1;i++){let t=Math.floor(o/1e3),u=o-t*1e3;if(u>0&&(r.milliseconds=u),t===0)break;let e=Math.floor(t/60);if(t-=e*60,t>0&&(r.seconds=t),e===0)break;let s=Math.floor(e/60);if(e-=s*60,e>0&&(r.minutes=e),s===0)break;let a=Math.floor(s/24);s-=a*24,s>0&&(r.hours=s),a>0&&(r.days=a)}return r}function x(o){let n=(o.days??0)*864e5,r=(o.hours??0)*36e5,i=(o.minutes??0)*6e4,t=(o.seconds??0)*1e3,u=o.milliseconds??0;return n+r+i+t+u}function R(o){return typeof o=="number"?o:x(o)}function _(o,n){n=n??"short";let r=y(n),i=g(n),t=[];for(let e of d){let s=o[e];if(s===void 0)continue;let a=h[e],c=s===1?a[n]:a[r];t.push(s+i+c)}let u=S(n);return t.join(u)}function T(o,n){let r=M(o,n?.durationTypeForZero);return _(r,n?.style)}function P(o){return o>6e4?T(o):o>100?`${(o/1e3).toFixed(3)}s`:o+"ms"}export{h as DURATION_STYLE_SUFFIX_MAP,d as DURATION_TYPE_SEQUENCE,R as durationOrMsToMs,x as durationToMs,P as elapsed,_ as formatDuration,M as msToDuration,T as readableDuration};
2
2
  //# sourceMappingURL=duration.min.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
1
+ {"version":3,"sources":["../src/duration.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 HOURS_PER_DAY,\n MINUTES_PER_HOUR,\n MS_PER_DAY,\n MS_PER_HOUR,\n MS_PER_MINUTE,\n MS_PER_SECOND,\n SECONDS_PER_MINUTE,\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 * Convenience function to return a duration given an ms or Duration.\n */\nexport function durationOrMsToMs(duration: number | Duration): number {\n return typeof duration === \"number\" ? duration : durationToMs(duration);\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/** A shortened duration string useful for logging timings. */\nexport function elapsed(ms: number): string {\n // Use long format for 1 minute or over.\n if (ms > MS_PER_MINUTE) {\n return readableDuration(ms);\n }\n\n // Use seconds format for over 100ms.\n if (ms > 100) {\n return `${(ms / 1000).toFixed(3)}s`;\n }\n\n // Use milliseconds format.\n return ms + \"ms\";\n}\n"],"mappings":"AAmCO,IAAMA,EAAyC,CACpD,OACA,QACA,UACA,UACA,cACF,EAsBaC,EAGT,CACF,KAAM,CACJ,MAAO,MACP,OAAQ,OACR,KAAM,MACN,MAAO,OACP,OAAQ,GACV,EACA,MAAO,CACL,MAAO,KACP,OAAQ,MACR,KAAM,OACN,MAAO,QACP,OAAQ,GACV,EACA,QAAS,CACP,MAAO,MACP,OAAQ,OACR,KAAM,SACN,MAAO,UACP,OAAQ,GACV,EACA,QAAS,CACP,MAAO,MACP,OAAQ,OACR,KAAM,SACN,MAAO,UACP,OAAQ,GACV,EACA,aAAc,CACZ,MAAO,KACP,OAAQ,KACR,KAAM,cACN,MAAO,eACP,OAAQ,IACV,CACF,EAEA,SAASC,EAA0BC,EAA0C,CAC3E,OAAOA,GAAS,QAAU,SAAWA,IAAU,OAAS,QAAUA,CACpE,CAEA,SAASC,EAAyBD,EAA8B,CAC9D,OAAOA,IAAU,SAAW,GAAK,GACnC,CAEA,SAASE,EAAyBF,EAA8B,CAC9D,OAAOA,IAAU,SAAW,IAAM,IACpC,CASO,SAASG,EACdC,EACAC,EACU,CACV,GAAID,IAAO,EACT,OAAAC,EAAsBA,GAAuB,eACtC,CAAE,CAACA,CAAmB,EAAG,CAAE,EAGpC,IAAMC,EAAqB,CAAC,EAE5B,QAA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yB,EAAUL,CAAW,EACrEE,EAAE,KAAKE,EAAQH,EAAQK,CAAM,CAC/B,CAEA,IAAMC,EAAYzB,EAAyBF,CAAK,EAChD,OAAOsB,EAAE,KAAKK,CAAS,CACzB,CAQO,SAASC,EACdxB,EACAyB,EACQ,CACR,IAAMvB,EAAWH,EAAaC,EAAIyB,GAAS,mBAAmB,EAE9D,OAAOV,EAAeb,EAAUuB,GAAS,KAAK,CAChD,CAGO,SAASC,EAAQ1B,EAAoB,CAE1C,OAAIA,EAAK,IACAwB,EAAiBxB,CAAE,EAIxBA,EAAK,IACA,IAAIA,EAAK,KAAM,QAAQ,CAAC,CAAC,IAI3BA,EAAK,IACd","names":["DURATION_TYPE_SEQUENCE","DURATION_STYLE_SUFFIX_MAP","getDurationStyleForPlural","style","getValueAndUnitSeparator","getDurationTypeSeparator","msToDuration","ms","durationTypeForZero","duration","seconds","millis","minutes","hours","days","durationToMs","daysMs","hoursMs","minsMs","secsMs","msMs","durationOrMsToMs","formatDuration","stylePlural","space","a","unit","value","suffixMap","suffix","separator","readableDuration","options","elapsed"]}