@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.mjs CHANGED
@@ -13,7 +13,11 @@ function clean(obj, undefinedOnly = false) {
13
13
  return obj;
14
14
  }
15
15
  function deepCopy(value) {
16
- return structuredClone(value);
16
+ try {
17
+ return structuredClone(value);
18
+ } catch {
19
+ return JSON.parse(JSONSanitize(value));
20
+ }
17
21
  }
18
22
  function deepMerge(target, ...sources) {
19
23
  sources.forEach((s) => {
@@ -102,12 +106,7 @@ function JSONAttemptParse(json) {
102
106
  }
103
107
  }
104
108
  function JSONSanitize(obj, space) {
105
- let cache = [];
106
109
  return JSON.stringify(obj, (key, value) => {
107
- if (typeof value === "object" && value !== null) {
108
- if (cache.includes(value)) return;
109
- cache.push(value);
110
- }
111
110
  return value;
112
111
  }, space);
113
112
  }
@@ -957,7 +956,7 @@ function formatBytes(bytes, decimals = 2) {
957
956
  function formatPhoneNumber(number) {
958
957
  const parts = /(\+?1)?.*?(\d{3}).*?(\d{3}).*?(\d{4})/g.exec(number);
959
958
  if (!parts) throw new Error(`Number cannot be parsed: ${number}`);
960
- return `${parts[1] ?? ""} (${parts[2]}) ${parts[3]}-${parts[4]}`.trim();
959
+ return `${parts[1] ? "+1" : ""} (${parts[2]}) ${parts[3]}-${parts[4]}`.trim();
961
960
  }
962
961
  function insertAt(target, str, index) {
963
962
  return `${target.slice(0, index)}${str}${target.slice(index + 1)}`;
@@ -992,6 +991,11 @@ function randomStringBuilder(length, letters = false, numbers = false, symbols =
992
991
  return c;
993
992
  }).join("");
994
993
  }
994
+ function strSplice(str, start, deleteCount, insert = "") {
995
+ const before = str.slice(0, start);
996
+ const after = str.slice(start + deleteCount);
997
+ return before + insert + after;
998
+ }
995
999
  function matchAll(value, regex) {
996
1000
  if (typeof regex === "string") {
997
1001
  regex = new RegExp(regex, "g");
@@ -1330,14 +1334,138 @@ function adjustedInterval(cb, ms) {
1330
1334
  if (timeout) clearTimeout(timeout);
1331
1335
  };
1332
1336
  }
1333
- function formatDate(date) {
1337
+ function formatDate(date, format = "YYYY-MM-DD H:mm ") {
1334
1338
  if (typeof date == "number" || typeof date == "string") date = new Date(date);
1335
- let hours = date.getHours(), postfix = "AM";
1336
- if (hours >= 12) {
1337
- if (hours > 12) hours -= 12;
1338
- postfix = "PM";
1339
- } else if (hours == 0) hours = 12;
1340
- return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, "0")}-${date.getDate().toString().padStart(2, "0")}, ${hours}:${date.getMinutes().toString().padStart(2, "0")} ${postfix}`;
1339
+ function day(num) {
1340
+ switch (num) {
1341
+ case 0:
1342
+ return "Sunday";
1343
+ case 1:
1344
+ return "Monday";
1345
+ case 2:
1346
+ return "Tuesday";
1347
+ case 3:
1348
+ return "Wednesday";
1349
+ case 4:
1350
+ return "Thursday";
1351
+ case 5:
1352
+ return "Friday";
1353
+ case 6:
1354
+ return "Saturday";
1355
+ default:
1356
+ return "Unknown";
1357
+ }
1358
+ }
1359
+ function doy(date2) {
1360
+ const start = /* @__PURE__ */ new Date(`${date2.getFullYear()}-01-01 0:00:00`);
1361
+ return Math.ceil((date2.getTime() - start.getTime()) / (1e3 * 60 * 60 * 24));
1362
+ }
1363
+ function month(num) {
1364
+ switch (num) {
1365
+ case 0:
1366
+ return "January";
1367
+ case 1:
1368
+ return "February";
1369
+ case 2:
1370
+ return "March";
1371
+ case 3:
1372
+ return "April";
1373
+ case 4:
1374
+ return "May";
1375
+ case 5:
1376
+ return "June";
1377
+ case 6:
1378
+ return "July";
1379
+ case 7:
1380
+ return "August";
1381
+ case 8:
1382
+ return "September";
1383
+ case 9:
1384
+ return "October";
1385
+ case 10:
1386
+ return "November";
1387
+ case 11:
1388
+ return "December";
1389
+ default:
1390
+ return "Unknown";
1391
+ }
1392
+ }
1393
+ function suffix(num) {
1394
+ let n = num.toString();
1395
+ switch (n.at(-1)) {
1396
+ case "1":
1397
+ return num + "st";
1398
+ case "2":
1399
+ return num + "nd";
1400
+ case "3":
1401
+ return num + "rd";
1402
+ default:
1403
+ return num + "th";
1404
+ }
1405
+ }
1406
+ function tzOffset(offset) {
1407
+ const hours = ~~(offset / 60);
1408
+ const minutes = offset % 60;
1409
+ return (offset > 0 ? "-" : "") + `${hours}:${minutes.toString().padStart(2, "0")}`;
1410
+ }
1411
+ function timezone(offset) {
1412
+ const hours = offset / 60;
1413
+ switch (hours) {
1414
+ case -12:
1415
+ return "IDLW";
1416
+ case -11:
1417
+ return "SST";
1418
+ case -10:
1419
+ return "HST";
1420
+ case -9:
1421
+ return "AKST";
1422
+ case -8:
1423
+ return "PST";
1424
+ case -7:
1425
+ return "MST";
1426
+ case -6:
1427
+ return "CST";
1428
+ case -5:
1429
+ return "EST";
1430
+ case -4:
1431
+ return "AST";
1432
+ case -3:
1433
+ return "ART";
1434
+ case -2:
1435
+ return "FNT";
1436
+ case -1:
1437
+ return "AZOT";
1438
+ case 0:
1439
+ return "UTC";
1440
+ case 1:
1441
+ return "CET";
1442
+ case 2:
1443
+ return "EET";
1444
+ case 3:
1445
+ return "MSK";
1446
+ case 4:
1447
+ return "SAMT";
1448
+ case 5:
1449
+ return "YEKT";
1450
+ case 6:
1451
+ return "OMST";
1452
+ case 7:
1453
+ return "KRAT";
1454
+ case 8:
1455
+ return "CST";
1456
+ case 9:
1457
+ return "JST";
1458
+ case 10:
1459
+ return "AEST";
1460
+ case 11:
1461
+ return "SBT";
1462
+ case 12:
1463
+ return "NZST";
1464
+ default:
1465
+ return "";
1466
+ }
1467
+ }
1468
+ 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()));
1341
1469
  }
1342
1470
  function sleep(ms) {
1343
1471
  return new Promise((res) => setTimeout(res, ms));
@@ -1348,7 +1476,7 @@ async function sleepWhile(fn, checkInterval = 100) {
1348
1476
  function timeUntil(date) {
1349
1477
  return (date instanceof Date ? date.getTime() : date) - (/* @__PURE__ */ new Date()).getTime();
1350
1478
  }
1351
- function tyoeKeys() {
1479
+ function typeKeys() {
1352
1480
  return Object.keys({});
1353
1481
  }
1354
1482
  export {
@@ -1424,9 +1552,10 @@ export {
1424
1552
  sleep,
1425
1553
  sleepWhile,
1426
1554
  sortByProp,
1555
+ strSplice,
1427
1556
  timeUntil,
1428
1557
  timestampFilename,
1429
- tyoeKeys,
1558
+ typeKeys,
1430
1559
  uploadWithProgress,
1431
1560
  validateEmail
1432
1561
  };