@choksheak/ts-utils 0.2.9 → 0.3.1

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 (315) hide show
  1. package/README.md +2 -0
  2. package/{arrayBuffer.js → arrayBuffer.cjs} +0 -1
  3. package/arrayBuffer.d.mts +10 -0
  4. package/arrayBuffer.d.ts +4 -2
  5. package/arrayBuffer.min.cjs +2 -0
  6. package/arrayBuffer.min.cjs.map +1 -0
  7. package/arrayBuffer.min.mjs +2 -0
  8. package/{arrayBuffer.js.map → arrayBuffer.min.mjs.map} +1 -1
  9. package/arrayBuffer.mjs +14 -0
  10. package/{assert.js → assert.cjs} +0 -1
  11. package/assert.d.mts +7 -0
  12. package/assert.d.ts +3 -1
  13. package/assert.min.cjs +2 -0
  14. package/assert.min.cjs.map +1 -0
  15. package/assert.min.mjs +2 -0
  16. package/{assert.js.map → assert.min.mjs.map} +1 -1
  17. package/assert.mjs +11 -0
  18. package/{average.js → average.cjs} +0 -1
  19. package/average.d.mts +7 -0
  20. package/average.d.ts +3 -1
  21. package/average.min.cjs +2 -0
  22. package/{average.js.map → average.min.cjs.map} +1 -1
  23. package/average.min.mjs +2 -0
  24. package/average.min.mjs.map +1 -0
  25. package/average.mjs +20 -0
  26. package/{base64Url.js → base64Url.cjs} +0 -1
  27. package/base64Url.d.mts +10 -0
  28. package/base64Url.d.ts +4 -2
  29. package/base64Url.min.cjs +2 -0
  30. package/base64Url.min.cjs.map +1 -0
  31. package/base64Url.min.mjs +2 -0
  32. package/{base64Url.js.map → base64Url.min.mjs.map} +1 -1
  33. package/base64Url.mjs +14 -0
  34. package/{dateTimeStr.js → dateTimeStr.cjs} +0 -1
  35. package/dateTimeStr.d.mts +85 -0
  36. package/dateTimeStr.d.ts +15 -13
  37. package/dateTimeStr.min.cjs +2 -0
  38. package/dateTimeStr.min.cjs.map +1 -0
  39. package/dateTimeStr.min.mjs +2 -0
  40. package/{dateTimeStr.js.map → dateTimeStr.min.mjs.map} +1 -1
  41. package/dateTimeStr.mjs +83 -0
  42. package/{duration.js → duration.cjs} +9 -11
  43. package/duration.d.mts +78 -0
  44. package/duration.d.ts +15 -13
  45. package/duration.min.cjs +2 -0
  46. package/duration.min.cjs.map +1 -0
  47. package/duration.min.mjs +2 -0
  48. package/duration.min.mjs.map +1 -0
  49. package/duration.mjs +154 -0
  50. package/{isEmpty.js → isEmpty.cjs} +0 -1
  51. package/isEmpty.d.mts +6 -0
  52. package/isEmpty.d.ts +3 -1
  53. package/isEmpty.min.cjs +2 -0
  54. package/isEmpty.min.cjs.map +1 -0
  55. package/isEmpty.min.mjs +2 -0
  56. package/{isEmpty.js.map → isEmpty.min.mjs.map} +1 -1
  57. package/isEmpty.mjs +22 -0
  58. package/iterators.cjs +34 -0
  59. package/iterators.d.mts +4 -0
  60. package/iterators.d.ts +4 -0
  61. package/iterators.min.cjs +2 -0
  62. package/iterators.min.cjs.map +1 -0
  63. package/iterators.min.mjs +2 -0
  64. package/iterators.min.mjs.map +1 -0
  65. package/iterators.mjs +9 -0
  66. package/kvStore.cjs +365 -0
  67. package/kvStore.d.mts +139 -0
  68. package/kvStore.d.ts +95 -50
  69. package/kvStore.min.cjs +2 -0
  70. package/kvStore.min.cjs.map +1 -0
  71. package/kvStore.min.mjs +2 -0
  72. package/kvStore.min.mjs.map +1 -0
  73. package/kvStore.mjs +333 -0
  74. package/localStore.cjs +267 -0
  75. package/localStore.d.mts +119 -0
  76. package/localStore.d.ts +119 -0
  77. package/localStore.min.cjs +2 -0
  78. package/localStore.min.cjs.map +1 -0
  79. package/localStore.min.mjs +2 -0
  80. package/localStore.min.mjs.map +1 -0
  81. package/localStore.mjs +235 -0
  82. package/{logging.js → logging.cjs} +0 -1
  83. package/logging.d.mts +4 -0
  84. package/logging.d.ts +4 -2
  85. package/logging.min.cjs +2 -0
  86. package/logging.min.cjs.map +1 -0
  87. package/logging.min.mjs +2 -0
  88. package/{logging.js.map → logging.min.mjs.map} +1 -1
  89. package/logging.mjs +21 -0
  90. package/{nonEmpty.js → nonEmpty.cjs} +0 -1
  91. package/nonEmpty.d.mts +10 -0
  92. package/nonEmpty.d.ts +3 -1
  93. package/nonEmpty.min.cjs +2 -0
  94. package/{nonEmpty.js.map → nonEmpty.min.cjs.map} +1 -1
  95. package/nonEmpty.min.mjs +2 -0
  96. package/nonEmpty.min.mjs.map +1 -0
  97. package/nonEmpty.mjs +30 -0
  98. package/{nonNil.js → nonNil.cjs} +0 -1
  99. package/{src/nonNil.ts → nonNil.d.mts} +3 -6
  100. package/nonNil.d.ts +3 -1
  101. package/nonNil.min.cjs +2 -0
  102. package/nonNil.min.cjs.map +1 -0
  103. package/nonNil.min.mjs +2 -0
  104. package/{nonNil.js.map → nonNil.min.mjs.map} +1 -1
  105. package/nonNil.mjs +10 -0
  106. package/package.json +447 -24
  107. package/{round.js → round.cjs} +0 -1
  108. package/{src/round.ts → round.d.mts} +4 -8
  109. package/round.d.ts +4 -2
  110. package/round.min.cjs +2 -0
  111. package/round.min.cjs.map +1 -0
  112. package/round.min.mjs +2 -0
  113. package/{round.js.map → round.min.mjs.map} +1 -1
  114. package/round.mjs +12 -0
  115. package/{safeBtoa.js → safeBtoa.cjs} +0 -1
  116. package/safeBtoa.d.mts +6 -0
  117. package/safeBtoa.d.ts +3 -1
  118. package/safeBtoa.min.cjs +2 -0
  119. package/safeBtoa.min.cjs.map +1 -0
  120. package/safeBtoa.min.mjs +2 -0
  121. package/{safeBtoa.js.map → safeBtoa.min.mjs.map} +1 -1
  122. package/safeBtoa.mjs +9 -0
  123. package/{safeParseFloat.js → safeParseFloat.cjs} +0 -1
  124. package/safeParseFloat.d.mts +6 -0
  125. package/safeParseFloat.d.ts +3 -1
  126. package/safeParseFloat.min.cjs +2 -0
  127. package/safeParseFloat.min.cjs.map +1 -0
  128. package/safeParseFloat.min.mjs +2 -0
  129. package/{safeParseFloat.js.map → safeParseFloat.min.mjs.map} +1 -1
  130. package/safeParseFloat.mjs +8 -0
  131. package/{safeParseInt.js → safeParseInt.cjs} +0 -1
  132. package/safeParseInt.d.mts +6 -0
  133. package/safeParseInt.d.ts +3 -1
  134. package/safeParseInt.min.cjs +2 -0
  135. package/safeParseInt.min.cjs.map +1 -0
  136. package/safeParseInt.min.mjs +2 -0
  137. package/{safeParseInt.js.map → safeParseInt.min.mjs.map} +1 -1
  138. package/safeParseInt.mjs +8 -0
  139. package/sha256.cjs +35 -0
  140. package/sha256.d.mts +6 -0
  141. package/sha256.d.ts +3 -1
  142. package/sha256.min.cjs +2 -0
  143. package/sha256.min.cjs.map +1 -0
  144. package/sha256.min.mjs +2 -0
  145. package/{sha256.js.map → sha256.min.mjs.map} +1 -1
  146. package/sha256.mjs +10 -0
  147. package/{sleep.js → sleep.cjs} +0 -1
  148. package/{src/sleep.ts → sleep.d.mts} +3 -3
  149. package/sleep.d.ts +3 -1
  150. package/sleep.min.cjs +2 -0
  151. package/sleep.min.cjs.map +1 -0
  152. package/sleep.min.mjs +2 -0
  153. package/{sleep.js.map → sleep.min.mjs.map} +1 -1
  154. package/sleep.mjs +7 -0
  155. package/storageAdapter.cjs +18 -0
  156. package/storageAdapter.d.mts +33 -0
  157. package/storageAdapter.d.ts +33 -0
  158. package/storageAdapter.min.cjs +2 -0
  159. package/storageAdapter.min.cjs.map +1 -0
  160. package/storageAdapter.min.mjs +1 -0
  161. package/storageAdapter.min.mjs.map +1 -0
  162. package/storageAdapter.mjs +0 -0
  163. package/{sum.js → sum.cjs} +0 -1
  164. package/sum.d.mts +7 -0
  165. package/sum.d.ts +3 -1
  166. package/sum.min.cjs +2 -0
  167. package/sum.min.cjs.map +1 -0
  168. package/sum.min.mjs +2 -0
  169. package/{sum.js.map → sum.min.mjs.map} +1 -1
  170. package/sum.mjs +13 -0
  171. package/{timeConstants.js → timeConstants.cjs} +0 -1
  172. package/timeConstants.d.mts +20 -0
  173. package/timeConstants.d.ts +16 -14
  174. package/timeConstants.min.cjs +2 -0
  175. package/timeConstants.min.cjs.map +1 -0
  176. package/timeConstants.min.mjs +2 -0
  177. package/{timeConstants.js.map → timeConstants.min.mjs.map} +1 -1
  178. package/timeConstants.mjs +31 -0
  179. package/{timer.js → timer.cjs} +6 -6
  180. package/timer.d.mts +13 -0
  181. package/timer.d.ts +4 -2
  182. package/timer.min.cjs +2 -0
  183. package/timer.min.cjs.map +1 -0
  184. package/timer.min.mjs +2 -0
  185. package/timer.min.mjs.map +1 -0
  186. package/timer.mjs +161 -0
  187. package/docs/.nojekyll +0 -1
  188. package/docs/assets/hierarchy.js +0 -1
  189. package/docs/assets/highlight.css +0 -22
  190. package/docs/assets/icons.js +0 -18
  191. package/docs/assets/icons.svg +0 -1
  192. package/docs/assets/main.js +0 -60
  193. package/docs/assets/navigation.js +0 -1
  194. package/docs/assets/search.js +0 -1
  195. package/docs/assets/style.css +0 -1640
  196. package/docs/classes/kvStore.KVStore.html +0 -26
  197. package/docs/classes/kvStore.KVStoreField.html +0 -7
  198. package/docs/classes/timer.Timer.html +0 -8
  199. package/docs/functions/arrayBuffer.arrayBufferToBase64.html +0 -2
  200. package/docs/functions/arrayBuffer.arrayBufferToHex.html +0 -2
  201. package/docs/functions/assert.assert.html +0 -3
  202. package/docs/functions/average.average.html +0 -3
  203. package/docs/functions/base64Url.base64ToBase64URL.html +0 -2
  204. package/docs/functions/base64Url.base64UrlToBase64.html +0 -2
  205. package/docs/functions/dateTimeStr.getDisplayDateTime.html +0 -3
  206. package/docs/functions/dateTimeStr.getLongMonthNameOneIndexed.html +0 -4
  207. package/docs/functions/dateTimeStr.getLongMonthNameZeroIndexed.html +0 -4
  208. package/docs/functions/dateTimeStr.getShortMonthNameOneIndexed.html +0 -4
  209. package/docs/functions/dateTimeStr.getShortMonthNameZeroIndexed.html +0 -4
  210. package/docs/functions/dateTimeStr.hhMm.html +0 -4
  211. package/docs/functions/dateTimeStr.hhMmSs.html +0 -4
  212. package/docs/functions/dateTimeStr.hhMmSsMs.html +0 -5
  213. package/docs/functions/dateTimeStr.toDate.html +0 -4
  214. package/docs/functions/dateTimeStr.tzShort.html +0 -4
  215. package/docs/functions/dateTimeStr.yyyyMm.html +0 -4
  216. package/docs/functions/dateTimeStr.yyyyMmDd.html +0 -4
  217. package/docs/functions/duration.durationOrMsToMs.html +0 -2
  218. package/docs/functions/duration.durationToMs.html +0 -2
  219. package/docs/functions/duration.elapsed.html +0 -2
  220. package/docs/functions/duration.formatDuration.html +0 -4
  221. package/docs/functions/duration.msToDuration.html +0 -5
  222. package/docs/functions/duration.readableDuration.html +0 -4
  223. package/docs/functions/isEmpty.isEmpty.html +0 -2
  224. package/docs/functions/kvStore.kvStoreItem.html +0 -2
  225. package/docs/functions/localStorageCache.storeItem.html +0 -15
  226. package/docs/functions/logging.capLength.html +0 -1
  227. package/docs/functions/logging.stringify.html +0 -1
  228. package/docs/functions/nonEmpty.nonEmpty.html +0 -5
  229. package/docs/functions/nonNil.nonNil.html +0 -5
  230. package/docs/functions/round.round.html +0 -5
  231. package/docs/functions/round.roundS.html +0 -3
  232. package/docs/functions/safeBtoa.safeBtoa.html +0 -2
  233. package/docs/functions/safeParseFloat.safeParseFloat.html +0 -2
  234. package/docs/functions/safeParseInt.safeParseInt.html +0 -2
  235. package/docs/functions/sha256.sha256.html +0 -2
  236. package/docs/functions/sleep.sleep.html +0 -3
  237. package/docs/functions/sum.sum.html +0 -3
  238. package/docs/functions/timer.timer.html +0 -2
  239. package/docs/hierarchy.html +0 -1
  240. package/docs/index.html +0 -4
  241. package/docs/modules/arrayBuffer.html +0 -1
  242. package/docs/modules/assert.html +0 -1
  243. package/docs/modules/average.html +0 -1
  244. package/docs/modules/base64Url.html +0 -1
  245. package/docs/modules/dateTimeStr.html +0 -1
  246. package/docs/modules/duration.html +0 -1
  247. package/docs/modules/isEmpty.html +0 -1
  248. package/docs/modules/kvStore.html +0 -1
  249. package/docs/modules/localStorageCache.html +0 -1
  250. package/docs/modules/logging.html +0 -1
  251. package/docs/modules/nonEmpty.html +0 -1
  252. package/docs/modules/nonNil.html +0 -1
  253. package/docs/modules/round.html +0 -1
  254. package/docs/modules/safeBtoa.html +0 -1
  255. package/docs/modules/safeParseFloat.html +0 -1
  256. package/docs/modules/safeParseInt.html +0 -1
  257. package/docs/modules/sha256.html +0 -1
  258. package/docs/modules/sleep.html +0 -1
  259. package/docs/modules/sum.html +0 -1
  260. package/docs/modules/timeConstants.html +0 -1
  261. package/docs/modules/timer.html +0 -1
  262. package/docs/modules.html +0 -1
  263. package/docs/types/dateTimeStr.AnyDateTime.html +0 -1
  264. package/docs/types/duration.Duration.html +0 -6
  265. package/docs/types/duration.DurationStyle.html +0 -6
  266. package/docs/types/duration.DurationSuffixMap.html +0 -6
  267. package/docs/types/duration.DurationSuffixType.html +0 -1
  268. package/docs/types/duration.DurationType.html +0 -2
  269. package/docs/types/localStorageCache.StoredItem.html +0 -4
  270. package/docs/variables/duration.DURATION_STYLE_SUFFIX_MAP.html +0 -1
  271. package/docs/variables/duration.DURATION_TYPE_SEQUENCE.html +0 -2
  272. package/docs/variables/kvStore.DEFAULT_EXPIRY_DELTA_MS.html +0 -2
  273. package/docs/variables/kvStore.GC_INTERVAL_MS.html +0 -2
  274. package/docs/variables/kvStore.MILLIS_PER_DAY.html +0 -2
  275. package/docs/variables/kvStore.kvStore.html +0 -3
  276. package/docs/variables/timeConstants.HOURS_PER_DAY.html +0 -1
  277. package/docs/variables/timeConstants.HOURS_PER_WEEK.html +0 -1
  278. package/docs/variables/timeConstants.MINUTES_PER_DAY.html +0 -1
  279. package/docs/variables/timeConstants.MINUTES_PER_HOUR.html +0 -1
  280. package/docs/variables/timeConstants.MINUTES_PER_WEEK.html +0 -1
  281. package/docs/variables/timeConstants.MS_PER_DAY.html +0 -1
  282. package/docs/variables/timeConstants.MS_PER_HOUR.html +0 -1
  283. package/docs/variables/timeConstants.MS_PER_MINUTE.html +0 -1
  284. package/docs/variables/timeConstants.MS_PER_SECOND.html +0 -3
  285. package/docs/variables/timeConstants.MS_PER_WEEK.html +0 -1
  286. package/docs/variables/timeConstants.SECONDS_PER_DAY.html +0 -1
  287. package/docs/variables/timeConstants.SECONDS_PER_HOUR.html +0 -1
  288. package/docs/variables/timeConstants.SECONDS_PER_MINUTE.html +0 -1
  289. package/docs/variables/timeConstants.SECONDS_PER_WEEK.html +0 -1
  290. package/duration.js.map +0 -1
  291. package/kvStore.js +0 -383
  292. package/kvStore.js.map +0 -1
  293. package/localStorageCache.d.ts +0 -55
  294. package/localStorageCache.js +0 -121
  295. package/localStorageCache.js.map +0 -1
  296. package/sha256.js +0 -58
  297. package/src/arrayBuffer.ts +0 -29
  298. package/src/assert.ts +0 -14
  299. package/src/average.ts +0 -13
  300. package/src/base64Url.ts +0 -20
  301. package/src/dateTimeStr.ts +0 -184
  302. package/src/duration.ts +0 -260
  303. package/src/isEmpty.ts +0 -32
  304. package/src/kvStore.ts +0 -444
  305. package/src/localStorageCache.ts +0 -121
  306. package/src/logging.ts +0 -26
  307. package/src/nonEmpty.ts +0 -18
  308. package/src/safeBtoa.ts +0 -15
  309. package/src/safeParseFloat.ts +0 -10
  310. package/src/safeParseInt.ts +0 -10
  311. package/src/sha256.ts +0 -13
  312. package/src/sum.ts +0 -17
  313. package/src/timeConstants.ts +0 -22
  314. package/src/timer.ts +0 -33
  315. package/timer.js.map +0 -1
package/sha256.js DELETED
@@ -1,58 +0,0 @@
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
- var __async = (__this, __arguments, generator) => {
20
- return new Promise((resolve, reject) => {
21
- var fulfilled = (value) => {
22
- try {
23
- step(generator.next(value));
24
- } catch (e) {
25
- reject(e);
26
- }
27
- };
28
- var rejected = (value) => {
29
- try {
30
- step(generator.throw(value));
31
- } catch (e) {
32
- reject(e);
33
- }
34
- };
35
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
36
- step((generator = generator.apply(__this, __arguments)).next());
37
- });
38
- };
39
-
40
- // src/sha256.ts
41
- var sha256_exports = {};
42
- __export(sha256_exports, {
43
- sha256: () => sha256
44
- });
45
- module.exports = __toCommonJS(sha256_exports);
46
- function sha256(input) {
47
- return __async(this, null, function* () {
48
- const encoder = new TextEncoder();
49
- const uint8Array = encoder.encode(input);
50
- const arrayBuffer = yield crypto.subtle.digest("SHA-256", uint8Array);
51
- return arrayBuffer;
52
- });
53
- }
54
- // Annotate the CommonJS export names for ESM import in node:
55
- 0 && (module.exports = {
56
- sha256
57
- });
58
- //# sourceMappingURL=sha256.js.map
@@ -1,29 +0,0 @@
1
- /**
2
- * Encode an input ArrayBuffer into a hex string.
3
- */
4
- export function arrayBufferToHex(buffer: ArrayBuffer): string {
5
- // Create a Uint8Array view of the ArrayBuffer
6
- const byteArray = new Uint8Array(buffer);
7
-
8
- // Convert each byte to a two-character hexadecimal string
9
- return Array.from(byteArray)
10
- .map((byte) => byte.toString(16).padStart(2, "0"))
11
- .join("");
12
- }
13
-
14
- /**
15
- * Encode an input ArrayBuffer into a base64 string.
16
- */
17
- export function arrayBufferToBase64(buffer: ArrayBuffer): string {
18
- // Convert the ArrayBuffer to a Uint8Array
19
- const byteArray = new Uint8Array(buffer);
20
-
21
- // Create a binary string from the byte array
22
- const binaryString = Array.from(byteArray)
23
- .map((byte) => String.fromCodePoint(byte))
24
- .join("");
25
-
26
- // Encode the binary string to base64. No need to use safeBtoa because we
27
- // already simplified the binary input above.
28
- return btoa(binaryString);
29
- }
package/src/assert.ts DELETED
@@ -1,14 +0,0 @@
1
- /**
2
- * Type asserts that `t` is truthy. Throws an error with `errorMessage` if
3
- * `t` is falsy.
4
- */
5
- export function assert<T>(
6
- t: T | null | undefined | "" | 0 | -0 | 0n | false | typeof NaN,
7
- errorMessage?: string,
8
- ): asserts t is T {
9
- if (!t) {
10
- throw new Error(
11
- errorMessage || `Assertion failed: ${JSON.stringify(t)} is falsy`,
12
- );
13
- }
14
- }
package/src/average.ts DELETED
@@ -1,13 +0,0 @@
1
- import { sum } from "./sum";
2
-
3
- /**
4
- * Average all the numbers together in the given array. Treats null, undefined
5
- * and NaN as zero.
6
- */
7
- export function average(numbers: (number | null | undefined)[]): number {
8
- if (numbers.length === 0) return 0;
9
-
10
- const total = sum(numbers);
11
-
12
- return total / numbers.length;
13
- }
package/src/base64Url.ts DELETED
@@ -1,20 +0,0 @@
1
- /**
2
- * Convert a base64 string to a base64url string.
3
- */
4
- export function base64ToBase64URL(base64: string): string {
5
- return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""); // Remove padding
6
- }
7
-
8
- /**
9
- * Convert a base64url string to a base64 string.
10
- */
11
- export function base64UrlToBase64(base64Url: string): string {
12
- if (!base64Url) return "";
13
-
14
- // Replace URL-safe characters
15
- const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
16
-
17
- // Calculate and add padding
18
- const padLength = (4 - (base64.length % 4)) % 4;
19
- return base64 + "=".repeat(padLength);
20
- }
@@ -1,184 +0,0 @@
1
- export type AnyDateTime = number | Date | string;
2
-
3
- function isAllDigits(str: string): boolean {
4
- return /^\d+$/.test(str);
5
- }
6
-
7
- /**
8
- * Convert a number (epoch seconds or milliseconds), string (parseable
9
- * date/time or epoch seconds or milliseconds), or Date object (no conversion)
10
- * into a Date object.
11
- */
12
- export function toDate(ts: AnyDateTime): Date {
13
- if (typeof ts === "number") {
14
- // Handle timestamp in seconds (less than 12 digits long).
15
- if (ts < 1_000_000_000_00) {
16
- return new Date(ts * 1000);
17
- }
18
-
19
- // Handle timestamp in milliseconds.
20
- return new Date(ts);
21
- }
22
-
23
- if (typeof ts === "string") {
24
- if (isAllDigits(ts)) {
25
- return toDate(Number(ts));
26
- }
27
-
28
- return new Date(ts);
29
- }
30
-
31
- return ts;
32
- }
33
-
34
- /**
35
- * Returns a date in yyyy-MM format. E.g. '2000-01'.
36
- *
37
- * @param dt Specify a date object or default to the current date.
38
- * @param separator Defaults to '-'.
39
- */
40
- export function yyyyMm(dt = new Date(), separator = "-"): string {
41
- const yr = dt.getFullYear();
42
- const mth = dt.getMonth() + 1;
43
-
44
- return yr + separator + (mth < 10 ? "0" + mth : mth);
45
- }
46
-
47
- /**
48
- * Returns a date in yyyy-MM-dd format. E.g. '2000-01-02'.
49
- *
50
- * @param dt Specify a date object or default to the current date.
51
- * @param separator Defaults to '-'.
52
- */
53
- export function yyyyMmDd(dt = new Date(), separator = "-"): string {
54
- const day = dt.getDate();
55
-
56
- return yyyyMm(dt, separator) + separator + (day < 10 ? "0" + day : day);
57
- }
58
-
59
- /**
60
- * Returns a date in hh:mm format. E.g. '01:02'.
61
- *
62
- * @param dt Specify a date object or default to the current date/time.
63
- * @param separator Defaults to ':'.
64
- */
65
- export function hhMm(dt = new Date(), separator = ":"): string {
66
- const hr = dt.getHours();
67
- const min = dt.getMinutes();
68
-
69
- return (hr < 10 ? "0" + hr : hr) + separator + (min < 10 ? "0" + min : min);
70
- }
71
-
72
- /**
73
- * Returns a date in hh:mm:ss format. E.g. '01:02:03'.
74
- *
75
- * @param dt Specify a date object or default to the current date/time.
76
- * @param separator Defaults to ':'.
77
- */
78
- export function hhMmSs(dt = new Date(), separator = ":"): string {
79
- const sec = dt.getSeconds();
80
-
81
- return hhMm(dt, separator) + separator + (sec < 10 ? "0" + sec : sec);
82
- }
83
-
84
- /**
85
- * Returns a date in hh:mm:ss.SSS format. E.g. '01:02:03.004'.
86
- *
87
- * @param dt Specify a date object or default to the current date/time.
88
- * @param timeSeparator Separator for hh/mm/ss. Defaults to ':'.
89
- * @param msSeparator Separator before SSS. Defaults to '.'.
90
- */
91
- export function hhMmSsMs(
92
- dt = new Date(),
93
- timeSeparator = ":",
94
- msSeparator = ".",
95
- ): string {
96
- const ms = dt.getMilliseconds();
97
-
98
- return (
99
- hhMmSs(dt, timeSeparator) +
100
- msSeparator +
101
- (ms < 10 ? "00" + ms : ms < 100 ? "0" + ms : ms)
102
- );
103
- }
104
-
105
- /**
106
- * Returns the timezone string for the given date. E.g. '+8', '-3.5'.
107
- * Returns 'Z' for UTC.
108
- *
109
- * @param dt Specify a date object or default to the current date/time.
110
- */
111
- export function tzShort(dt = new Date()): string {
112
- if (dt.getTimezoneOffset() === 0) {
113
- return "Z";
114
- }
115
-
116
- const tzHours = dt.getTimezoneOffset() / 60;
117
- return tzHours >= 0 ? "+" + tzHours : String(tzHours);
118
- }
119
-
120
- /**
121
- * Returns the long month name, zero-indexed. E.g. 0 for 'January'.
122
- *
123
- * @param month Zero-indexed month.
124
- * @param locales Specify the locale, e.g. 'en-US', new Intl.Locale("en-US").
125
- */
126
- export function getLongMonthNameZeroIndexed(
127
- month: number,
128
- locales: Intl.LocalesArgument = "default",
129
- ): string {
130
- return new Date(2024, month, 15).toLocaleString(locales, {
131
- month: "long",
132
- });
133
- }
134
-
135
- /**
136
- * Returns the long month name, one-indexed. E.g. 1 for 'January'.
137
- *
138
- * @param month One-indexed month.
139
- * @param locales Specify the locale, e.g. 'en-US', new Intl.Locale("en-US").
140
- */
141
- export function getLongMonthNameOneIndexed(
142
- month: number,
143
- locales: Intl.LocalesArgument = "default",
144
- ): string {
145
- return getLongMonthNameZeroIndexed(month - 1, locales);
146
- }
147
-
148
- /**
149
- * Returns the short month name, zero-indexed. E.g. 0 for 'Jan'.
150
- *
151
- * @param month Zero-indexed month.
152
- * @param locales Specify the locale, e.g. 'en-US', new Intl.Locale("en-US").
153
- */
154
- export function getShortMonthNameZeroIndexed(
155
- month: number,
156
- locales: Intl.LocalesArgument = "default",
157
- ): string {
158
- return new Date(2000, month, 15).toLocaleString(locales, {
159
- month: "short",
160
- });
161
- }
162
-
163
- /**
164
- * Returns the short month name, one-indexed. E.g. 1 for 'Jan'.
165
- *
166
- * @param month One-indexed month.
167
- * @param locales Specify the locale, e.g. 'en-US', new Intl.Locale("en-US").
168
- */
169
- export function getShortMonthNameOneIndexed(
170
- month: number,
171
- locales: Intl.LocalesArgument = "default",
172
- ): string {
173
- return getShortMonthNameZeroIndexed(month - 1, locales);
174
- }
175
-
176
- /**
177
- * Returns a human-readable string date/time like '2025-01-01 22:31:16Z'.
178
- * Excludes the milliseconds assuming it is not necessary for display.
179
- */
180
- export function getDisplayDateTime(ts: AnyDateTime) {
181
- const iso = toDate(ts).toISOString();
182
- const noMs = iso.slice(0, 19) + "Z";
183
- return noMs.replace("T", " ");
184
- }
package/src/duration.ts DELETED
@@ -1,260 +0,0 @@
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
-
10
- import {
11
- MS_PER_SECOND,
12
- SECONDS_PER_MINUTE,
13
- MINUTES_PER_HOUR,
14
- HOURS_PER_DAY,
15
- MS_PER_DAY,
16
- MS_PER_MINUTE,
17
- MS_PER_HOUR,
18
- } from "./timeConstants";
19
-
20
- export type Duration = {
21
- days?: number;
22
- hours?: number;
23
- minutes?: number;
24
- seconds?: number;
25
- milliseconds?: number;
26
- };
27
-
28
- /**
29
- * One of: days, hours, minutes, seconds, milliseconds
30
- */
31
- export type DurationType = keyof Duration;
32
-
33
- /**
34
- * Order in which the duration type appears in the duration string.
35
- */
36
- export const DURATION_TYPE_SEQUENCE: DurationType[] = [
37
- "days",
38
- "hours",
39
- "minutes",
40
- "seconds",
41
- "milliseconds",
42
- ];
43
-
44
- /**
45
- * Follows the same format as Intl.DurationFormat.prototype.format().
46
- *
47
- * Short: 1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns
48
- * Long: 1 year, 2 months, 3 weeks, 3 days, 4 hours, 5 minutes, 6 seconds,
49
- * 7 milliseconds, 8 microseconds, 9 nanoseconds
50
- * Narrow: 1y 2mo 3w 3d 4h 5m 6s 7ms 8μs 9ns
51
- */
52
- export type DurationStyle = "short" | "long" | "narrow";
53
-
54
- export type DurationSuffixMap = {
55
- short: string;
56
- shorts: string;
57
- long: string;
58
- longs: string;
59
- narrow: string;
60
- };
61
-
62
- export type DurationSuffixType = keyof DurationSuffixMap;
63
-
64
- export const DURATION_STYLE_SUFFIX_MAP: Record<
65
- DurationType,
66
- DurationSuffixMap
67
- > = {
68
- days: {
69
- short: "day",
70
- shorts: "days",
71
- long: "day",
72
- longs: "days",
73
- narrow: "d",
74
- },
75
- hours: {
76
- short: "hr",
77
- shorts: "hrs",
78
- long: "hour",
79
- longs: "hours",
80
- narrow: "h",
81
- },
82
- minutes: {
83
- short: "min",
84
- shorts: "mins",
85
- long: "minute",
86
- longs: "minutes",
87
- narrow: "m",
88
- },
89
- seconds: {
90
- short: "sec",
91
- shorts: "secs",
92
- long: "second",
93
- longs: "seconds",
94
- narrow: "s",
95
- },
96
- milliseconds: {
97
- short: "ms",
98
- shorts: "ms",
99
- long: "millisecond",
100
- longs: "milliseconds",
101
- narrow: "ms",
102
- },
103
- };
104
-
105
- function getDurationStyleForPlural(style: DurationStyle): DurationSuffixType {
106
- return style == "short" ? "shorts" : style === "long" ? "longs" : style;
107
- }
108
-
109
- function getValueAndUnitSeparator(style: DurationStyle): string {
110
- return style === "narrow" ? "" : " ";
111
- }
112
-
113
- function getDurationTypeSeparator(style: DurationStyle): string {
114
- return style === "narrow" ? " " : ", ";
115
- }
116
-
117
- /**
118
- * Convert a milliseconds duration into a Duration object. If the given ms is
119
- * zero, then return an object with a single field of zero with duration type
120
- * of durationTypeForZero.
121
- *
122
- * @param durationTypeForZero Defaults to 'milliseconds'
123
- */
124
- export function msToDuration(
125
- ms: number,
126
- durationTypeForZero?: DurationType,
127
- ): Duration {
128
- if (ms === 0) {
129
- durationTypeForZero = durationTypeForZero ?? "milliseconds";
130
- return { [durationTypeForZero]: 0 };
131
- }
132
-
133
- const duration: Duration = {};
134
-
135
- for (let i = 0; i < 1; i++) {
136
- let seconds = Math.floor(ms / MS_PER_SECOND);
137
- const millis = ms - seconds * MS_PER_SECOND;
138
-
139
- if (millis > 0) {
140
- duration["milliseconds"] = millis;
141
- }
142
-
143
- if (seconds === 0) {
144
- break;
145
- }
146
-
147
- let minutes = Math.floor(seconds / SECONDS_PER_MINUTE);
148
- seconds -= minutes * SECONDS_PER_MINUTE;
149
-
150
- if (seconds > 0) {
151
- duration["seconds"] = seconds;
152
- }
153
-
154
- if (minutes === 0) {
155
- break;
156
- }
157
-
158
- let hours = Math.floor(minutes / MINUTES_PER_HOUR);
159
- minutes -= hours * MINUTES_PER_HOUR;
160
-
161
- if (minutes > 0) {
162
- duration["minutes"] = minutes;
163
- }
164
-
165
- if (hours === 0) {
166
- break;
167
- }
168
-
169
- const days = Math.floor(hours / HOURS_PER_DAY);
170
- hours -= days * HOURS_PER_DAY;
171
-
172
- if (hours > 0) {
173
- duration["hours"] = hours;
174
- }
175
-
176
- if (days > 0) {
177
- duration["days"] = days;
178
- }
179
- }
180
-
181
- return duration;
182
- }
183
-
184
- /**
185
- * Returns the number of milliseconds for the given duration.
186
- */
187
- export function durationToMs(duration: Duration): number {
188
- const daysMs = (duration.days ?? 0) * MS_PER_DAY;
189
- const hoursMs = (duration.hours ?? 0) * MS_PER_HOUR;
190
- const minsMs = (duration.minutes ?? 0) * MS_PER_MINUTE;
191
- const secsMs = (duration.seconds ?? 0) * MS_PER_SECOND;
192
- const msMs = duration.milliseconds ?? 0;
193
-
194
- return daysMs + hoursMs + minsMs + secsMs + msMs;
195
- }
196
-
197
- /**
198
- * Convenience function to return a duration given an ms or Duration.
199
- */
200
- export function durationOrMsToMs(duration: number | Duration): number {
201
- return typeof duration === "number" ? duration : durationToMs(duration);
202
- }
203
-
204
- /**
205
- * Format a given Duration object into a string. If the object has no fields,
206
- * then returns an empty string.
207
- *
208
- * @param style Defaults to 'short'
209
- */
210
- export function formatDuration(duration: Duration, style?: DurationStyle) {
211
- style = style ?? "short";
212
- const stylePlural = getDurationStyleForPlural(style);
213
-
214
- const space = getValueAndUnitSeparator(style);
215
-
216
- const a: string[] = [];
217
-
218
- for (const unit of DURATION_TYPE_SEQUENCE) {
219
- const value = duration[unit];
220
- if (value === undefined) continue;
221
-
222
- const suffixMap = DURATION_STYLE_SUFFIX_MAP[unit];
223
- const suffix = value === 1 ? suffixMap[style] : suffixMap[stylePlural];
224
- a.push(value + space + suffix);
225
- }
226
-
227
- const separator = getDurationTypeSeparator(style);
228
- return a.join(separator);
229
- }
230
-
231
- /**
232
- * Convert a millisecond duration into a human-readable duration string.
233
- *
234
- * @param options.durationTypeForZero - Defaults to 'milliseconds'
235
- * @param options.style - Defaults to 'short'
236
- */
237
- export function readableDuration(
238
- ms: number,
239
- options?: { durationTypeForZero?: DurationType; style?: DurationStyle },
240
- ): string {
241
- const duration = msToDuration(ms, options?.durationTypeForZero);
242
-
243
- return formatDuration(duration, options?.style);
244
- }
245
-
246
- /** A shortened duration string useful for logging timings. */
247
- export function elapsed(ms: number): string {
248
- // Use long format for 1 minute or over.
249
- if (ms > MS_PER_MINUTE) {
250
- return readableDuration(ms);
251
- }
252
-
253
- // Use seconds format for over 100ms.
254
- if (ms > 100) {
255
- return `${(ms / 1000).toFixed(3)}s`;
256
- }
257
-
258
- // Use milliseconds format.
259
- return ms + "ms";
260
- }
package/src/isEmpty.ts DELETED
@@ -1,32 +0,0 @@
1
- /**
2
- * Returns true if `t` is empty.
3
- */
4
- export function isEmpty(t: unknown): boolean {
5
- // Anything falsy is considered empty.
6
- if (!t) {
7
- return true;
8
- }
9
-
10
- // Arrays are also of type `object`.
11
- if (typeof t !== "object") {
12
- return false;
13
- }
14
-
15
- // `length` includes arrays as well.
16
- if ("length" in t) {
17
- return t.length === 0;
18
- }
19
-
20
- // `size` is for Set, Map, Blob etc.
21
- if ("size" in t) {
22
- return t.size === 0;
23
- }
24
-
25
- // Super fast check for object emptiness.
26
- // https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object
27
- for (const k in t) {
28
- return false;
29
- }
30
-
31
- return true;
32
- }