@ztimson/utils 0.21.6 → 0.22.0

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
@@ -992,6 +992,11 @@ function randomStringBuilder(length, letters = false, numbers = false, symbols =
992
992
  return c;
993
993
  }).join("");
994
994
  }
995
+ function strSplice(str, start, deleteCount, insert = "") {
996
+ const before = str.slice(0, start);
997
+ const after = str.slice(start + deleteCount);
998
+ return before + insert + after;
999
+ }
995
1000
  function matchAll(value, regex) {
996
1001
  if (typeof regex === "string") {
997
1002
  regex = new RegExp(regex, "g");
@@ -1330,14 +1335,138 @@ function adjustedInterval(cb, ms) {
1330
1335
  if (timeout) clearTimeout(timeout);
1331
1336
  };
1332
1337
  }
1333
- function formatDate(date) {
1338
+ function formatDate(date, format = "YYYY-MM-DD H:mm ") {
1334
1339
  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}`;
1340
+ function day(num) {
1341
+ switch (num) {
1342
+ case 0:
1343
+ return "Sunday";
1344
+ case 1:
1345
+ return "Monday";
1346
+ case 2:
1347
+ return "Tuesday";
1348
+ case 3:
1349
+ return "Wednesday";
1350
+ case 4:
1351
+ return "Thursday";
1352
+ case 5:
1353
+ return "Friday";
1354
+ case 6:
1355
+ return "Saturday";
1356
+ default:
1357
+ return "Unknown";
1358
+ }
1359
+ }
1360
+ function doy(date2) {
1361
+ const start = /* @__PURE__ */ new Date(`${date2.getFullYear()}-01-01 0:00:00`);
1362
+ return Math.ceil((date2.getTime() - start.getTime()) / (1e3 * 60 * 60 * 24));
1363
+ }
1364
+ function month(num) {
1365
+ switch (num) {
1366
+ case 0:
1367
+ return "January";
1368
+ case 1:
1369
+ return "February";
1370
+ case 2:
1371
+ return "March";
1372
+ case 3:
1373
+ return "April";
1374
+ case 4:
1375
+ return "May";
1376
+ case 5:
1377
+ return "June";
1378
+ case 6:
1379
+ return "July";
1380
+ case 7:
1381
+ return "August";
1382
+ case 8:
1383
+ return "September";
1384
+ case 9:
1385
+ return "October";
1386
+ case 10:
1387
+ return "November";
1388
+ case 11:
1389
+ return "December";
1390
+ default:
1391
+ return "Unknown";
1392
+ }
1393
+ }
1394
+ function suffix(num) {
1395
+ let n = num.toString();
1396
+ switch (n.at(-1)) {
1397
+ case "1":
1398
+ return num + "st";
1399
+ case "2":
1400
+ return num + "nd";
1401
+ case "3":
1402
+ return num + "rd";
1403
+ default:
1404
+ return num + "th";
1405
+ }
1406
+ }
1407
+ function tzOffset(offset) {
1408
+ const hours = ~~(offset / 60);
1409
+ const minutes = offset % 60;
1410
+ return (offset > 0 ? "-" : "") + `${hours}:${minutes.toString().padStart(2, "0")}`;
1411
+ }
1412
+ function timezone(offset) {
1413
+ const hours = offset / 60;
1414
+ switch (hours) {
1415
+ case -12:
1416
+ return "IDLW";
1417
+ case -11:
1418
+ return "SST";
1419
+ case -10:
1420
+ return "HST";
1421
+ case -9:
1422
+ return "AKST";
1423
+ case -8:
1424
+ return "PST";
1425
+ case -7:
1426
+ return "MST";
1427
+ case -6:
1428
+ return "CST";
1429
+ case -5:
1430
+ return "EST";
1431
+ case -4:
1432
+ return "AST";
1433
+ case -3:
1434
+ return "ART";
1435
+ case -2:
1436
+ return "FNT";
1437
+ case -1:
1438
+ return "AZOT";
1439
+ case 0:
1440
+ return "UTC";
1441
+ case 1:
1442
+ return "CET";
1443
+ case 2:
1444
+ return "EET";
1445
+ case 3:
1446
+ return "MSK";
1447
+ case 4:
1448
+ return "SAMT";
1449
+ case 5:
1450
+ return "YEKT";
1451
+ case 6:
1452
+ return "OMST";
1453
+ case 7:
1454
+ return "KRAT";
1455
+ case 8:
1456
+ return "CST";
1457
+ case 9:
1458
+ return "JST";
1459
+ case 10:
1460
+ return "AEST";
1461
+ case 11:
1462
+ return "SBT";
1463
+ case 12:
1464
+ return "NZST";
1465
+ default:
1466
+ return "";
1467
+ }
1468
+ }
1469
+ 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
1470
  }
1342
1471
  function sleep(ms) {
1343
1472
  return new Promise((res) => setTimeout(res, ms));
@@ -1424,6 +1553,7 @@ export {
1424
1553
  sleep,
1425
1554
  sleepWhile,
1426
1555
  sortByProp,
1556
+ strSplice,
1427
1557
  timeUntil,
1428
1558
  timestampFilename,
1429
1559
  tyoeKeys,