@sv443-network/coreutils 3.3.0 → 3.5.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/CHANGELOG.md +21 -0
- package/README.md +14 -4
- package/dist/CoreUtils.cjs +216 -9
- package/dist/CoreUtils.min.cjs +4 -3
- package/dist/CoreUtils.min.mjs +4 -3
- package/dist/CoreUtils.min.umd.js +4 -3
- package/dist/CoreUtils.mjs +216 -9
- package/dist/CoreUtils.umd.js +798 -548
- package/dist/lib/DataStore.d.ts +2 -2
- package/dist/lib/DataStoreSerializer.d.ts +10 -6
- package/dist/lib/Debouncer.d.ts +3 -3
- package/dist/lib/misc.d.ts +36 -0
- package/dist/lib/text.d.ts +65 -0
- package/package.json +23 -23
package/dist/CoreUtils.mjs
CHANGED
|
@@ -368,12 +368,45 @@ function getCallStack(asArray, lines = Infinity) {
|
|
|
368
368
|
if (typeof lines !== "number" || isNaN(lines) || lines < 0)
|
|
369
369
|
throw new TypeError("lines parameter must be a non-negative number");
|
|
370
370
|
try {
|
|
371
|
-
throw new
|
|
371
|
+
throw new CustomError("GetCallStack", "Capturing a stack trace with CoreUtils.getCallStack(). If you see this anywhere, you can safely ignore it.");
|
|
372
372
|
} catch (err) {
|
|
373
373
|
const stack = (err.stack ?? "").split("\n").map((line) => line.trim()).slice(2, lines + 2);
|
|
374
374
|
return asArray !== false ? stack : stack.join("\n");
|
|
375
375
|
}
|
|
376
376
|
}
|
|
377
|
+
function createRecurringTask(options) {
|
|
378
|
+
var _a;
|
|
379
|
+
let iterations = 0;
|
|
380
|
+
let aborted = false;
|
|
381
|
+
(_a = options.signal) == null ? void 0 : _a.addEventListener("abort", () => {
|
|
382
|
+
aborted = true;
|
|
383
|
+
}, { once: true });
|
|
384
|
+
const runRecurringTask = async (initial = false) => {
|
|
385
|
+
var _a2;
|
|
386
|
+
if (aborted)
|
|
387
|
+
return;
|
|
388
|
+
try {
|
|
389
|
+
if ((options.immediate ?? true) || !initial) {
|
|
390
|
+
iterations++;
|
|
391
|
+
if (await ((_a2 = options.condition) == null ? void 0 : _a2.call(options, iterations - 1)) ?? true) {
|
|
392
|
+
const val = await options.task(iterations - 1);
|
|
393
|
+
if (options.onSuccess)
|
|
394
|
+
await options.onSuccess(val, iterations - 1);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
} catch (err) {
|
|
398
|
+
if (options.onError)
|
|
399
|
+
await options.onError(err, iterations - 1);
|
|
400
|
+
if (options.abortOnError)
|
|
401
|
+
aborted = true;
|
|
402
|
+
if (!options.onError && !options.abortOnError)
|
|
403
|
+
throw err;
|
|
404
|
+
}
|
|
405
|
+
if (!aborted && (typeof options.maxIterations !== "number" || iterations < options.maxIterations))
|
|
406
|
+
setTimeout(runRecurringTask, options.timeout);
|
|
407
|
+
};
|
|
408
|
+
return runRecurringTask(true);
|
|
409
|
+
}
|
|
377
410
|
|
|
378
411
|
// lib/text.ts
|
|
379
412
|
function autoPlural(term, num, pluralType = "auto") {
|
|
@@ -462,6 +495,173 @@ function truncStr(input, length, endStr = "...") {
|
|
|
462
495
|
const finalStr = str.length > length ? str.substring(0, length - endStr.length) + endStr : str;
|
|
463
496
|
return finalStr.length > length ? finalStr.substring(0, length) : finalStr;
|
|
464
497
|
}
|
|
498
|
+
var defaultTableLineCharset = {
|
|
499
|
+
single: {
|
|
500
|
+
horizontal: "\u2500",
|
|
501
|
+
vertical: "\u2502",
|
|
502
|
+
topLeft: "\u250C",
|
|
503
|
+
topRight: "\u2510",
|
|
504
|
+
bottomLeft: "\u2514",
|
|
505
|
+
bottomRight: "\u2518",
|
|
506
|
+
leftT: "\u251C",
|
|
507
|
+
rightT: "\u2524",
|
|
508
|
+
topT: "\u252C",
|
|
509
|
+
bottomT: "\u2534",
|
|
510
|
+
cross: "\u253C"
|
|
511
|
+
},
|
|
512
|
+
double: {
|
|
513
|
+
horizontal: "\u2550",
|
|
514
|
+
vertical: "\u2551",
|
|
515
|
+
topLeft: "\u2554",
|
|
516
|
+
topRight: "\u2557",
|
|
517
|
+
bottomLeft: "\u255A",
|
|
518
|
+
bottomRight: "\u255D",
|
|
519
|
+
leftT: "\u2560",
|
|
520
|
+
rightT: "\u2563",
|
|
521
|
+
topT: "\u2566",
|
|
522
|
+
bottomT: "\u2569",
|
|
523
|
+
cross: "\u256C"
|
|
524
|
+
},
|
|
525
|
+
none: {
|
|
526
|
+
horizontal: " ",
|
|
527
|
+
vertical: " ",
|
|
528
|
+
topLeft: " ",
|
|
529
|
+
topRight: " ",
|
|
530
|
+
bottomLeft: " ",
|
|
531
|
+
bottomRight: " ",
|
|
532
|
+
leftT: " ",
|
|
533
|
+
rightT: " ",
|
|
534
|
+
topT: " ",
|
|
535
|
+
bottomT: " ",
|
|
536
|
+
cross: " "
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
function createTable(rows, options) {
|
|
540
|
+
var _a;
|
|
541
|
+
const opts = {
|
|
542
|
+
columnAlign: "left",
|
|
543
|
+
truncateAbove: Infinity,
|
|
544
|
+
truncEndStr: "\u2026",
|
|
545
|
+
minPadding: 1,
|
|
546
|
+
lineStyle: "single",
|
|
547
|
+
applyCellStyle: () => void 0,
|
|
548
|
+
applyLineStyle: () => void 0,
|
|
549
|
+
lineCharset: defaultTableLineCharset,
|
|
550
|
+
...options ?? {}
|
|
551
|
+
};
|
|
552
|
+
const defRange = (val, min, max) => clamp(typeof val !== "number" || isNaN(Number(val)) ? min : val, min, max);
|
|
553
|
+
opts.truncateAbove = defRange(opts.truncateAbove, 0, Infinity);
|
|
554
|
+
opts.minPadding = defRange(opts.minPadding, 0, Infinity);
|
|
555
|
+
const lnCh = opts.lineCharset[opts.lineStyle];
|
|
556
|
+
const stripAnsi = (str) => str.replace(/\u001b\[[0-9;]*m/g, "");
|
|
557
|
+
const stringRows = rows.map((row) => row.map((cell) => String(cell)));
|
|
558
|
+
const colCount = ((_a = rows[0]) == null ? void 0 : _a.length) ?? 0;
|
|
559
|
+
if (colCount === 0 || stringRows.length === 0)
|
|
560
|
+
return "";
|
|
561
|
+
if (isFinite(opts.truncateAbove)) {
|
|
562
|
+
const truncAnsi = (str, maxVisible, endStr) => {
|
|
563
|
+
const limit = maxVisible - endStr.length;
|
|
564
|
+
if (limit <= 0)
|
|
565
|
+
return endStr.slice(0, maxVisible);
|
|
566
|
+
let visible = 0;
|
|
567
|
+
let result = "";
|
|
568
|
+
let i = 0;
|
|
569
|
+
let hasAnsi = false;
|
|
570
|
+
while (i < str.length) {
|
|
571
|
+
if (str[i] === "\x1B" && str[i + 1] === "[") {
|
|
572
|
+
const seqEnd = str.indexOf("m", i + 2);
|
|
573
|
+
if (seqEnd !== -1) {
|
|
574
|
+
result += str.slice(i, seqEnd + 1);
|
|
575
|
+
hasAnsi = true;
|
|
576
|
+
i = seqEnd + 1;
|
|
577
|
+
continue;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
if (visible === limit) {
|
|
581
|
+
result += endStr;
|
|
582
|
+
if (hasAnsi)
|
|
583
|
+
result += "\x1B[0m";
|
|
584
|
+
return result;
|
|
585
|
+
}
|
|
586
|
+
result += str[i];
|
|
587
|
+
visible++;
|
|
588
|
+
i++;
|
|
589
|
+
}
|
|
590
|
+
return result;
|
|
591
|
+
};
|
|
592
|
+
for (const row of stringRows)
|
|
593
|
+
for (let j = 0; j < row.length; j++)
|
|
594
|
+
if (stripAnsi(row[j] ?? "").length > opts.truncateAbove)
|
|
595
|
+
row[j] = truncAnsi(row[j] ?? "", opts.truncateAbove, opts.truncEndStr);
|
|
596
|
+
}
|
|
597
|
+
const colWidths = Array.from(
|
|
598
|
+
{ length: colCount },
|
|
599
|
+
(_, j) => Math.max(0, ...stringRows.map((row) => stripAnsi(row[j] ?? "").length))
|
|
600
|
+
);
|
|
601
|
+
const applyLn = (i, j, ch) => {
|
|
602
|
+
const [before = "", after = ""] = opts.applyLineStyle(i, j) ?? [];
|
|
603
|
+
return `${before}${ch}${after}`;
|
|
604
|
+
};
|
|
605
|
+
const buildBorderRow = (lineIdx, leftCh, midCh, rightCh) => {
|
|
606
|
+
let result = "";
|
|
607
|
+
let j = 0;
|
|
608
|
+
result += applyLn(lineIdx, j++, leftCh);
|
|
609
|
+
for (let col = 0; col < colCount; col++) {
|
|
610
|
+
const cellWidth = (colWidths[col] ?? 0) + opts.minPadding * 2;
|
|
611
|
+
for (let ci = 0; ci < cellWidth; ci++)
|
|
612
|
+
result += applyLn(lineIdx, j++, lnCh.horizontal);
|
|
613
|
+
if (col < colCount - 1)
|
|
614
|
+
result += applyLn(lineIdx, j++, midCh);
|
|
615
|
+
}
|
|
616
|
+
result += applyLn(lineIdx, j++, rightCh);
|
|
617
|
+
return result;
|
|
618
|
+
};
|
|
619
|
+
const lines = [];
|
|
620
|
+
for (let rowIdx = 0; rowIdx < stringRows.length; rowIdx++) {
|
|
621
|
+
const row = stringRows[rowIdx] ?? [];
|
|
622
|
+
const lineIdxBase = rowIdx * 3;
|
|
623
|
+
if (opts.lineStyle !== "none") {
|
|
624
|
+
lines.push(
|
|
625
|
+
rowIdx === 0 ? buildBorderRow(lineIdxBase, lnCh.topLeft, lnCh.topT, lnCh.topRight) : buildBorderRow(lineIdxBase, lnCh.leftT, lnCh.cross, lnCh.rightT)
|
|
626
|
+
);
|
|
627
|
+
}
|
|
628
|
+
let contentLine = "";
|
|
629
|
+
let j = 0;
|
|
630
|
+
contentLine += applyLn(lineIdxBase + 1, j++, lnCh.vertical);
|
|
631
|
+
for (let colIdx = 0; colIdx < colCount; colIdx++) {
|
|
632
|
+
const cell = row[colIdx] ?? "";
|
|
633
|
+
const visLen = stripAnsi(cell).length;
|
|
634
|
+
const extra = (colWidths[colIdx] ?? 0) - visLen;
|
|
635
|
+
const align = (Array.isArray(opts.columnAlign) ? opts.columnAlign[colIdx] : opts.columnAlign) ?? "left";
|
|
636
|
+
let leftPad;
|
|
637
|
+
let rightPad;
|
|
638
|
+
switch (align) {
|
|
639
|
+
case "right":
|
|
640
|
+
leftPad = opts.minPadding + extra;
|
|
641
|
+
rightPad = opts.minPadding;
|
|
642
|
+
break;
|
|
643
|
+
case "centerLeft":
|
|
644
|
+
leftPad = opts.minPadding + Math.floor(extra / 2);
|
|
645
|
+
rightPad = opts.minPadding + Math.ceil(extra / 2);
|
|
646
|
+
break;
|
|
647
|
+
case "centerRight":
|
|
648
|
+
leftPad = opts.minPadding + Math.ceil(extra / 2);
|
|
649
|
+
rightPad = opts.minPadding + Math.floor(extra / 2);
|
|
650
|
+
break;
|
|
651
|
+
default:
|
|
652
|
+
leftPad = opts.minPadding;
|
|
653
|
+
rightPad = opts.minPadding + extra;
|
|
654
|
+
}
|
|
655
|
+
const [cellBefore = "", cellAfter = ""] = opts.applyCellStyle(rowIdx, colIdx) ?? [];
|
|
656
|
+
contentLine += " ".repeat(leftPad) + cellBefore + cell + cellAfter + " ".repeat(rightPad);
|
|
657
|
+
contentLine += applyLn(lineIdxBase + 1, j++, lnCh.vertical);
|
|
658
|
+
}
|
|
659
|
+
lines.push(contentLine);
|
|
660
|
+
if (opts.lineStyle !== "none" && rowIdx === stringRows.length - 1)
|
|
661
|
+
lines.push(buildBorderRow(lineIdxBase + 2, lnCh.bottomLeft, lnCh.bottomT, lnCh.bottomRight));
|
|
662
|
+
}
|
|
663
|
+
return lines.join("\n");
|
|
664
|
+
}
|
|
465
665
|
|
|
466
666
|
// node_modules/.pnpm/nanoevents@9.1.0/node_modules/nanoevents/index.js
|
|
467
667
|
var createNanoEvents = () => ({
|
|
@@ -1172,6 +1372,7 @@ var FileStorageEngine = class extends DataStoreEngine {
|
|
|
1172
1372
|
// lib/DataStoreSerializer.ts
|
|
1173
1373
|
var DataStoreSerializer = class _DataStoreSerializer {
|
|
1174
1374
|
stores;
|
|
1375
|
+
// eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1175
1376
|
options;
|
|
1176
1377
|
constructor(stores, options = {}) {
|
|
1177
1378
|
if (!crypto || !crypto.subtle)
|
|
@@ -1184,7 +1385,10 @@ var DataStoreSerializer = class _DataStoreSerializer {
|
|
|
1184
1385
|
...options
|
|
1185
1386
|
};
|
|
1186
1387
|
}
|
|
1187
|
-
/**
|
|
1388
|
+
/**
|
|
1389
|
+
* Calculates the checksum of a string. Uses {@linkcode computeHash()} with SHA-256 and digests as a hex string by default.
|
|
1390
|
+
* Override this in a subclass if a custom checksum method is needed.
|
|
1391
|
+
*/
|
|
1188
1392
|
async calcChecksum(input) {
|
|
1189
1393
|
return computeHash(input, "SHA-256");
|
|
1190
1394
|
}
|
|
@@ -1315,8 +1519,8 @@ var Debouncer = class extends NanoEmitter {
|
|
|
1315
1519
|
* @param timeout Timeout in milliseconds between letting through calls - defaults to 200
|
|
1316
1520
|
* @param type The edge type to use for the debouncer - see {@linkcode DebouncerType} for details or [the documentation for an explanation and diagram](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#debouncer) - defaults to "immediate"
|
|
1317
1521
|
*/
|
|
1318
|
-
constructor(timeout = 200, type = "immediate") {
|
|
1319
|
-
super();
|
|
1522
|
+
constructor(timeout = 200, type = "immediate", nanoEmitterOptions) {
|
|
1523
|
+
super(nanoEmitterOptions);
|
|
1320
1524
|
this.timeout = timeout;
|
|
1321
1525
|
this.type = type;
|
|
1322
1526
|
}
|
|
@@ -1347,7 +1551,7 @@ var Debouncer = class extends NanoEmitter {
|
|
|
1347
1551
|
//#region timeout
|
|
1348
1552
|
/** Sets the timeout for the debouncer */
|
|
1349
1553
|
setTimeout(timeout) {
|
|
1350
|
-
this.emit("change", this.timeout = timeout, this.type);
|
|
1554
|
+
this.events.emit("change", this.timeout = timeout, this.type);
|
|
1351
1555
|
}
|
|
1352
1556
|
/** Returns the current timeout */
|
|
1353
1557
|
getTimeout() {
|
|
@@ -1360,7 +1564,7 @@ var Debouncer = class extends NanoEmitter {
|
|
|
1360
1564
|
//#region type
|
|
1361
1565
|
/** Sets the edge type for the debouncer */
|
|
1362
1566
|
setType(type) {
|
|
1363
|
-
this.emit("change", this.timeout, this.type = type);
|
|
1567
|
+
this.events.emit("change", this.timeout, this.type = type);
|
|
1364
1568
|
}
|
|
1365
1569
|
/** Returns the current edge type */
|
|
1366
1570
|
getType() {
|
|
@@ -1371,7 +1575,7 @@ var Debouncer = class extends NanoEmitter {
|
|
|
1371
1575
|
call(...args) {
|
|
1372
1576
|
const cl = (...a) => {
|
|
1373
1577
|
this.queuedCall = void 0;
|
|
1374
|
-
this.emit("call", ...a);
|
|
1578
|
+
this.events.emit("call", ...a);
|
|
1375
1579
|
this.listeners.forEach((l) => l.call(this, ...a));
|
|
1376
1580
|
};
|
|
1377
1581
|
const setRepeatTimeout = () => {
|
|
@@ -1404,8 +1608,8 @@ var Debouncer = class extends NanoEmitter {
|
|
|
1404
1608
|
}
|
|
1405
1609
|
}
|
|
1406
1610
|
};
|
|
1407
|
-
function debounce(fn, timeout = 200, type = "immediate") {
|
|
1408
|
-
const debouncer = new Debouncer(timeout, type);
|
|
1611
|
+
function debounce(fn, timeout = 200, type = "immediate", nanoEmitterOptions) {
|
|
1612
|
+
const debouncer = new Debouncer(timeout, type, nanoEmitterOptions);
|
|
1409
1613
|
debouncer.addListener(fn);
|
|
1410
1614
|
const func = ((...args) => debouncer.call(...args));
|
|
1411
1615
|
func.debouncer = debouncer;
|
|
@@ -1437,10 +1641,13 @@ export {
|
|
|
1437
1641
|
consumeGen,
|
|
1438
1642
|
consumeStringGen,
|
|
1439
1643
|
createProgressBar,
|
|
1644
|
+
createRecurringTask,
|
|
1645
|
+
createTable,
|
|
1440
1646
|
darkenColor,
|
|
1441
1647
|
debounce,
|
|
1442
1648
|
decompress,
|
|
1443
1649
|
defaultPbChars,
|
|
1650
|
+
defaultTableLineCharset,
|
|
1444
1651
|
digitCount,
|
|
1445
1652
|
fetchAdvanced,
|
|
1446
1653
|
formatNumber,
|