@ztimson/utils 0.21.6 → 0.22.1
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 +145 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +145 -16
- package/dist/index.mjs.map +1 -1
- package/dist/objects.d.ts +0 -1
- package/dist/string.d.ts +12 -0
- package/dist/time.d.ts +3 -2
- package/dist/types.d.ts +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -17,7 +17,11 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
17
17
|
return obj;
|
|
18
18
|
}
|
|
19
19
|
function deepCopy(value) {
|
|
20
|
-
|
|
20
|
+
try {
|
|
21
|
+
return structuredClone(value);
|
|
22
|
+
} catch {
|
|
23
|
+
return JSON.parse(JSONSanitize(value));
|
|
24
|
+
}
|
|
21
25
|
}
|
|
22
26
|
function deepMerge(target, ...sources) {
|
|
23
27
|
sources.forEach((s) => {
|
|
@@ -106,12 +110,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
106
110
|
}
|
|
107
111
|
}
|
|
108
112
|
function JSONSanitize(obj, space) {
|
|
109
|
-
let cache = [];
|
|
110
113
|
return JSON.stringify(obj, (key, value) => {
|
|
111
|
-
if (typeof value === "object" && value !== null) {
|
|
112
|
-
if (cache.includes(value)) return;
|
|
113
|
-
cache.push(value);
|
|
114
|
-
}
|
|
115
114
|
return value;
|
|
116
115
|
}, space);
|
|
117
116
|
}
|
|
@@ -961,7 +960,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
961
960
|
function formatPhoneNumber(number) {
|
|
962
961
|
const parts = /(\+?1)?.*?(\d{3}).*?(\d{3}).*?(\d{4})/g.exec(number);
|
|
963
962
|
if (!parts) throw new Error(`Number cannot be parsed: ${number}`);
|
|
964
|
-
return `${parts[1]
|
|
963
|
+
return `${parts[1] ? "+1" : ""} (${parts[2]}) ${parts[3]}-${parts[4]}`.trim();
|
|
965
964
|
}
|
|
966
965
|
function insertAt(target, str, index) {
|
|
967
966
|
return `${target.slice(0, index)}${str}${target.slice(index + 1)}`;
|
|
@@ -996,6 +995,11 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
996
995
|
return c;
|
|
997
996
|
}).join("");
|
|
998
997
|
}
|
|
998
|
+
function strSplice(str, start, deleteCount, insert = "") {
|
|
999
|
+
const before = str.slice(0, start);
|
|
1000
|
+
const after = str.slice(start + deleteCount);
|
|
1001
|
+
return before + insert + after;
|
|
1002
|
+
}
|
|
999
1003
|
function matchAll(value, regex) {
|
|
1000
1004
|
if (typeof regex === "string") {
|
|
1001
1005
|
regex = new RegExp(regex, "g");
|
|
@@ -1334,14 +1338,138 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
1334
1338
|
if (timeout) clearTimeout(timeout);
|
|
1335
1339
|
};
|
|
1336
1340
|
}
|
|
1337
|
-
function formatDate(date) {
|
|
1341
|
+
function formatDate(date, format = "YYYY-MM-DD H:mm ") {
|
|
1338
1342
|
if (typeof date == "number" || typeof date == "string") date = new Date(date);
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1343
|
+
function day(num) {
|
|
1344
|
+
switch (num) {
|
|
1345
|
+
case 0:
|
|
1346
|
+
return "Sunday";
|
|
1347
|
+
case 1:
|
|
1348
|
+
return "Monday";
|
|
1349
|
+
case 2:
|
|
1350
|
+
return "Tuesday";
|
|
1351
|
+
case 3:
|
|
1352
|
+
return "Wednesday";
|
|
1353
|
+
case 4:
|
|
1354
|
+
return "Thursday";
|
|
1355
|
+
case 5:
|
|
1356
|
+
return "Friday";
|
|
1357
|
+
case 6:
|
|
1358
|
+
return "Saturday";
|
|
1359
|
+
default:
|
|
1360
|
+
return "Unknown";
|
|
1361
|
+
}
|
|
1362
|
+
}
|
|
1363
|
+
function doy(date2) {
|
|
1364
|
+
const start = /* @__PURE__ */ new Date(`${date2.getFullYear()}-01-01 0:00:00`);
|
|
1365
|
+
return Math.ceil((date2.getTime() - start.getTime()) / (1e3 * 60 * 60 * 24));
|
|
1366
|
+
}
|
|
1367
|
+
function month(num) {
|
|
1368
|
+
switch (num) {
|
|
1369
|
+
case 0:
|
|
1370
|
+
return "January";
|
|
1371
|
+
case 1:
|
|
1372
|
+
return "February";
|
|
1373
|
+
case 2:
|
|
1374
|
+
return "March";
|
|
1375
|
+
case 3:
|
|
1376
|
+
return "April";
|
|
1377
|
+
case 4:
|
|
1378
|
+
return "May";
|
|
1379
|
+
case 5:
|
|
1380
|
+
return "June";
|
|
1381
|
+
case 6:
|
|
1382
|
+
return "July";
|
|
1383
|
+
case 7:
|
|
1384
|
+
return "August";
|
|
1385
|
+
case 8:
|
|
1386
|
+
return "September";
|
|
1387
|
+
case 9:
|
|
1388
|
+
return "October";
|
|
1389
|
+
case 10:
|
|
1390
|
+
return "November";
|
|
1391
|
+
case 11:
|
|
1392
|
+
return "December";
|
|
1393
|
+
default:
|
|
1394
|
+
return "Unknown";
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
function suffix(num) {
|
|
1398
|
+
let n = num.toString();
|
|
1399
|
+
switch (n.at(-1)) {
|
|
1400
|
+
case "1":
|
|
1401
|
+
return num + "st";
|
|
1402
|
+
case "2":
|
|
1403
|
+
return num + "nd";
|
|
1404
|
+
case "3":
|
|
1405
|
+
return num + "rd";
|
|
1406
|
+
default:
|
|
1407
|
+
return num + "th";
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
function tzOffset(offset) {
|
|
1411
|
+
const hours = ~~(offset / 60);
|
|
1412
|
+
const minutes = offset % 60;
|
|
1413
|
+
return (offset > 0 ? "-" : "") + `${hours}:${minutes.toString().padStart(2, "0")}`;
|
|
1414
|
+
}
|
|
1415
|
+
function timezone(offset) {
|
|
1416
|
+
const hours = offset / 60;
|
|
1417
|
+
switch (hours) {
|
|
1418
|
+
case -12:
|
|
1419
|
+
return "IDLW";
|
|
1420
|
+
case -11:
|
|
1421
|
+
return "SST";
|
|
1422
|
+
case -10:
|
|
1423
|
+
return "HST";
|
|
1424
|
+
case -9:
|
|
1425
|
+
return "AKST";
|
|
1426
|
+
case -8:
|
|
1427
|
+
return "PST";
|
|
1428
|
+
case -7:
|
|
1429
|
+
return "MST";
|
|
1430
|
+
case -6:
|
|
1431
|
+
return "CST";
|
|
1432
|
+
case -5:
|
|
1433
|
+
return "EST";
|
|
1434
|
+
case -4:
|
|
1435
|
+
return "AST";
|
|
1436
|
+
case -3:
|
|
1437
|
+
return "ART";
|
|
1438
|
+
case -2:
|
|
1439
|
+
return "FNT";
|
|
1440
|
+
case -1:
|
|
1441
|
+
return "AZOT";
|
|
1442
|
+
case 0:
|
|
1443
|
+
return "UTC";
|
|
1444
|
+
case 1:
|
|
1445
|
+
return "CET";
|
|
1446
|
+
case 2:
|
|
1447
|
+
return "EET";
|
|
1448
|
+
case 3:
|
|
1449
|
+
return "MSK";
|
|
1450
|
+
case 4:
|
|
1451
|
+
return "SAMT";
|
|
1452
|
+
case 5:
|
|
1453
|
+
return "YEKT";
|
|
1454
|
+
case 6:
|
|
1455
|
+
return "OMST";
|
|
1456
|
+
case 7:
|
|
1457
|
+
return "KRAT";
|
|
1458
|
+
case 8:
|
|
1459
|
+
return "CST";
|
|
1460
|
+
case 9:
|
|
1461
|
+
return "JST";
|
|
1462
|
+
case 10:
|
|
1463
|
+
return "AEST";
|
|
1464
|
+
case 11:
|
|
1465
|
+
return "SBT";
|
|
1466
|
+
case 12:
|
|
1467
|
+
return "NZST";
|
|
1468
|
+
default:
|
|
1469
|
+
return "";
|
|
1470
|
+
}
|
|
1471
|
+
}
|
|
1472
|
+
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()));
|
|
1345
1473
|
}
|
|
1346
1474
|
function sleep(ms) {
|
|
1347
1475
|
return new Promise((res) => setTimeout(res, ms));
|
|
@@ -1352,7 +1480,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
1352
1480
|
function timeUntil(date) {
|
|
1353
1481
|
return (date instanceof Date ? date.getTime() : date) - (/* @__PURE__ */ new Date()).getTime();
|
|
1354
1482
|
}
|
|
1355
|
-
function
|
|
1483
|
+
function typeKeys() {
|
|
1356
1484
|
return Object.keys({});
|
|
1357
1485
|
}
|
|
1358
1486
|
exports2.ASet = ASet;
|
|
@@ -1427,9 +1555,10 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
1427
1555
|
exports2.sleep = sleep;
|
|
1428
1556
|
exports2.sleepWhile = sleepWhile;
|
|
1429
1557
|
exports2.sortByProp = sortByProp;
|
|
1558
|
+
exports2.strSplice = strSplice;
|
|
1430
1559
|
exports2.timeUntil = timeUntil;
|
|
1431
1560
|
exports2.timestampFilename = timestampFilename;
|
|
1432
|
-
exports2.
|
|
1561
|
+
exports2.typeKeys = typeKeys;
|
|
1433
1562
|
exports2.uploadWithProgress = uploadWithProgress;
|
|
1434
1563
|
exports2.validateEmail = validateEmail;
|
|
1435
1564
|
Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
|