@ztimson/utils 0.23.13 → 0.23.14

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
@@ -760,6 +760,134 @@ class PromiseProgress extends Promise {
760
760
  return this.from(super.finally(res));
761
761
  }
762
762
  }
763
+ function adjustedInterval(cb, ms) {
764
+ let cancel = false, timeout = null;
765
+ const p2 = async () => {
766
+ if (cancel) return;
767
+ const start = (/* @__PURE__ */ new Date()).getTime();
768
+ await cb();
769
+ const end = (/* @__PURE__ */ new Date()).getTime();
770
+ timeout = setTimeout(() => p2(), ms - (end - start) || 1);
771
+ };
772
+ p2();
773
+ return () => {
774
+ cancel = true;
775
+ if (timeout) clearTimeout(timeout);
776
+ };
777
+ }
778
+ function formatDate(format = "YYYY-MM-DD H:mm", date = /* @__PURE__ */ new Date(), tz) {
779
+ const timezones = [
780
+ ["IDLW", -12],
781
+ ["SST", -11],
782
+ ["HST", -10],
783
+ ["AKST", -9],
784
+ ["PST", -8],
785
+ ["MST", -7],
786
+ ["CST", -6],
787
+ ["EST", -5],
788
+ ["AST", -4],
789
+ ["BRT", -3],
790
+ ["MAT", -2],
791
+ ["AZOT", -1],
792
+ ["UTC", 0],
793
+ ["CET", 1],
794
+ ["EET", 2],
795
+ ["MSK", 3],
796
+ ["AST", 4],
797
+ ["PKT", 5],
798
+ ["IST", 5.5],
799
+ ["BST", 6],
800
+ ["ICT", 7],
801
+ ["CST", 8],
802
+ ["JST", 9],
803
+ ["AEST", 10],
804
+ ["SBT", 11],
805
+ ["FJT", 12],
806
+ ["TOT", 13],
807
+ ["LINT", 14]
808
+ ];
809
+ function adjustTz(date2, gmt) {
810
+ const currentOffset = date2.getTimezoneOffset();
811
+ const adjustedOffset = gmt * 60;
812
+ return new Date(date2.getTime() + (adjustedOffset + currentOffset) * 6e4);
813
+ }
814
+ function day(num) {
815
+ return ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][num] || "Unknown";
816
+ }
817
+ function doy(date2) {
818
+ const start = /* @__PURE__ */ new Date(`${date2.getFullYear()}-01-01 0:00:00`);
819
+ return Math.ceil((date2.getTime() - start.getTime()) / (1e3 * 60 * 60 * 24));
820
+ }
821
+ function month(num) {
822
+ return ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][num] || "Unknown";
823
+ }
824
+ function suffix(num) {
825
+ if (num % 100 >= 11 && num % 100 <= 13) return `${num}th`;
826
+ switch (num % 10) {
827
+ case 1:
828
+ return `${num}st`;
829
+ case 2:
830
+ return `${num}nd`;
831
+ case 3:
832
+ return `${num}rd`;
833
+ default:
834
+ return `${num}th`;
835
+ }
836
+ }
837
+ function tzOffset(offset) {
838
+ const hours = ~~(offset / 60);
839
+ const minutes = offset % 60;
840
+ return (offset > 0 ? "-" : "") + `${hours}:${minutes.toString().padStart(2, "0")}`;
841
+ }
842
+ if (typeof date == "number" || typeof date == "string") date = new Date(date);
843
+ let t;
844
+ if (tz == null) tz = -(date.getTimezoneOffset() / 60);
845
+ t = timezones.find((t2) => isNaN(tz) ? t2[0] == tz : t2[1] == tz);
846
+ if (!t) throw new Error(`Unknown timezone: ${tz}`);
847
+ date = adjustTz(date, t[1]);
848
+ const tokens = {
849
+ "YYYY": date.getFullYear().toString(),
850
+ "YY": date.getFullYear().toString().slice(2),
851
+ "MMMM": month(date.getMonth()),
852
+ "MMM": month(date.getMonth()).slice(0, 3),
853
+ "MM": (date.getMonth() + 1).toString().padStart(2, "0"),
854
+ "M": (date.getMonth() + 1).toString(),
855
+ "DDD": doy(date).toString(),
856
+ "DD": date.getDate().toString().padStart(2, "0"),
857
+ "Do": suffix(date.getDate()),
858
+ "D": date.getDate().toString(),
859
+ "dddd": day(date.getDay()),
860
+ "ddd": day(date.getDay()).slice(0, 3),
861
+ "HH": date.getHours().toString().padStart(2, "0"),
862
+ "H": date.getHours().toString(),
863
+ "hh": (date.getHours() % 12 || 12).toString().padStart(2, "0"),
864
+ "h": (date.getHours() % 12 || 12).toString(),
865
+ "mm": date.getMinutes().toString().padStart(2, "0"),
866
+ "m": date.getMinutes().toString(),
867
+ "ss": date.getSeconds().toString().padStart(2, "0"),
868
+ "s": date.getSeconds().toString(),
869
+ "SSS": date.getMilliseconds().toString().padStart(3, "0"),
870
+ "A": date.getHours() >= 12 ? "PM" : "AM",
871
+ "a": date.getHours() >= 12 ? "pm" : "am",
872
+ "ZZ": tzOffset(t[1] * 60).replace(":", ""),
873
+ "Z": tzOffset(t[1] * 60),
874
+ "z": typeof tz == "string" ? tz : t[0]
875
+ };
876
+ 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]);
877
+ }
878
+ function instantInterval(fn, interval) {
879
+ fn();
880
+ return setInterval(fn, interval);
881
+ }
882
+ function sleep(ms) {
883
+ return new Promise((res) => setTimeout(res, ms));
884
+ }
885
+ async function sleepWhile(fn, checkInterval = 100) {
886
+ while (await fn()) await sleep(checkInterval);
887
+ }
888
+ function timeUntil(date) {
889
+ return (date instanceof Date ? date.getTime() : date) - (/* @__PURE__ */ new Date()).getTime();
890
+ }
763
891
  function downloadFile(blob, name) {
764
892
  if (!(blob instanceof Blob)) blob = new Blob(makeArray(blob));
765
893
  const url = URL.createObjectURL(blob);
@@ -799,7 +927,7 @@ function fileText(file) {
799
927
  }
800
928
  function timestampFilename(name, date = /* @__PURE__ */ new Date()) {
801
929
  if (typeof date == "number" || typeof date == "string") date = new Date(date);
802
- const timestamp = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, "0")}-${date.getDate().toString().padStart(2, "0")}_${date.getHours().toString().padStart(2, "0")}-${date.getMinutes().toString().padStart(2, "0")}-${date.getSeconds().toString().padStart(2, "0")}`;
930
+ const timestamp = formatDate("YYYY-MM-DD_HH:mm:ss", date);
803
931
  return name ? name.replace("{{TIMESTAMP}}", timestamp) : timestamp;
804
932
  }
805
933
  function uploadWithProgress(options) {
@@ -1526,134 +1654,6 @@ function search(rows, search, regex, transform = (r2) => r2) {
1526
1654
  });
1527
1655
  });
1528
1656
  }
1529
- function adjustedInterval(cb, ms) {
1530
- let cancel = false, timeout = null;
1531
- const p2 = async () => {
1532
- if (cancel) return;
1533
- const start = (/* @__PURE__ */ new Date()).getTime();
1534
- await cb();
1535
- const end = (/* @__PURE__ */ new Date()).getTime();
1536
- timeout = setTimeout(() => p2(), ms - (end - start) || 1);
1537
- };
1538
- p2();
1539
- return () => {
1540
- cancel = true;
1541
- if (timeout) clearTimeout(timeout);
1542
- };
1543
- }
1544
- function formatDate(format = "YYYY-MM-DD H:mm", date = /* @__PURE__ */ new Date(), tz) {
1545
- const timezones = [
1546
- ["IDLW", -12],
1547
- ["SST", -11],
1548
- ["HST", -10],
1549
- ["AKST", -9],
1550
- ["PST", -8],
1551
- ["MST", -7],
1552
- ["CST", -6],
1553
- ["EST", -5],
1554
- ["AST", -4],
1555
- ["BRT", -3],
1556
- ["MAT", -2],
1557
- ["AZOT", -1],
1558
- ["UTC", 0],
1559
- ["CET", 1],
1560
- ["EET", 2],
1561
- ["MSK", 3],
1562
- ["AST", 4],
1563
- ["PKT", 5],
1564
- ["IST", 5.5],
1565
- ["BST", 6],
1566
- ["ICT", 7],
1567
- ["CST", 8],
1568
- ["JST", 9],
1569
- ["AEST", 10],
1570
- ["SBT", 11],
1571
- ["FJT", 12],
1572
- ["TOT", 13],
1573
- ["LINT", 14]
1574
- ];
1575
- function adjustTz(date2, gmt) {
1576
- const currentOffset = date2.getTimezoneOffset();
1577
- const adjustedOffset = gmt * 60;
1578
- return new Date(date2.getTime() + (adjustedOffset + currentOffset) * 6e4);
1579
- }
1580
- function day(num) {
1581
- return ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][num] || "Unknown";
1582
- }
1583
- function doy(date2) {
1584
- const start = /* @__PURE__ */ new Date(`${date2.getFullYear()}-01-01 0:00:00`);
1585
- return Math.ceil((date2.getTime() - start.getTime()) / (1e3 * 60 * 60 * 24));
1586
- }
1587
- function month(num) {
1588
- return ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][num] || "Unknown";
1589
- }
1590
- function suffix(num) {
1591
- if (num % 100 >= 11 && num % 100 <= 13) return `${num}th`;
1592
- switch (num % 10) {
1593
- case 1:
1594
- return `${num}st`;
1595
- case 2:
1596
- return `${num}nd`;
1597
- case 3:
1598
- return `${num}rd`;
1599
- default:
1600
- return `${num}th`;
1601
- }
1602
- }
1603
- function tzOffset(offset) {
1604
- const hours = ~~(offset / 60);
1605
- const minutes = offset % 60;
1606
- return (offset > 0 ? "-" : "") + `${hours}:${minutes.toString().padStart(2, "0")}`;
1607
- }
1608
- if (typeof date == "number" || typeof date == "string") date = new Date(date);
1609
- let t;
1610
- if (tz == null) tz = -(date.getTimezoneOffset() / 60);
1611
- t = timezones.find((t2) => isNaN(tz) ? t2[0] == tz : t2[1] == tz);
1612
- if (!t) throw new Error(`Unknown timezone: ${tz}`);
1613
- date = adjustTz(date, t[1]);
1614
- const tokens = {
1615
- "YYYY": date.getFullYear().toString(),
1616
- "YY": date.getFullYear().toString().slice(2),
1617
- "MMMM": month(date.getMonth()),
1618
- "MMM": month(date.getMonth()).slice(0, 3),
1619
- "MM": (date.getMonth() + 1).toString().padStart(2, "0"),
1620
- "M": (date.getMonth() + 1).toString(),
1621
- "DDD": doy(date).toString(),
1622
- "DD": date.getDate().toString().padStart(2, "0"),
1623
- "Do": suffix(date.getDate()),
1624
- "D": date.getDate().toString(),
1625
- "dddd": day(date.getDay()),
1626
- "ddd": day(date.getDay()).slice(0, 3),
1627
- "HH": date.getHours().toString().padStart(2, "0"),
1628
- "H": date.getHours().toString(),
1629
- "hh": (date.getHours() % 12 || 12).toString().padStart(2, "0"),
1630
- "h": (date.getHours() % 12 || 12).toString(),
1631
- "mm": date.getMinutes().toString().padStart(2, "0"),
1632
- "m": date.getMinutes().toString(),
1633
- "ss": date.getSeconds().toString().padStart(2, "0"),
1634
- "s": date.getSeconds().toString(),
1635
- "SSS": date.getMilliseconds().toString().padStart(3, "0"),
1636
- "A": date.getHours() >= 12 ? "PM" : "AM",
1637
- "a": date.getHours() >= 12 ? "pm" : "am",
1638
- "ZZ": tzOffset(t[1] * 60).replace(":", ""),
1639
- "Z": tzOffset(t[1] * 60),
1640
- "z": typeof tz == "string" ? tz : t[0]
1641
- };
1642
- 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]);
1643
- }
1644
- function instantInterval(fn, interval) {
1645
- fn();
1646
- return setInterval(fn, interval);
1647
- }
1648
- function sleep(ms) {
1649
- return new Promise((res) => setTimeout(res, ms));
1650
- }
1651
- async function sleepWhile(fn, checkInterval = 100) {
1652
- while (await fn()) await sleep(checkInterval);
1653
- }
1654
- function timeUntil(date) {
1655
- return (date instanceof Date ? date.getTime() : date) - (/* @__PURE__ */ new Date()).getTime();
1656
- }
1657
1657
  function typeKeys() {
1658
1658
  return Object.keys({});
1659
1659
  }