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