@ztimson/utils 0.24.11 → 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 +90 -84
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +90 -84
- 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/cache.d.ts
CHANGED
|
@@ -52,12 +52,12 @@ export declare class Cache<K extends string | number | symbol, T> {
|
|
|
52
52
|
/**
|
|
53
53
|
* Remove all keys from cache
|
|
54
54
|
*/
|
|
55
|
-
clear():
|
|
55
|
+
clear(): this;
|
|
56
56
|
/**
|
|
57
57
|
* Delete an item from the cache
|
|
58
58
|
* @param {K} key Item's primary key
|
|
59
59
|
*/
|
|
60
|
-
delete(key: K):
|
|
60
|
+
delete(key: K): this;
|
|
61
61
|
/**
|
|
62
62
|
* Return cache as an array of key-value pairs
|
|
63
63
|
* @return {[K, T][]} Key-value pairs array
|
|
@@ -67,13 +67,13 @@ export declare class Cache<K extends string | number | symbol, T> {
|
|
|
67
67
|
* Manually expire a cached item
|
|
68
68
|
* @param {K} key Key to expire
|
|
69
69
|
*/
|
|
70
|
-
expire(key: K):
|
|
70
|
+
expire(key: K): this;
|
|
71
71
|
/**
|
|
72
72
|
* Get item from the cache
|
|
73
73
|
* @param {K} key Key to lookup
|
|
74
74
|
* @return {T} Cached item
|
|
75
75
|
*/
|
|
76
|
-
get(key: K, expired?: boolean): T | null;
|
|
76
|
+
get(key: K, expired?: boolean): CachedValue<T> | null;
|
|
77
77
|
/**
|
|
78
78
|
* Get a list of cached keys
|
|
79
79
|
* @return {K[]} Array of keys
|
package/dist/color.d.ts
CHANGED
|
@@ -3,4 +3,4 @@
|
|
|
3
3
|
* @param {string} background Color to compare against
|
|
4
4
|
* @return {"white" | "black"} Color with the most contrast
|
|
5
5
|
*/
|
|
6
|
-
export declare function
|
|
6
|
+
export declare function contrast(background: string): 'white' | 'black';
|
package/dist/index.cjs
CHANGED
|
@@ -65,7 +65,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
65
65
|
extras.push(arg);
|
|
66
66
|
continue;
|
|
67
67
|
}
|
|
68
|
-
const value = argDef.default === false ? true : argDef.default === true ? false : queue.splice(queue.findIndex((q) => q[0] != "-"), 1)[0] || argDef.default;
|
|
68
|
+
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;
|
|
69
69
|
if (value == null) parsed["_error"].push(`Option missing value: ${argDef.name || combined[0]}`);
|
|
70
70
|
parsed[argDef.name] = value;
|
|
71
71
|
} else {
|
|
@@ -124,7 +124,7 @@ ${opts.message || this.desc}`;
|
|
|
124
124
|
function clean(obj, undefinedOnly = false) {
|
|
125
125
|
if (obj == null) throw new Error("Cannot clean a NULL value");
|
|
126
126
|
if (Array.isArray(obj)) {
|
|
127
|
-
obj = obj.filter((o) => o != null);
|
|
127
|
+
obj = obj.filter((o) => undefinedOnly ? o !== void 0 : o != null);
|
|
128
128
|
} else {
|
|
129
129
|
Object.entries(obj).forEach(([key, value]) => {
|
|
130
130
|
if (undefinedOnly && value === void 0 || !undefinedOnly && value == null) delete obj[key];
|
|
@@ -175,7 +175,6 @@ ${opts.message || this.desc}`;
|
|
|
175
175
|
for (const key of Object.keys(obj)) {
|
|
176
176
|
const propName = parent ? `${parent}.${key}` : key;
|
|
177
177
|
if (typeof obj[key] === "object" && obj[key] != null && !Array.isArray(obj[key])) {
|
|
178
|
-
console.log(propName);
|
|
179
178
|
flattenObj(obj[key], propName, result);
|
|
180
179
|
} else {
|
|
181
180
|
result[propName] = obj[key];
|
|
@@ -231,55 +230,15 @@ ${opts.message || this.desc}`;
|
|
|
231
230
|
return obj;
|
|
232
231
|
}
|
|
233
232
|
function JSONSanitize(obj, space) {
|
|
233
|
+
const cache = [];
|
|
234
234
|
return JSON.stringify(obj, (key, value) => {
|
|
235
|
+
if (typeof value === "object" && value !== null) {
|
|
236
|
+
if (cache.includes(value)) return "[Circular]";
|
|
237
|
+
cache.push(value);
|
|
238
|
+
}
|
|
235
239
|
return value;
|
|
236
240
|
}, space);
|
|
237
241
|
}
|
|
238
|
-
function addUnique(array, el) {
|
|
239
|
-
if (array.indexOf(el) === -1) array.push(el);
|
|
240
|
-
return array;
|
|
241
|
-
}
|
|
242
|
-
function arrayDiff(a, b) {
|
|
243
|
-
return makeUnique([
|
|
244
|
-
...a.filter((v1) => !b.includes((v2) => isEqual(v1, v2))),
|
|
245
|
-
...b.filter((v1) => !a.includes((v2) => isEqual(v1, v2)))
|
|
246
|
-
]);
|
|
247
|
-
}
|
|
248
|
-
function caseInsensitiveSort(prop) {
|
|
249
|
-
return function(a, b) {
|
|
250
|
-
const aVal = dotNotation(a, prop);
|
|
251
|
-
const bVal = dotNotation(b, prop);
|
|
252
|
-
if (typeof aVal !== "string" || typeof bVal !== "string") return 1;
|
|
253
|
-
return aVal.toLowerCase().localeCompare(bVal.toLowerCase());
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
function findByProp(prop, value) {
|
|
257
|
-
return (v) => isEqual(dotNotation(v, prop), value);
|
|
258
|
-
}
|
|
259
|
-
function flattenArr(arr, result = []) {
|
|
260
|
-
arr.forEach((el) => Array.isArray(el) ? flattenArr(el, result) : result.push(el));
|
|
261
|
-
return result;
|
|
262
|
-
}
|
|
263
|
-
function sortByProp(prop, reverse = false) {
|
|
264
|
-
return function(a, b) {
|
|
265
|
-
const aVal = dotNotation(a, prop);
|
|
266
|
-
const bVal = dotNotation(b, prop);
|
|
267
|
-
if (typeof aVal == "number" && typeof bVal == "number")
|
|
268
|
-
return (reverse ? -1 : 1) * (aVal - bVal);
|
|
269
|
-
if (aVal > bVal) return reverse ? -1 : 1;
|
|
270
|
-
if (aVal < bVal) return reverse ? 1 : -1;
|
|
271
|
-
return 0;
|
|
272
|
-
};
|
|
273
|
-
}
|
|
274
|
-
function makeUnique(arr) {
|
|
275
|
-
for (let i = arr.length - 1; i >= 0; i--) {
|
|
276
|
-
if (arr.slice(0, i).find((n) => isEqual(n, arr[i]))) arr.splice(i, 1);
|
|
277
|
-
}
|
|
278
|
-
return arr;
|
|
279
|
-
}
|
|
280
|
-
function makeArray(value) {
|
|
281
|
-
return Array.isArray(value) ? value : [value];
|
|
282
|
-
}
|
|
283
242
|
class ASet extends Array {
|
|
284
243
|
/** Number of elements in set */
|
|
285
244
|
get size() {
|
|
@@ -395,6 +354,48 @@ ${opts.message || this.desc}`;
|
|
|
395
354
|
return new ASet([...this, ...set]);
|
|
396
355
|
}
|
|
397
356
|
}
|
|
357
|
+
function addUnique(array, el) {
|
|
358
|
+
if (array.indexOf(el) === -1) array.push(el);
|
|
359
|
+
return array;
|
|
360
|
+
}
|
|
361
|
+
function arrayDiff(a, b) {
|
|
362
|
+
return new ASet(a).symmetricDifference(new ASet(b));
|
|
363
|
+
}
|
|
364
|
+
function caseInsensitiveSort(prop) {
|
|
365
|
+
return function(a, b) {
|
|
366
|
+
const aVal = dotNotation(a, prop);
|
|
367
|
+
const bVal = dotNotation(b, prop);
|
|
368
|
+
if (typeof aVal !== "string" || typeof bVal !== "string") return 1;
|
|
369
|
+
return aVal.toLowerCase().localeCompare(bVal.toLowerCase());
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
function findByProp(prop, value) {
|
|
373
|
+
return (v) => isEqual(dotNotation(v, prop), value);
|
|
374
|
+
}
|
|
375
|
+
function flattenArr(arr, result = []) {
|
|
376
|
+
arr.forEach((el) => Array.isArray(el) ? flattenArr(el, result) : result.push(el));
|
|
377
|
+
return result;
|
|
378
|
+
}
|
|
379
|
+
function sortByProp(prop, reverse = false) {
|
|
380
|
+
return function(a, b) {
|
|
381
|
+
const aVal = dotNotation(a, prop);
|
|
382
|
+
const bVal = dotNotation(b, prop);
|
|
383
|
+
if (typeof aVal == "number" && typeof bVal == "number")
|
|
384
|
+
return (reverse ? -1 : 1) * (aVal - bVal);
|
|
385
|
+
if (aVal > bVal) return reverse ? -1 : 1;
|
|
386
|
+
if (aVal < bVal) return reverse ? 1 : -1;
|
|
387
|
+
return 0;
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
function makeUnique(arr) {
|
|
391
|
+
for (let i = arr.length - 1; i >= 0; i--) {
|
|
392
|
+
if (arr.slice(0, i).find((n) => isEqual(n, arr[i]))) arr.splice(i, 1);
|
|
393
|
+
}
|
|
394
|
+
return arr;
|
|
395
|
+
}
|
|
396
|
+
function makeArray(value) {
|
|
397
|
+
return Array.isArray(value) ? value : [value];
|
|
398
|
+
}
|
|
398
399
|
class Cache {
|
|
399
400
|
/**
|
|
400
401
|
* Create new cache
|
|
@@ -479,6 +480,7 @@ ${opts.message || this.desc}`;
|
|
|
479
480
|
clear() {
|
|
480
481
|
this.complete = false;
|
|
481
482
|
this.store = {};
|
|
483
|
+
return this;
|
|
482
484
|
}
|
|
483
485
|
/**
|
|
484
486
|
* Delete an item from the cache
|
|
@@ -487,6 +489,7 @@ ${opts.message || this.desc}`;
|
|
|
487
489
|
delete(key) {
|
|
488
490
|
delete this.store[key];
|
|
489
491
|
this.save();
|
|
492
|
+
return this;
|
|
490
493
|
}
|
|
491
494
|
/**
|
|
492
495
|
* Return cache as an array of key-value pairs
|
|
@@ -503,6 +506,7 @@ ${opts.message || this.desc}`;
|
|
|
503
506
|
this.complete = false;
|
|
504
507
|
if (this.options.expiryPolicy == "keep") this.store[key]._expired = true;
|
|
505
508
|
else this.delete(key);
|
|
509
|
+
return this;
|
|
506
510
|
}
|
|
507
511
|
/**
|
|
508
512
|
* Get item from the cache
|
|
@@ -540,20 +544,20 @@ ${opts.message || this.desc}`;
|
|
|
540
544
|
* @return {this}
|
|
541
545
|
*/
|
|
542
546
|
set(key, value, ttl = this.options.ttl) {
|
|
543
|
-
if (this.options.expiryPolicy == "keep") delete
|
|
547
|
+
if (this.options.expiryPolicy == "keep") delete value._expired;
|
|
544
548
|
this.store[key] = value;
|
|
545
549
|
this.save();
|
|
546
550
|
if (ttl) setTimeout(() => {
|
|
547
551
|
this.expire(key);
|
|
548
552
|
this.save();
|
|
549
|
-
}, ttl * 1e3);
|
|
553
|
+
}, (ttl || 0) * 1e3);
|
|
550
554
|
return this;
|
|
551
555
|
}
|
|
552
556
|
}
|
|
553
|
-
function
|
|
554
|
-
const exploded = background == null ? void 0 : background.match(background.length >= 6 ?
|
|
555
|
-
if (!exploded) return "black";
|
|
556
|
-
const [r, g, b] = exploded.map((hex) => parseInt(hex, 16));
|
|
557
|
+
function contrast(background) {
|
|
558
|
+
const exploded = background == null ? void 0 : background.match(background.length >= 6 ? /[0-9a-fA-F]{2}/g : /[0-9a-fA-F]/g);
|
|
559
|
+
if (!exploded || (exploded == null ? void 0 : exploded.length) < 3) return "black";
|
|
560
|
+
const [r, g, b] = exploded.map((hex) => parseInt(hex.length == 1 ? `${hex}${hex}` : hex, 16));
|
|
557
561
|
const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
|
|
558
562
|
return luminance > 0.5 ? "black" : "white";
|
|
559
563
|
}
|
|
@@ -563,7 +567,7 @@ ${opts.message || this.desc}`;
|
|
|
563
567
|
const CHAR_LIST = LETTER_LIST + LETTER_LIST.toLowerCase() + NUMBER_LIST + SYMBOL_LIST;
|
|
564
568
|
function camelCase(str) {
|
|
565
569
|
const text = pascalCase(str);
|
|
566
|
-
return text[0].toLowerCase() + text.slice(1);
|
|
570
|
+
return !text ? "" : text[0].toLowerCase() + text.slice(1);
|
|
567
571
|
}
|
|
568
572
|
function formatBytes(bytes, decimals = 2) {
|
|
569
573
|
if (bytes === 0) return "0 Bytes";
|
|
@@ -589,12 +593,9 @@ ${opts.message || this.desc}`;
|
|
|
589
593
|
return text.toString().padEnd(length, char);
|
|
590
594
|
}
|
|
591
595
|
function pascalCase(str) {
|
|
596
|
+
var _a;
|
|
592
597
|
if (!str) return "";
|
|
593
|
-
|
|
594
|
-
var _a;
|
|
595
|
-
return ((_a = args[1]) == null ? void 0 : _a.toUpperCase()) || "";
|
|
596
|
-
});
|
|
597
|
-
return text[0].toUpperCase() + text.slice(1);
|
|
598
|
+
return ((_a = str.match(/[a-zA-Z0-9]+/g)) == null ? void 0 : _a.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join("")) ?? "";
|
|
598
599
|
}
|
|
599
600
|
function randomHex(length) {
|
|
600
601
|
return Array(length).fill(null).map(() => Math.round(Math.random() * 15).toString(16)).join("");
|
|
@@ -1016,7 +1017,7 @@ ${opts.message || this.desc}`;
|
|
|
1016
1017
|
}
|
|
1017
1018
|
static off(event, listener) {
|
|
1018
1019
|
const e = event.toString();
|
|
1019
|
-
this.listeners[e] = (this.listeners[e] || []).filter((l) => l
|
|
1020
|
+
this.listeners[e] = (this.listeners[e] || []).filter((l) => l != listener);
|
|
1020
1021
|
}
|
|
1021
1022
|
static on(event, listener) {
|
|
1022
1023
|
var _a;
|
|
@@ -1039,7 +1040,7 @@ ${opts.message || this.desc}`;
|
|
|
1039
1040
|
(this.listeners[event] || []).forEach((l) => l(...args));
|
|
1040
1041
|
}
|
|
1041
1042
|
off(event, listener) {
|
|
1042
|
-
this.listeners[event] = (this.listeners[event] || []).filter((l) => l
|
|
1043
|
+
this.listeners[event] = (this.listeners[event] || []).filter((l) => l != listener);
|
|
1043
1044
|
}
|
|
1044
1045
|
on(event, listener) {
|
|
1045
1046
|
var _a;
|
|
@@ -1336,6 +1337,11 @@ ${opts.message || this.desc}`;
|
|
|
1336
1337
|
__publicField(_Http, "interceptors", {});
|
|
1337
1338
|
__publicField(_Http, "headers", {});
|
|
1338
1339
|
let Http = _Http;
|
|
1340
|
+
function createJwt(payload, signature = "unsigned") {
|
|
1341
|
+
const header = Buffer.from(JSON.stringify({ alg: "HS256", typ: "JWT" })).toString("base64url");
|
|
1342
|
+
const body = Buffer.from(JSON.stringify(payload)).toString("base64url");
|
|
1343
|
+
return `${header}.${body}.${signature}`;
|
|
1344
|
+
}
|
|
1339
1345
|
function decodeJwt(token) {
|
|
1340
1346
|
const base64 = token.split(".")[1].replace(/-/g, "+").replace(/_/g, "/");
|
|
1341
1347
|
return JSONAttemptParse(decodeURIComponent(atob(base64).split("").map(function(c) {
|
|
@@ -1431,20 +1437,22 @@ ${opts.message || this.desc}`;
|
|
|
1431
1437
|
};
|
|
1432
1438
|
__publicField(_Logger, "LOG_LEVEL", 4);
|
|
1433
1439
|
let Logger = _Logger;
|
|
1434
|
-
function dec2Frac(num) {
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
};
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1440
|
+
function dec2Frac(num, maxDen = 1e3) {
|
|
1441
|
+
let sign = Math.sign(num);
|
|
1442
|
+
num = Math.abs(num);
|
|
1443
|
+
if (Number.isInteger(num)) return sign * num + "";
|
|
1444
|
+
let closest = { n: 0, d: 1, diff: Math.abs(num) };
|
|
1445
|
+
for (let d = 1; d <= maxDen; d++) {
|
|
1446
|
+
let n = Math.round(num * d);
|
|
1447
|
+
let diff = Math.abs(num - n / d);
|
|
1448
|
+
if (diff < closest.diff) {
|
|
1449
|
+
closest = { n, d, diff };
|
|
1450
|
+
if (diff < 1e-8) break;
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
let integer = Math.floor(closest.n / closest.d);
|
|
1454
|
+
let numerator = closest.n - integer * closest.d;
|
|
1455
|
+
return (sign < 0 ? "-" : "") + (integer ? integer + " " : "") + (numerator ? numerator + "/" + closest.d : "");
|
|
1448
1456
|
}
|
|
1449
1457
|
function fracToDec(frac) {
|
|
1450
1458
|
let split = frac.split(" ");
|
|
@@ -1564,6 +1572,7 @@ ${opts.message || this.desc}`;
|
|
|
1564
1572
|
const l1 = p1.fullPath.length, l2 = p2.fullPath.length;
|
|
1565
1573
|
return l1 < l2 ? 1 : l1 > l2 ? -1 : 0;
|
|
1566
1574
|
}).reduce((acc, p) => {
|
|
1575
|
+
if (acc && !acc.fullPath.startsWith(p.fullPath)) return acc;
|
|
1567
1576
|
if (p.none) hitNone = true;
|
|
1568
1577
|
if (!acc) return p;
|
|
1569
1578
|
if (hitNone) return acc;
|
|
@@ -1671,15 +1680,15 @@ ${opts.message || this.desc}`;
|
|
|
1671
1680
|
this.prefix = prefix;
|
|
1672
1681
|
}
|
|
1673
1682
|
emit(event, ...args) {
|
|
1674
|
-
const parsed = new PathEvent(`${this.prefix}/${
|
|
1675
|
-
this.listeners.filter((l) => PathEvent.has(l[0], event)).forEach(async (l) => l[1](parsed, ...args));
|
|
1683
|
+
const parsed = new PathEvent(`${this.prefix}/${new PathEvent(event).toString()}`);
|
|
1684
|
+
this.listeners.filter((l) => PathEvent.has(l[0], `${this.prefix}/${event}`)).forEach(async (l) => l[1](parsed, ...args));
|
|
1676
1685
|
}
|
|
1677
1686
|
off(listener) {
|
|
1678
1687
|
this.listeners = this.listeners.filter((l) => l[1] != listener);
|
|
1679
1688
|
}
|
|
1680
1689
|
on(event, listener) {
|
|
1681
1690
|
makeArray(event).forEach((e) => this.listeners.push([
|
|
1682
|
-
new PathEvent(`${this.prefix}/${
|
|
1691
|
+
new PathEvent(`${this.prefix}/${new PathEvent(e).toString()}`),
|
|
1683
1692
|
listener
|
|
1684
1693
|
]));
|
|
1685
1694
|
return () => this.off(listener);
|
|
@@ -1750,9 +1759,6 @@ ${opts.message || this.desc}`;
|
|
|
1750
1759
|
}).length == and.length;
|
|
1751
1760
|
});
|
|
1752
1761
|
}
|
|
1753
|
-
function typeKeys() {
|
|
1754
|
-
return Object.keys({});
|
|
1755
|
-
}
|
|
1756
1762
|
var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
|
|
1757
1763
|
var dist = {};
|
|
1758
1764
|
var persist$1 = {};
|
|
@@ -1962,10 +1968,11 @@ ${opts.message || this.desc}`;
|
|
|
1962
1968
|
exports2.addUnique = addUnique;
|
|
1963
1969
|
exports2.adjustedInterval = adjustedInterval;
|
|
1964
1970
|
exports2.arrayDiff = arrayDiff;
|
|
1965
|
-
exports2.blackOrWhite = blackOrWhite;
|
|
1966
1971
|
exports2.camelCase = camelCase;
|
|
1967
1972
|
exports2.caseInsensitiveSort = caseInsensitiveSort;
|
|
1968
1973
|
exports2.clean = clean;
|
|
1974
|
+
exports2.contrast = contrast;
|
|
1975
|
+
exports2.createJwt = createJwt;
|
|
1969
1976
|
exports2.dec2Frac = dec2Frac;
|
|
1970
1977
|
exports2.decodeJwt = decodeJwt;
|
|
1971
1978
|
exports2.deepCopy = deepCopy;
|
|
@@ -2015,7 +2022,6 @@ ${opts.message || this.desc}`;
|
|
|
2015
2022
|
exports2.timeUntil = timeUntil;
|
|
2016
2023
|
exports2.timestampFilename = timestampFilename;
|
|
2017
2024
|
exports2.toCsv = toCsv;
|
|
2018
|
-
exports2.typeKeys = typeKeys;
|
|
2019
2025
|
exports2.uploadWithProgress = uploadWithProgress;
|
|
2020
2026
|
exports2.validateEmail = validateEmail;
|
|
2021
2027
|
Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
|