@ztimson/utils 0.28.12 → 0.28.14
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/color.d.ts +15 -2
- package/dist/index.cjs +158 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +158 -31
- package/dist/index.mjs.map +1 -1
- package/dist/math.d.ts +2 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -665,13 +665,101 @@ class Cache {
|
|
|
665
665
|
/** Get all cached items */
|
|
666
666
|
values = this.all;
|
|
667
667
|
}
|
|
668
|
-
function
|
|
669
|
-
|
|
668
|
+
function dec2Frac(num, maxDen = 1e3) {
|
|
669
|
+
let sign = Math.sign(num);
|
|
670
|
+
num = Math.abs(num);
|
|
671
|
+
if (Number.isInteger(num)) return sign * num + "";
|
|
672
|
+
let closest = { n: 0, d: 1, diff: Math.abs(num) };
|
|
673
|
+
for (let d = 1; d <= maxDen; d++) {
|
|
674
|
+
let n = Math.round(num * d);
|
|
675
|
+
let diff = Math.abs(num - n / d);
|
|
676
|
+
if (diff < closest.diff) {
|
|
677
|
+
closest = { n, d, diff };
|
|
678
|
+
if (diff < 1e-8) break;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
let integer = Math.floor(closest.n / closest.d);
|
|
682
|
+
let numerator = closest.n - integer * closest.d;
|
|
683
|
+
return (sign < 0 ? "-" : "") + (integer ? integer + " " : "") + (numerator ? numerator + "/" + closest.d : "");
|
|
684
|
+
}
|
|
685
|
+
function dec2Hex(num) {
|
|
686
|
+
const hex = Math.round(num * 255).toString(16);
|
|
687
|
+
return hex.length === 1 ? "0" + hex : hex;
|
|
688
|
+
}
|
|
689
|
+
function frac2Dec(frac) {
|
|
690
|
+
let split = frac.split(" ");
|
|
691
|
+
const whole = split.length == 2 ? Number(split[0]) : 0;
|
|
692
|
+
split = split.pop().split("/");
|
|
693
|
+
return whole + Number(split[0]) / Number(split[1]);
|
|
694
|
+
}
|
|
695
|
+
function numSuffix(n) {
|
|
696
|
+
const s = ["th", "st", "nd", "rd"], v = n % 100;
|
|
697
|
+
return `${n}${s[(v - 20) % 10] || s[v] || s[0]}`;
|
|
698
|
+
}
|
|
699
|
+
function contrast(color) {
|
|
700
|
+
const exploded = color?.match(color.length >= 6 ? /[0-9a-fA-F]{2}/g : /[0-9a-fA-F]/g);
|
|
670
701
|
if (!exploded || exploded?.length < 3) return "black";
|
|
671
702
|
const [r, g, b] = exploded.map((hex) => parseInt(hex.length == 1 ? `${hex}${hex}` : hex, 16));
|
|
672
703
|
const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
|
|
673
704
|
return luminance > 0.5 ? "black" : "white";
|
|
674
705
|
}
|
|
706
|
+
function hex2Int(hex) {
|
|
707
|
+
let r = 0, g = 0, b = 0;
|
|
708
|
+
if (hex.length === 4) {
|
|
709
|
+
r = parseInt(hex[1] + hex[1], 16);
|
|
710
|
+
g = parseInt(hex[2] + hex[2], 16);
|
|
711
|
+
b = parseInt(hex[3] + hex[3], 16);
|
|
712
|
+
} else {
|
|
713
|
+
r = parseInt(hex.slice(1, 3), 16);
|
|
714
|
+
g = parseInt(hex.slice(3, 5), 16);
|
|
715
|
+
b = parseInt(hex.slice(5, 7), 16);
|
|
716
|
+
}
|
|
717
|
+
return { r, g, b };
|
|
718
|
+
}
|
|
719
|
+
function hue2rgb(p, q, t) {
|
|
720
|
+
if (t < 0) t += 1;
|
|
721
|
+
if (t > 1) t -= 1;
|
|
722
|
+
if (t < 1 / 6) return p + (q - p) * 6 * t;
|
|
723
|
+
if (t < 1 / 2) return q;
|
|
724
|
+
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
|
|
725
|
+
return p;
|
|
726
|
+
}
|
|
727
|
+
function int2Hex(r, g, b) {
|
|
728
|
+
return "#" + dec2Hex(r) + dec2Hex(g) + dec2Hex(b);
|
|
729
|
+
}
|
|
730
|
+
function shadeColor(hex, amount) {
|
|
731
|
+
let { r, g, b } = hex2Int(hex);
|
|
732
|
+
r /= 255;
|
|
733
|
+
g /= 255;
|
|
734
|
+
b /= 255;
|
|
735
|
+
const max = Math.max(r, g, b), min = Math.min(r, g, b);
|
|
736
|
+
let h, s, l = (max + min) / 2;
|
|
737
|
+
if (max === min) {
|
|
738
|
+
h = s = 0;
|
|
739
|
+
} else {
|
|
740
|
+
const d = max - min;
|
|
741
|
+
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
742
|
+
switch (max) {
|
|
743
|
+
case r:
|
|
744
|
+
h = (g - b) / d + (g < b ? 6 : 0);
|
|
745
|
+
break;
|
|
746
|
+
case g:
|
|
747
|
+
h = (b - r) / d + 2;
|
|
748
|
+
break;
|
|
749
|
+
case b:
|
|
750
|
+
h = (r - g) / d + 4;
|
|
751
|
+
break;
|
|
752
|
+
default:
|
|
753
|
+
h = 0;
|
|
754
|
+
break;
|
|
755
|
+
}
|
|
756
|
+
h /= 6;
|
|
757
|
+
}
|
|
758
|
+
l = Math.max(0, Math.min(1, l + amount));
|
|
759
|
+
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
|
760
|
+
const p = 2 * l - q;
|
|
761
|
+
return int2Hex(hue2rgb(p, q, h + 1 / 3), hue2rgb(p, q, h), hue2rgb(p, q, h - 1 / 3));
|
|
762
|
+
}
|
|
675
763
|
const LETTER_LIST = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
676
764
|
const NUMBER_LIST = "0123456789";
|
|
677
765
|
const SYMBOL_LIST = "~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/";
|
|
@@ -1798,33 +1886,6 @@ class Logger extends TypedEmitter {
|
|
|
1798
1886
|
console.error(CliForeground.RED + str + CliEffects.CLEAR);
|
|
1799
1887
|
}
|
|
1800
1888
|
}
|
|
1801
|
-
function dec2Frac(num, maxDen = 1e3) {
|
|
1802
|
-
let sign = Math.sign(num);
|
|
1803
|
-
num = Math.abs(num);
|
|
1804
|
-
if (Number.isInteger(num)) return sign * num + "";
|
|
1805
|
-
let closest = { n: 0, d: 1, diff: Math.abs(num) };
|
|
1806
|
-
for (let d = 1; d <= maxDen; d++) {
|
|
1807
|
-
let n = Math.round(num * d);
|
|
1808
|
-
let diff = Math.abs(num - n / d);
|
|
1809
|
-
if (diff < closest.diff) {
|
|
1810
|
-
closest = { n, d, diff };
|
|
1811
|
-
if (diff < 1e-8) break;
|
|
1812
|
-
}
|
|
1813
|
-
}
|
|
1814
|
-
let integer = Math.floor(closest.n / closest.d);
|
|
1815
|
-
let numerator = closest.n - integer * closest.d;
|
|
1816
|
-
return (sign < 0 ? "-" : "") + (integer ? integer + " " : "") + (numerator ? numerator + "/" + closest.d : "");
|
|
1817
|
-
}
|
|
1818
|
-
function fracToDec(frac) {
|
|
1819
|
-
let split = frac.split(" ");
|
|
1820
|
-
const whole = split.length == 2 ? Number(split[0]) : 0;
|
|
1821
|
-
split = split.pop().split("/");
|
|
1822
|
-
return whole + Number(split[0]) / Number(split[1]);
|
|
1823
|
-
}
|
|
1824
|
-
function numSuffix(n) {
|
|
1825
|
-
const s = ["th", "st", "nd", "rd"], v = n % 100;
|
|
1826
|
-
return `${n}${s[(v - 20) % 10] || s[v] || s[0]}`;
|
|
1827
|
-
}
|
|
1828
1889
|
function compareVersions(target, vs) {
|
|
1829
1890
|
const [tMajor, tMinor, tPatch] = target.split(".").map((v) => +v.replace(/[^0-9]/g, ""));
|
|
1830
1891
|
const [vMajor, vMinor, vPatch] = vs.split(".").map((v) => +v.replace(/[^0-9]/g, ""));
|
|
@@ -2322,7 +2383,68 @@ class TemplateError extends BadRequestError {
|
|
|
2322
2383
|
function findTemplateVars(html) {
|
|
2323
2384
|
const variables = /* @__PURE__ */ new Set();
|
|
2324
2385
|
const arrays = /* @__PURE__ */ new Set();
|
|
2325
|
-
const excluded = /* @__PURE__ */ new Set([
|
|
2386
|
+
const excluded = /* @__PURE__ */ new Set([
|
|
2387
|
+
"let",
|
|
2388
|
+
"const",
|
|
2389
|
+
"var",
|
|
2390
|
+
"function",
|
|
2391
|
+
"if",
|
|
2392
|
+
"while",
|
|
2393
|
+
"do",
|
|
2394
|
+
"this",
|
|
2395
|
+
"typeof",
|
|
2396
|
+
"new",
|
|
2397
|
+
"instanceof",
|
|
2398
|
+
"in",
|
|
2399
|
+
"for",
|
|
2400
|
+
"else",
|
|
2401
|
+
"case",
|
|
2402
|
+
"break",
|
|
2403
|
+
"continue",
|
|
2404
|
+
"switch",
|
|
2405
|
+
"default",
|
|
2406
|
+
"with",
|
|
2407
|
+
"eval",
|
|
2408
|
+
"arguments",
|
|
2409
|
+
"void",
|
|
2410
|
+
"delete",
|
|
2411
|
+
"null",
|
|
2412
|
+
"undefined",
|
|
2413
|
+
"true",
|
|
2414
|
+
"false",
|
|
2415
|
+
"async",
|
|
2416
|
+
"await",
|
|
2417
|
+
"try",
|
|
2418
|
+
"catch",
|
|
2419
|
+
"finally",
|
|
2420
|
+
"throw",
|
|
2421
|
+
"return",
|
|
2422
|
+
"yield",
|
|
2423
|
+
"debugger",
|
|
2424
|
+
"extends",
|
|
2425
|
+
"import",
|
|
2426
|
+
"export",
|
|
2427
|
+
"class",
|
|
2428
|
+
"super",
|
|
2429
|
+
"static",
|
|
2430
|
+
"get",
|
|
2431
|
+
"set",
|
|
2432
|
+
"constructor",
|
|
2433
|
+
"enum",
|
|
2434
|
+
"implements",
|
|
2435
|
+
"interface",
|
|
2436
|
+
"package",
|
|
2437
|
+
"private",
|
|
2438
|
+
"protected",
|
|
2439
|
+
"public",
|
|
2440
|
+
"abstract",
|
|
2441
|
+
"final",
|
|
2442
|
+
"native",
|
|
2443
|
+
"synchronized",
|
|
2444
|
+
"throws",
|
|
2445
|
+
"transient",
|
|
2446
|
+
"volatile"
|
|
2447
|
+
]);
|
|
2326
2448
|
for (const loop of matchAll(html, /\{\{\s*?\*\s*?(.+?)\s+in\s+(.+?)\s*?}}/g)) {
|
|
2327
2449
|
const [element, index = "index"] = loop[1].replaceAll(/[()\s]/g, "").split(",");
|
|
2328
2450
|
excluded.add(element);
|
|
@@ -2913,6 +3035,7 @@ export {
|
|
|
2913
3035
|
dayOfWeek,
|
|
2914
3036
|
dayOfYear,
|
|
2915
3037
|
dec2Frac,
|
|
3038
|
+
dec2Hex,
|
|
2916
3039
|
decodeJwt,
|
|
2917
3040
|
deepCopy,
|
|
2918
3041
|
deepMerge,
|
|
@@ -2934,12 +3057,15 @@ export {
|
|
|
2934
3057
|
formatDate,
|
|
2935
3058
|
formatMs,
|
|
2936
3059
|
formatPhoneNumber,
|
|
2937
|
-
|
|
3060
|
+
frac2Dec,
|
|
2938
3061
|
fromCsv,
|
|
2939
3062
|
gravatar,
|
|
3063
|
+
hex2Int,
|
|
3064
|
+
hue2rgb,
|
|
2940
3065
|
includes,
|
|
2941
3066
|
insertAt,
|
|
2942
3067
|
instantInterval,
|
|
3068
|
+
int2Hex,
|
|
2943
3069
|
ipV6ToV4,
|
|
2944
3070
|
isEqual,
|
|
2945
3071
|
kebabCase,
|
|
@@ -2963,6 +3089,7 @@ export {
|
|
|
2963
3089
|
renderTemplate,
|
|
2964
3090
|
reservedIp,
|
|
2965
3091
|
search,
|
|
3092
|
+
shadeColor,
|
|
2966
3093
|
sleep,
|
|
2967
3094
|
sleepWhile,
|
|
2968
3095
|
snakeCase,
|