@ztimson/utils 0.24.12 → 0.25.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/cache.d.ts +4 -4
- package/dist/color.d.ts +1 -1
- package/dist/index.cjs +89 -83
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +89 -83
- package/dist/index.mjs.map +1 -1
- package/dist/jwt.d.ts +9 -1
- package/dist/math.d.ts +2 -1
- package/dist/misc.d.ts +8 -0
- package/dist/search.d.ts +9 -0
- package/dist/types.d.ts +1 -21
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -61,7 +61,7 @@ class ArgParser {
|
|
|
61
61
|
extras.push(arg);
|
|
62
62
|
continue;
|
|
63
63
|
}
|
|
64
|
-
const value = argDef.default === false ? true : argDef.default === true ? false : queue.splice(queue.findIndex((q) => q[0] != "-"), 1)[0] || argDef.default;
|
|
64
|
+
const value = combined[1] != null ? combined[1] : argDef.default === false ? true : argDef.default === true ? false : queue.splice(queue.findIndex((q) => q[0] != "-"), 1)[0] || argDef.default;
|
|
65
65
|
if (value == null) parsed["_error"].push(`Option missing value: ${argDef.name || combined[0]}`);
|
|
66
66
|
parsed[argDef.name] = value;
|
|
67
67
|
} else {
|
|
@@ -120,7 +120,7 @@ ${opts.message || this.desc}`;
|
|
|
120
120
|
function clean(obj, undefinedOnly = false) {
|
|
121
121
|
if (obj == null) throw new Error("Cannot clean a NULL value");
|
|
122
122
|
if (Array.isArray(obj)) {
|
|
123
|
-
obj = obj.filter((o) => o != null);
|
|
123
|
+
obj = obj.filter((o) => undefinedOnly ? o !== void 0 : o != null);
|
|
124
124
|
} else {
|
|
125
125
|
Object.entries(obj).forEach(([key, value]) => {
|
|
126
126
|
if (undefinedOnly && value === void 0 || !undefinedOnly && value == null) delete obj[key];
|
|
@@ -171,7 +171,6 @@ function flattenObj(obj, parent, result = {}) {
|
|
|
171
171
|
for (const key of Object.keys(obj)) {
|
|
172
172
|
const propName = parent ? `${parent}.${key}` : key;
|
|
173
173
|
if (typeof obj[key] === "object" && obj[key] != null && !Array.isArray(obj[key])) {
|
|
174
|
-
console.log(propName);
|
|
175
174
|
flattenObj(obj[key], propName, result);
|
|
176
175
|
} else {
|
|
177
176
|
result[propName] = obj[key];
|
|
@@ -227,55 +226,15 @@ function JSONSerialize(obj) {
|
|
|
227
226
|
return obj;
|
|
228
227
|
}
|
|
229
228
|
function JSONSanitize(obj, space) {
|
|
229
|
+
const cache = [];
|
|
230
230
|
return JSON.stringify(obj, (key, value) => {
|
|
231
|
+
if (typeof value === "object" && value !== null) {
|
|
232
|
+
if (cache.includes(value)) return "[Circular]";
|
|
233
|
+
cache.push(value);
|
|
234
|
+
}
|
|
231
235
|
return value;
|
|
232
236
|
}, space);
|
|
233
237
|
}
|
|
234
|
-
function addUnique(array, el) {
|
|
235
|
-
if (array.indexOf(el) === -1) array.push(el);
|
|
236
|
-
return array;
|
|
237
|
-
}
|
|
238
|
-
function arrayDiff(a, b) {
|
|
239
|
-
return makeUnique([
|
|
240
|
-
...a.filter((v1) => !b.includes((v2) => isEqual(v1, v2))),
|
|
241
|
-
...b.filter((v1) => !a.includes((v2) => isEqual(v1, v2)))
|
|
242
|
-
]);
|
|
243
|
-
}
|
|
244
|
-
function caseInsensitiveSort(prop) {
|
|
245
|
-
return function(a, b) {
|
|
246
|
-
const aVal = dotNotation(a, prop);
|
|
247
|
-
const bVal = dotNotation(b, prop);
|
|
248
|
-
if (typeof aVal !== "string" || typeof bVal !== "string") return 1;
|
|
249
|
-
return aVal.toLowerCase().localeCompare(bVal.toLowerCase());
|
|
250
|
-
};
|
|
251
|
-
}
|
|
252
|
-
function findByProp(prop, value) {
|
|
253
|
-
return (v) => isEqual(dotNotation(v, prop), value);
|
|
254
|
-
}
|
|
255
|
-
function flattenArr(arr, result = []) {
|
|
256
|
-
arr.forEach((el) => Array.isArray(el) ? flattenArr(el, result) : result.push(el));
|
|
257
|
-
return result;
|
|
258
|
-
}
|
|
259
|
-
function sortByProp(prop, reverse = false) {
|
|
260
|
-
return function(a, b) {
|
|
261
|
-
const aVal = dotNotation(a, prop);
|
|
262
|
-
const bVal = dotNotation(b, prop);
|
|
263
|
-
if (typeof aVal == "number" && typeof bVal == "number")
|
|
264
|
-
return (reverse ? -1 : 1) * (aVal - bVal);
|
|
265
|
-
if (aVal > bVal) return reverse ? -1 : 1;
|
|
266
|
-
if (aVal < bVal) return reverse ? 1 : -1;
|
|
267
|
-
return 0;
|
|
268
|
-
};
|
|
269
|
-
}
|
|
270
|
-
function makeUnique(arr) {
|
|
271
|
-
for (let i = arr.length - 1; i >= 0; i--) {
|
|
272
|
-
if (arr.slice(0, i).find((n) => isEqual(n, arr[i]))) arr.splice(i, 1);
|
|
273
|
-
}
|
|
274
|
-
return arr;
|
|
275
|
-
}
|
|
276
|
-
function makeArray(value) {
|
|
277
|
-
return Array.isArray(value) ? value : [value];
|
|
278
|
-
}
|
|
279
238
|
class ASet extends Array {
|
|
280
239
|
/** Number of elements in set */
|
|
281
240
|
get size() {
|
|
@@ -391,6 +350,48 @@ class ASet extends Array {
|
|
|
391
350
|
return new ASet([...this, ...set]);
|
|
392
351
|
}
|
|
393
352
|
}
|
|
353
|
+
function addUnique(array, el) {
|
|
354
|
+
if (array.indexOf(el) === -1) array.push(el);
|
|
355
|
+
return array;
|
|
356
|
+
}
|
|
357
|
+
function arrayDiff(a, b) {
|
|
358
|
+
return new ASet(a).symmetricDifference(new ASet(b));
|
|
359
|
+
}
|
|
360
|
+
function caseInsensitiveSort(prop) {
|
|
361
|
+
return function(a, b) {
|
|
362
|
+
const aVal = dotNotation(a, prop);
|
|
363
|
+
const bVal = dotNotation(b, prop);
|
|
364
|
+
if (typeof aVal !== "string" || typeof bVal !== "string") return 1;
|
|
365
|
+
return aVal.toLowerCase().localeCompare(bVal.toLowerCase());
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
function findByProp(prop, value) {
|
|
369
|
+
return (v) => isEqual(dotNotation(v, prop), value);
|
|
370
|
+
}
|
|
371
|
+
function flattenArr(arr, result = []) {
|
|
372
|
+
arr.forEach((el) => Array.isArray(el) ? flattenArr(el, result) : result.push(el));
|
|
373
|
+
return result;
|
|
374
|
+
}
|
|
375
|
+
function sortByProp(prop, reverse = false) {
|
|
376
|
+
return function(a, b) {
|
|
377
|
+
const aVal = dotNotation(a, prop);
|
|
378
|
+
const bVal = dotNotation(b, prop);
|
|
379
|
+
if (typeof aVal == "number" && typeof bVal == "number")
|
|
380
|
+
return (reverse ? -1 : 1) * (aVal - bVal);
|
|
381
|
+
if (aVal > bVal) return reverse ? -1 : 1;
|
|
382
|
+
if (aVal < bVal) return reverse ? 1 : -1;
|
|
383
|
+
return 0;
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
function makeUnique(arr) {
|
|
387
|
+
for (let i = arr.length - 1; i >= 0; i--) {
|
|
388
|
+
if (arr.slice(0, i).find((n) => isEqual(n, arr[i]))) arr.splice(i, 1);
|
|
389
|
+
}
|
|
390
|
+
return arr;
|
|
391
|
+
}
|
|
392
|
+
function makeArray(value) {
|
|
393
|
+
return Array.isArray(value) ? value : [value];
|
|
394
|
+
}
|
|
394
395
|
class Cache {
|
|
395
396
|
/**
|
|
396
397
|
* Create new cache
|
|
@@ -475,6 +476,7 @@ class Cache {
|
|
|
475
476
|
clear() {
|
|
476
477
|
this.complete = false;
|
|
477
478
|
this.store = {};
|
|
479
|
+
return this;
|
|
478
480
|
}
|
|
479
481
|
/**
|
|
480
482
|
* Delete an item from the cache
|
|
@@ -483,6 +485,7 @@ class Cache {
|
|
|
483
485
|
delete(key) {
|
|
484
486
|
delete this.store[key];
|
|
485
487
|
this.save();
|
|
488
|
+
return this;
|
|
486
489
|
}
|
|
487
490
|
/**
|
|
488
491
|
* Return cache as an array of key-value pairs
|
|
@@ -499,6 +502,7 @@ class Cache {
|
|
|
499
502
|
this.complete = false;
|
|
500
503
|
if (this.options.expiryPolicy == "keep") this.store[key]._expired = true;
|
|
501
504
|
else this.delete(key);
|
|
505
|
+
return this;
|
|
502
506
|
}
|
|
503
507
|
/**
|
|
504
508
|
* Get item from the cache
|
|
@@ -542,14 +546,14 @@ class Cache {
|
|
|
542
546
|
if (ttl) setTimeout(() => {
|
|
543
547
|
this.expire(key);
|
|
544
548
|
this.save();
|
|
545
|
-
}, ttl * 1e3);
|
|
549
|
+
}, (ttl || 0) * 1e3);
|
|
546
550
|
return this;
|
|
547
551
|
}
|
|
548
552
|
}
|
|
549
|
-
function
|
|
550
|
-
const exploded = background == null ? void 0 : background.match(background.length >= 6 ?
|
|
551
|
-
if (!exploded) return "black";
|
|
552
|
-
const [r, g, b] = exploded.map((hex) => parseInt(hex, 16));
|
|
553
|
+
function contrast(background) {
|
|
554
|
+
const exploded = background == null ? void 0 : background.match(background.length >= 6 ? /[0-9a-fA-F]{2}/g : /[0-9a-fA-F]/g);
|
|
555
|
+
if (!exploded || (exploded == null ? void 0 : exploded.length) < 3) return "black";
|
|
556
|
+
const [r, g, b] = exploded.map((hex) => parseInt(hex.length == 1 ? `${hex}${hex}` : hex, 16));
|
|
553
557
|
const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
|
|
554
558
|
return luminance > 0.5 ? "black" : "white";
|
|
555
559
|
}
|
|
@@ -559,7 +563,7 @@ const SYMBOL_LIST = "~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/";
|
|
|
559
563
|
const CHAR_LIST = LETTER_LIST + LETTER_LIST.toLowerCase() + NUMBER_LIST + SYMBOL_LIST;
|
|
560
564
|
function camelCase(str) {
|
|
561
565
|
const text = pascalCase(str);
|
|
562
|
-
return text[0].toLowerCase() + text.slice(1);
|
|
566
|
+
return !text ? "" : text[0].toLowerCase() + text.slice(1);
|
|
563
567
|
}
|
|
564
568
|
function formatBytes(bytes, decimals = 2) {
|
|
565
569
|
if (bytes === 0) return "0 Bytes";
|
|
@@ -585,12 +589,9 @@ function pad(text, length, char = " ", start = true) {
|
|
|
585
589
|
return text.toString().padEnd(length, char);
|
|
586
590
|
}
|
|
587
591
|
function pascalCase(str) {
|
|
592
|
+
var _a;
|
|
588
593
|
if (!str) return "";
|
|
589
|
-
|
|
590
|
-
var _a;
|
|
591
|
-
return ((_a = args[1]) == null ? void 0 : _a.toUpperCase()) || "";
|
|
592
|
-
});
|
|
593
|
-
return text[0].toUpperCase() + text.slice(1);
|
|
594
|
+
return ((_a = str.match(/[a-zA-Z0-9]+/g)) == null ? void 0 : _a.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join("")) ?? "";
|
|
594
595
|
}
|
|
595
596
|
function randomHex(length) {
|
|
596
597
|
return Array(length).fill(null).map(() => Math.round(Math.random() * 15).toString(16)).join("");
|
|
@@ -1012,7 +1013,7 @@ class TypedEmitter {
|
|
|
1012
1013
|
}
|
|
1013
1014
|
static off(event, listener) {
|
|
1014
1015
|
const e = event.toString();
|
|
1015
|
-
this.listeners[e] = (this.listeners[e] || []).filter((l) => l
|
|
1016
|
+
this.listeners[e] = (this.listeners[e] || []).filter((l) => l != listener);
|
|
1016
1017
|
}
|
|
1017
1018
|
static on(event, listener) {
|
|
1018
1019
|
var _a;
|
|
@@ -1035,7 +1036,7 @@ class TypedEmitter {
|
|
|
1035
1036
|
(this.listeners[event] || []).forEach((l) => l(...args));
|
|
1036
1037
|
}
|
|
1037
1038
|
off(event, listener) {
|
|
1038
|
-
this.listeners[event] = (this.listeners[event] || []).filter((l) => l
|
|
1039
|
+
this.listeners[event] = (this.listeners[event] || []).filter((l) => l != listener);
|
|
1039
1040
|
}
|
|
1040
1041
|
on(event, listener) {
|
|
1041
1042
|
var _a;
|
|
@@ -1332,6 +1333,11 @@ const _Http = class _Http {
|
|
|
1332
1333
|
__publicField(_Http, "interceptors", {});
|
|
1333
1334
|
__publicField(_Http, "headers", {});
|
|
1334
1335
|
let Http = _Http;
|
|
1336
|
+
function createJwt(payload, signature = "unsigned") {
|
|
1337
|
+
const header = Buffer.from(JSON.stringify({ alg: "HS256", typ: "JWT" })).toString("base64url");
|
|
1338
|
+
const body = Buffer.from(JSON.stringify(payload)).toString("base64url");
|
|
1339
|
+
return `${header}.${body}.${signature}`;
|
|
1340
|
+
}
|
|
1335
1341
|
function decodeJwt(token) {
|
|
1336
1342
|
const base64 = token.split(".")[1].replace(/-/g, "+").replace(/_/g, "/");
|
|
1337
1343
|
return JSONAttemptParse(decodeURIComponent(atob(base64).split("").map(function(c) {
|
|
@@ -1427,20 +1433,22 @@ const _Logger = class _Logger extends TypedEmitter {
|
|
|
1427
1433
|
};
|
|
1428
1434
|
__publicField(_Logger, "LOG_LEVEL", 4);
|
|
1429
1435
|
let Logger = _Logger;
|
|
1430
|
-
function dec2Frac(num) {
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
};
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1436
|
+
function dec2Frac(num, maxDen = 1e3) {
|
|
1437
|
+
let sign = Math.sign(num);
|
|
1438
|
+
num = Math.abs(num);
|
|
1439
|
+
if (Number.isInteger(num)) return sign * num + "";
|
|
1440
|
+
let closest = { n: 0, d: 1, diff: Math.abs(num) };
|
|
1441
|
+
for (let d = 1; d <= maxDen; d++) {
|
|
1442
|
+
let n = Math.round(num * d);
|
|
1443
|
+
let diff = Math.abs(num - n / d);
|
|
1444
|
+
if (diff < closest.diff) {
|
|
1445
|
+
closest = { n, d, diff };
|
|
1446
|
+
if (diff < 1e-8) break;
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
let integer = Math.floor(closest.n / closest.d);
|
|
1450
|
+
let numerator = closest.n - integer * closest.d;
|
|
1451
|
+
return (sign < 0 ? "-" : "") + (integer ? integer + " " : "") + (numerator ? numerator + "/" + closest.d : "");
|
|
1444
1452
|
}
|
|
1445
1453
|
function fracToDec(frac) {
|
|
1446
1454
|
let split = frac.split(" ");
|
|
@@ -1560,6 +1568,7 @@ class PathEvent {
|
|
|
1560
1568
|
const l1 = p1.fullPath.length, l2 = p2.fullPath.length;
|
|
1561
1569
|
return l1 < l2 ? 1 : l1 > l2 ? -1 : 0;
|
|
1562
1570
|
}).reduce((acc, p) => {
|
|
1571
|
+
if (acc && !acc.fullPath.startsWith(p.fullPath)) return acc;
|
|
1563
1572
|
if (p.none) hitNone = true;
|
|
1564
1573
|
if (!acc) return p;
|
|
1565
1574
|
if (hitNone) return acc;
|
|
@@ -1667,15 +1676,15 @@ class PathEventEmitter {
|
|
|
1667
1676
|
this.prefix = prefix;
|
|
1668
1677
|
}
|
|
1669
1678
|
emit(event, ...args) {
|
|
1670
|
-
const parsed = new PathEvent(`${this.prefix}/${
|
|
1671
|
-
this.listeners.filter((l) => PathEvent.has(l[0], event)).forEach(async (l) => l[1](parsed, ...args));
|
|
1679
|
+
const parsed = new PathEvent(`${this.prefix}/${new PathEvent(event).toString()}`);
|
|
1680
|
+
this.listeners.filter((l) => PathEvent.has(l[0], `${this.prefix}/${event}`)).forEach(async (l) => l[1](parsed, ...args));
|
|
1672
1681
|
}
|
|
1673
1682
|
off(listener) {
|
|
1674
1683
|
this.listeners = this.listeners.filter((l) => l[1] != listener);
|
|
1675
1684
|
}
|
|
1676
1685
|
on(event, listener) {
|
|
1677
1686
|
makeArray(event).forEach((e) => this.listeners.push([
|
|
1678
|
-
new PathEvent(`${this.prefix}/${
|
|
1687
|
+
new PathEvent(`${this.prefix}/${new PathEvent(e).toString()}`),
|
|
1679
1688
|
listener
|
|
1680
1689
|
]));
|
|
1681
1690
|
return () => this.off(listener);
|
|
@@ -1746,9 +1755,6 @@ function logicTest(target, condition) {
|
|
|
1746
1755
|
}).length == and.length;
|
|
1747
1756
|
});
|
|
1748
1757
|
}
|
|
1749
|
-
function typeKeys() {
|
|
1750
|
-
return Object.keys({});
|
|
1751
|
-
}
|
|
1752
1758
|
var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
|
|
1753
1759
|
var dist = {};
|
|
1754
1760
|
var persist$1 = {};
|
|
@@ -1959,10 +1965,11 @@ export {
|
|
|
1959
1965
|
addUnique,
|
|
1960
1966
|
adjustedInterval,
|
|
1961
1967
|
arrayDiff,
|
|
1962
|
-
blackOrWhite,
|
|
1963
1968
|
camelCase,
|
|
1964
1969
|
caseInsensitiveSort,
|
|
1965
1970
|
clean,
|
|
1971
|
+
contrast,
|
|
1972
|
+
createJwt,
|
|
1966
1973
|
dec2Frac,
|
|
1967
1974
|
decodeJwt,
|
|
1968
1975
|
deepCopy,
|
|
@@ -2012,7 +2019,6 @@ export {
|
|
|
2012
2019
|
timeUntil,
|
|
2013
2020
|
timestampFilename,
|
|
2014
2021
|
toCsv,
|
|
2015
|
-
typeKeys,
|
|
2016
2022
|
uploadWithProgress,
|
|
2017
2023
|
validateEmail
|
|
2018
2024
|
};
|