@ztimson/utils 0.26.15 → 0.26.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -638,6 +638,18 @@ function formatBytes(bytes, decimals = 2) {
638
638
  const i = Math.floor(Math.log(bytes) / Math.log(k));
639
639
  return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + " " + sizes[i];
640
640
  }
641
+ function formatMs(number) {
642
+ const ms = parseInt(number, 10);
643
+ if (isNaN(ms) || ms < 0) return "Invalid input";
644
+ const seconds = ms / 1e3;
645
+ const minutes = seconds / 60;
646
+ const hours = minutes / 60;
647
+ const days = hours / 24;
648
+ if (days >= 1) return `${days.toFixed(1)} days`;
649
+ else if (hours >= 1) return `${hours.toFixed(1)} hours`;
650
+ else if (minutes >= 1) return `${minutes.toFixed(1)} minutes`;
651
+ else return `${seconds.toFixed(1)} seconds`;
652
+ }
641
653
  function formatPhoneNumber(number) {
642
654
  const parts = /(\+?1)?.*?(\d{3}).*?(\d{3}).*?(\d{4})/g.exec(number);
643
655
  if (!parts) throw new Error(`Number cannot be parsed: ${number}`);
@@ -1525,7 +1537,7 @@ const _Http = class _Http {
1525
1537
  if (!this.url && !opts.url) throw new Error("URL needs to be set");
1526
1538
  let url = ((_a = opts.url) == null ? void 0 : _a.startsWith("http")) ? opts.url : (this.url || "") + (opts.url || "");
1527
1539
  url = url.replaceAll(/([^:]\/)\/+/g, "$1");
1528
- if (opts.fragment) url.includes("#") ? url.replace(/#.*(\?|\n)/g, (match, arg1) => `#${opts.fragment}${arg1}`) : `${url}#${opts.fragment}`;
1540
+ if (opts.fragment) url.includes("#") ? url.replace(/#.*([?\n])/g, (match, arg1) => `#${opts.fragment}${arg1}`) : `${url}#${opts.fragment}`;
1529
1541
  if (opts.query) {
1530
1542
  const q = Array.isArray(opts.query) ? opts.query : Object.keys(opts.query).map((k) => ({ key: k, value: opts.query[k] }));
1531
1543
  url += (url.includes("?") ? "&" : "?") + q.map((q2) => `${q2.key}=${q2.value}`).join("&");
@@ -2314,6 +2326,7 @@ export {
2314
2326
  formData,
2315
2327
  formatBytes,
2316
2328
  formatDate,
2329
+ formatMs,
2317
2330
  formatPhoneNumber,
2318
2331
  fracToDec,
2319
2332
  fromCsv,