@ztimson/utils 0.23.7 → 0.23.9

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.cjs CHANGED
@@ -533,7 +533,7 @@ ${opts.message || this.desc}`;
533
533
  }
534
534
  }
535
535
  function blackOrWhite(background) {
536
- const exploded = background.match(background.length >= 6 ? /\w\w/g : /\w/g);
536
+ const exploded = background == null ? void 0 : background.match(background.length >= 6 ? /\w\w/g : /\w/g);
537
537
  if (!exploded) return "black";
538
538
  const [r2, g, b2] = exploded.map((hex) => parseInt(hex, 16));
539
539
  const luminance = (0.299 * r2 + 0.587 * g + 0.114 * b2) / 255;
@@ -1531,7 +1531,7 @@ ${opts.message || this.desc}`;
1531
1531
  if (timeout) clearTimeout(timeout);
1532
1532
  };
1533
1533
  }
1534
- function formatDate(date, format = "YYYY-MM-DD H:mm", tz) {
1534
+ function formatDate(format = "YYYY-MM-DD H:mm", date = /* @__PURE__ */ new Date(), tz) {
1535
1535
  const timezones = [
1536
1536
  ["IDLW", -12],
1537
1537
  ["SST", -11],
@@ -1568,70 +1568,26 @@ ${opts.message || this.desc}`;
1568
1568
  return new Date(date2.getTime() + (adjustedOffset + currentOffset) * 6e4);
1569
1569
  }
1570
1570
  function day(num) {
1571
- switch (num) {
1572
- case 0:
1573
- return "Sunday";
1574
- case 1:
1575
- return "Monday";
1576
- case 2:
1577
- return "Tuesday";
1578
- case 3:
1579
- return "Wednesday";
1580
- case 4:
1581
- return "Thursday";
1582
- case 5:
1583
- return "Friday";
1584
- case 6:
1585
- return "Saturday";
1586
- default:
1587
- return "Unknown";
1588
- }
1571
+ return ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][num] || "Unknown";
1589
1572
  }
1590
1573
  function doy(date2) {
1591
1574
  const start = /* @__PURE__ */ new Date(`${date2.getFullYear()}-01-01 0:00:00`);
1592
1575
  return Math.ceil((date2.getTime() - start.getTime()) / (1e3 * 60 * 60 * 24));
1593
1576
  }
1594
1577
  function month(num) {
1595
- switch (num) {
1596
- case 0:
1597
- return "January";
1578
+ return ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][num] || "Unknown";
1579
+ }
1580
+ function suffix(num) {
1581
+ if (num % 100 >= 11 && num % 100 <= 13) return `${num}th`;
1582
+ switch (num % 10) {
1598
1583
  case 1:
1599
- return "February";
1584
+ return `${num}st`;
1600
1585
  case 2:
1601
- return "March";
1586
+ return `${num}nd`;
1602
1587
  case 3:
1603
- return "April";
1604
- case 4:
1605
- return "May";
1606
- case 5:
1607
- return "June";
1608
- case 6:
1609
- return "July";
1610
- case 7:
1611
- return "August";
1612
- case 8:
1613
- return "September";
1614
- case 9:
1615
- return "October";
1616
- case 10:
1617
- return "November";
1618
- case 11:
1619
- return "December";
1620
- default:
1621
- return "Unknown";
1622
- }
1623
- }
1624
- function suffix(num) {
1625
- let n = num.toString();
1626
- switch (n.at(-1)) {
1627
- case "1":
1628
- return num + "st";
1629
- case "2":
1630
- return num + "nd";
1631
- case "3":
1632
- return num + "rd";
1588
+ return `${num}rd`;
1633
1589
  default:
1634
- return num + "th";
1590
+ return `${num}th`;
1635
1591
  }
1636
1592
  }
1637
1593
  function tzOffset(offset) {
@@ -1645,7 +1601,35 @@ ${opts.message || this.desc}`;
1645
1601
  t = timezones.find((t2) => isNaN(tz) ? t2[0] == tz : t2[1] == tz);
1646
1602
  if (!t) throw new Error(`Unknown timezone: ${tz}`);
1647
1603
  date = adjustTz(date, t[1]);
1648
- 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]);
1604
+ const tokens = {
1605
+ "YYYY": date.getFullYear().toString(),
1606
+ "YY": date.getFullYear().toString().slice(2),
1607
+ "MMMM": month(date.getMonth()),
1608
+ "MMM": month(date.getMonth()).slice(0, 3),
1609
+ "MM": (date.getMonth() + 1).toString().padStart(2, "0"),
1610
+ "M": (date.getMonth() + 1).toString(),
1611
+ "DDD": doy(date).toString(),
1612
+ "DD": date.getDate().toString().padStart(2, "0"),
1613
+ "Do": suffix(date.getDate()),
1614
+ "D": date.getDate().toString(),
1615
+ "dddd": day(date.getDay()),
1616
+ "ddd": day(date.getDay()).slice(0, 3),
1617
+ "HH": date.getHours().toString().padStart(2, "0"),
1618
+ "H": date.getHours().toString(),
1619
+ "hh": (date.getHours() % 12 || 12).toString().padStart(2, "0"),
1620
+ "h": (date.getHours() % 12 || 12).toString(),
1621
+ "mm": date.getMinutes().toString().padStart(2, "0"),
1622
+ "m": date.getMinutes().toString(),
1623
+ "ss": date.getSeconds().toString().padStart(2, "0"),
1624
+ "s": date.getSeconds().toString(),
1625
+ "SSS": date.getMilliseconds().toString().padStart(3, "0"),
1626
+ "A": date.getHours() >= 12 ? "PM" : "AM",
1627
+ "a": date.getHours() >= 12 ? "pm" : "am",
1628
+ "ZZ": tzOffset(t[1] * 60).replace(":", ""),
1629
+ "Z": tzOffset(t[1] * 60),
1630
+ "z": typeof tz == "string" ? tz : t[0]
1631
+ };
1632
+ 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]);
1649
1633
  }
1650
1634
  function instantInterval(fn, interval) {
1651
1635
  fn();