@ztimson/utils 0.23.7 → 0.23.8

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
@@ -1527,7 +1527,7 @@ function adjustedInterval(cb, ms) {
1527
1527
  if (timeout) clearTimeout(timeout);
1528
1528
  };
1529
1529
  }
1530
- function formatDate(date, format = "YYYY-MM-DD H:mm", tz) {
1530
+ function formatDate(format = "YYYY-MM-DD H:mm", date = /* @__PURE__ */ new Date(), tz) {
1531
1531
  const timezones = [
1532
1532
  ["IDLW", -12],
1533
1533
  ["SST", -11],
@@ -1564,70 +1564,26 @@ function formatDate(date, format = "YYYY-MM-DD H:mm", tz) {
1564
1564
  return new Date(date2.getTime() + (adjustedOffset + currentOffset) * 6e4);
1565
1565
  }
1566
1566
  function day(num) {
1567
- switch (num) {
1568
- case 0:
1569
- return "Sunday";
1570
- case 1:
1571
- return "Monday";
1572
- case 2:
1573
- return "Tuesday";
1574
- case 3:
1575
- return "Wednesday";
1576
- case 4:
1577
- return "Thursday";
1578
- case 5:
1579
- return "Friday";
1580
- case 6:
1581
- return "Saturday";
1582
- default:
1583
- return "Unknown";
1584
- }
1567
+ return ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][num] || "Unknown";
1585
1568
  }
1586
1569
  function doy(date2) {
1587
1570
  const start = /* @__PURE__ */ new Date(`${date2.getFullYear()}-01-01 0:00:00`);
1588
1571
  return Math.ceil((date2.getTime() - start.getTime()) / (1e3 * 60 * 60 * 24));
1589
1572
  }
1590
1573
  function month(num) {
1591
- switch (num) {
1592
- case 0:
1593
- return "January";
1574
+ return ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][num] || "Unknown";
1575
+ }
1576
+ function suffix(num) {
1577
+ if (num % 100 >= 11 && num % 100 <= 13) return `${num}th`;
1578
+ switch (num % 10) {
1594
1579
  case 1:
1595
- return "February";
1580
+ return `${num}st`;
1596
1581
  case 2:
1597
- return "March";
1582
+ return `${num}nd`;
1598
1583
  case 3:
1599
- return "April";
1600
- case 4:
1601
- return "May";
1602
- case 5:
1603
- return "June";
1604
- case 6:
1605
- return "July";
1606
- case 7:
1607
- return "August";
1608
- case 8:
1609
- return "September";
1610
- case 9:
1611
- return "October";
1612
- case 10:
1613
- return "November";
1614
- case 11:
1615
- return "December";
1616
- default:
1617
- return "Unknown";
1618
- }
1619
- }
1620
- function suffix(num) {
1621
- let n = num.toString();
1622
- switch (n.at(-1)) {
1623
- case "1":
1624
- return num + "st";
1625
- case "2":
1626
- return num + "nd";
1627
- case "3":
1628
- return num + "rd";
1584
+ return `${num}rd`;
1629
1585
  default:
1630
- return num + "th";
1586
+ return `${num}th`;
1631
1587
  }
1632
1588
  }
1633
1589
  function tzOffset(offset) {
@@ -1641,7 +1597,35 @@ function formatDate(date, format = "YYYY-MM-DD H:mm", tz) {
1641
1597
  t = timezones.find((t2) => isNaN(tz) ? t2[0] == tz : t2[1] == tz);
1642
1598
  if (!t) throw new Error(`Unknown timezone: ${tz}`);
1643
1599
  date = adjustTz(date, t[1]);
1644
- return format.replaceAll("YYYY", date.getFullYear().toString()).replaceAll("YY", date.getFullYear().toString().slice(2)).replaceAll("MMMM", month(date.getMonth())).replaceAll("MMM", month(date.getMonth()).slice(0, 3)).replaceAll("MM", (date.getMonth() + 1).toString().padStart(2, "0")).replaceAll("M", (date.getMonth() + 1).toString()).replaceAll("DDD", doy(date).toString()).replaceAll("DD", date.getDate().toString().padStart(2, "0")).replaceAll("Do", suffix(date.getDate())).replaceAll("D", date.getDate().toString()).replaceAll("dddd", day(date.getDay())).replaceAll("ddd", day(date.getDay()).slice(0, 2)).replaceAll("dd", date.getDate().toString().padStart(2, "0")).replaceAll("d", date.getDay().toString()).replaceAll("HH", date.getHours().toString().padStart(2, "0")).replaceAll("H", date.getHours().toString()).replaceAll("hh", (date.getHours() > 12 ? date.getHours() - 12 : date.getHours()).toString().padStart(2, "0")).replaceAll("h", (date.getHours() > 12 ? date.getHours() - 12 : date.getHours()).toString()).replaceAll("mm", date.getMinutes().toString().padStart(2, "0")).replaceAll("m", date.getMinutes().toString()).replaceAll("ss", date.getSeconds().toString().padStart(2, "0")).replaceAll("s", date.getSeconds().toString()).replaceAll("SSS", date.getMilliseconds().toString().padEnd(3, "0")).replaceAll("SS", date.getMilliseconds().toString().slice(0, 1).padEnd(2, "0")).replaceAll("S", date.getMilliseconds().toString()[0]).replaceAll("A", date.getHours() >= 12 ? "PM" : "AM").replaceAll("a", date.getHours() >= 12 ? "pm" : "am").replaceAll("ZZ", tzOffset(t[1] * 60).replace(":", "")).replaceAll("Z", tzOffset(t[1] * 60)).replaceAll("z", typeof tz == "string" ? tz : t[0]);
1600
+ const tokens = {
1601
+ "YYYY": date.getFullYear().toString(),
1602
+ "YY": date.getFullYear().toString().slice(2),
1603
+ "MMMM": month(date.getMonth()),
1604
+ "MMM": month(date.getMonth()).slice(0, 3),
1605
+ "MM": (date.getMonth() + 1).toString().padStart(2, "0"),
1606
+ "M": (date.getMonth() + 1).toString(),
1607
+ "DDD": doy(date).toString(),
1608
+ "DD": date.getDate().toString().padStart(2, "0"),
1609
+ "Do": suffix(date.getDate()),
1610
+ "D": date.getDate().toString(),
1611
+ "dddd": day(date.getDay()),
1612
+ "ddd": day(date.getDay()).slice(0, 3),
1613
+ "HH": date.getHours().toString().padStart(2, "0"),
1614
+ "H": date.getHours().toString(),
1615
+ "hh": (date.getHours() % 12 || 12).toString().padStart(2, "0"),
1616
+ "h": (date.getHours() % 12 || 12).toString(),
1617
+ "mm": date.getMinutes().toString().padStart(2, "0"),
1618
+ "m": date.getMinutes().toString(),
1619
+ "ss": date.getSeconds().toString().padStart(2, "0"),
1620
+ "s": date.getSeconds().toString(),
1621
+ "SSS": date.getMilliseconds().toString().padStart(3, "0"),
1622
+ "A": date.getHours() >= 12 ? "PM" : "AM",
1623
+ "a": date.getHours() >= 12 ? "pm" : "am",
1624
+ "ZZ": tzOffset(t[1] * 60).replace(":", ""),
1625
+ "Z": tzOffset(t[1] * 60),
1626
+ "z": typeof tz == "string" ? tz : t[0]
1627
+ };
1628
+ return format.replace(/YYYY|YY|MMMM|MMM|MM|M|DDD|DD|Do|D|dddd|ddd|HH|H|hh|h|mm|m|ss|s|SSS|A|a|ZZ|Z|z/g, (token) => tokens[token]);
1645
1629
  }
1646
1630
  function instantInterval(fn, interval) {
1647
1631
  fn();