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