@ztimson/utils 0.22.6 → 0.22.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
@@ -1368,7 +1368,12 @@ function adjustedInterval(cb, ms) {
1368
1368
  if (timeout) clearTimeout(timeout);
1369
1369
  };
1370
1370
  }
1371
- function formatDate(date, format = "YYYY-MM-DD H:mm ") {
1371
+ function adjustTz(date, offset) {
1372
+ const currentOffset = date.getTimezoneOffset();
1373
+ offset = currentOffset - offset * 60;
1374
+ return new Date(date.getTime() + offset * 6e4);
1375
+ }
1376
+ function formatDate(date, format = "YYYY-MM-DD H:mm", tz) {
1372
1377
  if (typeof date == "number" || typeof date == "string") date = new Date(date);
1373
1378
  function day(num) {
1374
1379
  switch (num) {
@@ -1442,64 +1447,14 @@ function formatDate(date, format = "YYYY-MM-DD H:mm ") {
1442
1447
  const minutes = offset % 60;
1443
1448
  return (offset > 0 ? "-" : "") + `${hours}:${minutes.toString().padStart(2, "0")}`;
1444
1449
  }
1445
- function timezone(offset) {
1446
- const hours = offset / 60;
1447
- switch (hours) {
1448
- case -12:
1449
- return "IDLW";
1450
- case -11:
1451
- return "SST";
1452
- case -10:
1453
- return "HST";
1454
- case -9:
1455
- return "AKST";
1456
- case -8:
1457
- return "PST";
1458
- case -7:
1459
- return "MST";
1460
- case -6:
1461
- return "CST";
1462
- case -5:
1463
- return "EST";
1464
- case -4:
1465
- return "AST";
1466
- case -3:
1467
- return "ART";
1468
- case -2:
1469
- return "FNT";
1470
- case -1:
1471
- return "AZOT";
1472
- case 0:
1473
- return "UTC";
1474
- case 1:
1475
- return "CET";
1476
- case 2:
1477
- return "EET";
1478
- case 3:
1479
- return "MSK";
1480
- case 4:
1481
- return "SAMT";
1482
- case 5:
1483
- return "YEKT";
1484
- case 6:
1485
- return "OMST";
1486
- case 7:
1487
- return "KRAT";
1488
- case 8:
1489
- return "CST";
1490
- case 9:
1491
- return "JST";
1492
- case 10:
1493
- return "AEST";
1494
- case 11:
1495
- return "SBT";
1496
- case 12:
1497
- return "NZST";
1498
- default:
1499
- return "";
1500
- }
1450
+ function timezone(date2) {
1451
+ return new Intl.DateTimeFormat("en-US", { timeZoneName: "short" }).format(date2).split(" ").pop() || "";
1501
1452
  }
1502
- 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, 2)).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()).replaceAll("SS", date.getMilliseconds().toString().slice(0, 1)).replaceAll("S", date.getMilliseconds().toString()[0]).replaceAll("A", date.getHours() >= 12 ? "PM" : "AM").replaceAll("a", date.getHours() >= 12 ? "pm" : "am").replaceAll("ZZ", tzOffset(date.getTimezoneOffset()).replace(":", "")).replaceAll("Z", tzOffset(date.getTimezoneOffset())).replaceAll("z", timezone(date.getTimezoneOffset()));
1453
+ 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, 2)).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(isNaN(tz) ? date.getTimezoneOffset() : tz).replace(":", "")).replaceAll("Z", tzOffset(isNaN(tz) ? date.getTimezoneOffset() : tz)).replaceAll("z", typeof tz == "string" ? tz : timezone(date));
1454
+ }
1455
+ function instantInterval(fn, interval) {
1456
+ fn();
1457
+ return setInterval(fn, interval);
1503
1458
  }
1504
1459
  function sleep(ms) {
1505
1460
  return new Promise((res) => setTimeout(res, ms));
@@ -1549,6 +1504,7 @@ export {
1549
1504
  TypedEmitter,
1550
1505
  UnauthorizedError,
1551
1506
  addUnique,
1507
+ adjustTz,
1552
1508
  adjustedInterval,
1553
1509
  arrayDiff,
1554
1510
  caseInsensitiveSort,
@@ -1576,6 +1532,7 @@ export {
1576
1532
  gravatar,
1577
1533
  includes,
1578
1534
  insertAt,
1535
+ instantInterval,
1579
1536
  isEqual,
1580
1537
  jwtDecode,
1581
1538
  makeArray,