@ztimson/utils 0.23.13 → 0.23.15
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 +138 -129
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +138 -129
- package/dist/index.mjs.map +1 -1
- package/dist/string.d.ts +4 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -543,6 +543,14 @@ ${opts.message || this.desc}`;
|
|
|
543
543
|
const NUMBER_LIST = "0123456789";
|
|
544
544
|
const SYMBOL_LIST = "~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/";
|
|
545
545
|
const CHAR_LIST = LETTER_LIST + LETTER_LIST.toLowerCase() + NUMBER_LIST + SYMBOL_LIST;
|
|
546
|
+
function camelCase(text) {
|
|
547
|
+
if (!text) return "";
|
|
548
|
+
text = text.replaceAll(/^[0-9]+/g, "").replaceAll(/[^a-zA-Z0-9]+(\w?)/g, (...args) => {
|
|
549
|
+
var _a;
|
|
550
|
+
return ((_a = args[1]) == null ? void 0 : _a.toUpperCase()) || "";
|
|
551
|
+
});
|
|
552
|
+
return text[0].toLowerCase() + text.slice(1);
|
|
553
|
+
}
|
|
546
554
|
function formatBytes(bytes, decimals = 2) {
|
|
547
555
|
if (bytes === 0) return "0 Bytes";
|
|
548
556
|
const k = 1024;
|
|
@@ -764,6 +772,134 @@ ${opts.message || this.desc}`;
|
|
|
764
772
|
return this.from(super.finally(res));
|
|
765
773
|
}
|
|
766
774
|
}
|
|
775
|
+
function adjustedInterval(cb, ms) {
|
|
776
|
+
let cancel = false, timeout = null;
|
|
777
|
+
const p2 = async () => {
|
|
778
|
+
if (cancel) return;
|
|
779
|
+
const start = (/* @__PURE__ */ new Date()).getTime();
|
|
780
|
+
await cb();
|
|
781
|
+
const end = (/* @__PURE__ */ new Date()).getTime();
|
|
782
|
+
timeout = setTimeout(() => p2(), ms - (end - start) || 1);
|
|
783
|
+
};
|
|
784
|
+
p2();
|
|
785
|
+
return () => {
|
|
786
|
+
cancel = true;
|
|
787
|
+
if (timeout) clearTimeout(timeout);
|
|
788
|
+
};
|
|
789
|
+
}
|
|
790
|
+
function formatDate(format = "YYYY-MM-DD H:mm", date = /* @__PURE__ */ new Date(), tz) {
|
|
791
|
+
const timezones = [
|
|
792
|
+
["IDLW", -12],
|
|
793
|
+
["SST", -11],
|
|
794
|
+
["HST", -10],
|
|
795
|
+
["AKST", -9],
|
|
796
|
+
["PST", -8],
|
|
797
|
+
["MST", -7],
|
|
798
|
+
["CST", -6],
|
|
799
|
+
["EST", -5],
|
|
800
|
+
["AST", -4],
|
|
801
|
+
["BRT", -3],
|
|
802
|
+
["MAT", -2],
|
|
803
|
+
["AZOT", -1],
|
|
804
|
+
["UTC", 0],
|
|
805
|
+
["CET", 1],
|
|
806
|
+
["EET", 2],
|
|
807
|
+
["MSK", 3],
|
|
808
|
+
["AST", 4],
|
|
809
|
+
["PKT", 5],
|
|
810
|
+
["IST", 5.5],
|
|
811
|
+
["BST", 6],
|
|
812
|
+
["ICT", 7],
|
|
813
|
+
["CST", 8],
|
|
814
|
+
["JST", 9],
|
|
815
|
+
["AEST", 10],
|
|
816
|
+
["SBT", 11],
|
|
817
|
+
["FJT", 12],
|
|
818
|
+
["TOT", 13],
|
|
819
|
+
["LINT", 14]
|
|
820
|
+
];
|
|
821
|
+
function adjustTz(date2, gmt) {
|
|
822
|
+
const currentOffset = date2.getTimezoneOffset();
|
|
823
|
+
const adjustedOffset = gmt * 60;
|
|
824
|
+
return new Date(date2.getTime() + (adjustedOffset + currentOffset) * 6e4);
|
|
825
|
+
}
|
|
826
|
+
function day(num) {
|
|
827
|
+
return ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][num] || "Unknown";
|
|
828
|
+
}
|
|
829
|
+
function doy(date2) {
|
|
830
|
+
const start = /* @__PURE__ */ new Date(`${date2.getFullYear()}-01-01 0:00:00`);
|
|
831
|
+
return Math.ceil((date2.getTime() - start.getTime()) / (1e3 * 60 * 60 * 24));
|
|
832
|
+
}
|
|
833
|
+
function month(num) {
|
|
834
|
+
return ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][num] || "Unknown";
|
|
835
|
+
}
|
|
836
|
+
function suffix(num) {
|
|
837
|
+
if (num % 100 >= 11 && num % 100 <= 13) return `${num}th`;
|
|
838
|
+
switch (num % 10) {
|
|
839
|
+
case 1:
|
|
840
|
+
return `${num}st`;
|
|
841
|
+
case 2:
|
|
842
|
+
return `${num}nd`;
|
|
843
|
+
case 3:
|
|
844
|
+
return `${num}rd`;
|
|
845
|
+
default:
|
|
846
|
+
return `${num}th`;
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
function tzOffset(offset) {
|
|
850
|
+
const hours = ~~(offset / 60);
|
|
851
|
+
const minutes = offset % 60;
|
|
852
|
+
return (offset > 0 ? "-" : "") + `${hours}:${minutes.toString().padStart(2, "0")}`;
|
|
853
|
+
}
|
|
854
|
+
if (typeof date == "number" || typeof date == "string") date = new Date(date);
|
|
855
|
+
let t;
|
|
856
|
+
if (tz == null) tz = -(date.getTimezoneOffset() / 60);
|
|
857
|
+
t = timezones.find((t2) => isNaN(tz) ? t2[0] == tz : t2[1] == tz);
|
|
858
|
+
if (!t) throw new Error(`Unknown timezone: ${tz}`);
|
|
859
|
+
date = adjustTz(date, t[1]);
|
|
860
|
+
const tokens = {
|
|
861
|
+
"YYYY": date.getFullYear().toString(),
|
|
862
|
+
"YY": date.getFullYear().toString().slice(2),
|
|
863
|
+
"MMMM": month(date.getMonth()),
|
|
864
|
+
"MMM": month(date.getMonth()).slice(0, 3),
|
|
865
|
+
"MM": (date.getMonth() + 1).toString().padStart(2, "0"),
|
|
866
|
+
"M": (date.getMonth() + 1).toString(),
|
|
867
|
+
"DDD": doy(date).toString(),
|
|
868
|
+
"DD": date.getDate().toString().padStart(2, "0"),
|
|
869
|
+
"Do": suffix(date.getDate()),
|
|
870
|
+
"D": date.getDate().toString(),
|
|
871
|
+
"dddd": day(date.getDay()),
|
|
872
|
+
"ddd": day(date.getDay()).slice(0, 3),
|
|
873
|
+
"HH": date.getHours().toString().padStart(2, "0"),
|
|
874
|
+
"H": date.getHours().toString(),
|
|
875
|
+
"hh": (date.getHours() % 12 || 12).toString().padStart(2, "0"),
|
|
876
|
+
"h": (date.getHours() % 12 || 12).toString(),
|
|
877
|
+
"mm": date.getMinutes().toString().padStart(2, "0"),
|
|
878
|
+
"m": date.getMinutes().toString(),
|
|
879
|
+
"ss": date.getSeconds().toString().padStart(2, "0"),
|
|
880
|
+
"s": date.getSeconds().toString(),
|
|
881
|
+
"SSS": date.getMilliseconds().toString().padStart(3, "0"),
|
|
882
|
+
"A": date.getHours() >= 12 ? "PM" : "AM",
|
|
883
|
+
"a": date.getHours() >= 12 ? "pm" : "am",
|
|
884
|
+
"ZZ": tzOffset(t[1] * 60).replace(":", ""),
|
|
885
|
+
"Z": tzOffset(t[1] * 60),
|
|
886
|
+
"z": typeof tz == "string" ? tz : t[0]
|
|
887
|
+
};
|
|
888
|
+
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]);
|
|
889
|
+
}
|
|
890
|
+
function instantInterval(fn, interval) {
|
|
891
|
+
fn();
|
|
892
|
+
return setInterval(fn, interval);
|
|
893
|
+
}
|
|
894
|
+
function sleep(ms) {
|
|
895
|
+
return new Promise((res) => setTimeout(res, ms));
|
|
896
|
+
}
|
|
897
|
+
async function sleepWhile(fn, checkInterval = 100) {
|
|
898
|
+
while (await fn()) await sleep(checkInterval);
|
|
899
|
+
}
|
|
900
|
+
function timeUntil(date) {
|
|
901
|
+
return (date instanceof Date ? date.getTime() : date) - (/* @__PURE__ */ new Date()).getTime();
|
|
902
|
+
}
|
|
767
903
|
function downloadFile(blob, name) {
|
|
768
904
|
if (!(blob instanceof Blob)) blob = new Blob(makeArray(blob));
|
|
769
905
|
const url = URL.createObjectURL(blob);
|
|
@@ -803,7 +939,7 @@ ${opts.message || this.desc}`;
|
|
|
803
939
|
}
|
|
804
940
|
function timestampFilename(name, date = /* @__PURE__ */ new Date()) {
|
|
805
941
|
if (typeof date == "number" || typeof date == "string") date = new Date(date);
|
|
806
|
-
const timestamp =
|
|
942
|
+
const timestamp = formatDate("YYYY-MM-DD_HH:mm:ss", date);
|
|
807
943
|
return name ? name.replace("{{TIMESTAMP}}", timestamp) : timestamp;
|
|
808
944
|
}
|
|
809
945
|
function uploadWithProgress(options) {
|
|
@@ -1530,134 +1666,6 @@ ${opts.message || this.desc}`;
|
|
|
1530
1666
|
});
|
|
1531
1667
|
});
|
|
1532
1668
|
}
|
|
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
1669
|
function typeKeys() {
|
|
1662
1670
|
return Object.keys({});
|
|
1663
1671
|
}
|
|
@@ -1701,6 +1709,7 @@ ${opts.message || this.desc}`;
|
|
|
1701
1709
|
exports.adjustedInterval = adjustedInterval;
|
|
1702
1710
|
exports.arrayDiff = arrayDiff;
|
|
1703
1711
|
exports.blackOrWhite = blackOrWhite;
|
|
1712
|
+
exports.camelCase = camelCase;
|
|
1704
1713
|
exports.caseInsensitiveSort = caseInsensitiveSort;
|
|
1705
1714
|
exports.clean = clean;
|
|
1706
1715
|
exports.dec2Frac = dec2Frac;
|