@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/color.d.ts
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Determine if either black or white provides more contrast to the provided color
|
|
3
|
-
* @param {string}
|
|
3
|
+
* @param {string} color Color to compare against
|
|
4
4
|
* @return {"white" | "black"} Color with the most contrast
|
|
5
5
|
*/
|
|
6
|
-
export declare function contrast(
|
|
6
|
+
export declare function contrast(color: string): 'white' | 'black';
|
|
7
|
+
export declare function hex2Int(hex: string): {
|
|
8
|
+
r: number;
|
|
9
|
+
g: number;
|
|
10
|
+
b: number;
|
|
11
|
+
};
|
|
12
|
+
export declare function hue2rgb(p: number, q: number, t: number): number;
|
|
13
|
+
export declare function int2Hex(r: number, g: number, b: number): string;
|
|
14
|
+
/**
|
|
15
|
+
* Adjusts the darkness of a hex color.
|
|
16
|
+
* @param {string} hex - The hex color (e.g., '#ff0000').
|
|
17
|
+
* @param {number} amount - A value between -1 (black) and 1 (white)
|
|
18
|
+
*/
|
|
19
|
+
export declare function shadeColor(hex: string, amount: number): string;
|
package/dist/index.cjs
CHANGED
|
@@ -669,13 +669,101 @@ ${opts.message || this.desc}`;
|
|
|
669
669
|
/** Get all cached items */
|
|
670
670
|
values = this.all;
|
|
671
671
|
}
|
|
672
|
-
function
|
|
673
|
-
|
|
672
|
+
function dec2Frac(num, maxDen = 1e3) {
|
|
673
|
+
let sign = Math.sign(num);
|
|
674
|
+
num = Math.abs(num);
|
|
675
|
+
if (Number.isInteger(num)) return sign * num + "";
|
|
676
|
+
let closest = { n: 0, d: 1, diff: Math.abs(num) };
|
|
677
|
+
for (let d = 1; d <= maxDen; d++) {
|
|
678
|
+
let n = Math.round(num * d);
|
|
679
|
+
let diff = Math.abs(num - n / d);
|
|
680
|
+
if (diff < closest.diff) {
|
|
681
|
+
closest = { n, d, diff };
|
|
682
|
+
if (diff < 1e-8) break;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
let integer = Math.floor(closest.n / closest.d);
|
|
686
|
+
let numerator = closest.n - integer * closest.d;
|
|
687
|
+
return (sign < 0 ? "-" : "") + (integer ? integer + " " : "") + (numerator ? numerator + "/" + closest.d : "");
|
|
688
|
+
}
|
|
689
|
+
function dec2Hex(num) {
|
|
690
|
+
const hex = Math.round(num * 255).toString(16);
|
|
691
|
+
return hex.length === 1 ? "0" + hex : hex;
|
|
692
|
+
}
|
|
693
|
+
function frac2Dec(frac) {
|
|
694
|
+
let split = frac.split(" ");
|
|
695
|
+
const whole = split.length == 2 ? Number(split[0]) : 0;
|
|
696
|
+
split = split.pop().split("/");
|
|
697
|
+
return whole + Number(split[0]) / Number(split[1]);
|
|
698
|
+
}
|
|
699
|
+
function numSuffix(n) {
|
|
700
|
+
const s = ["th", "st", "nd", "rd"], v = n % 100;
|
|
701
|
+
return `${n}${s[(v - 20) % 10] || s[v] || s[0]}`;
|
|
702
|
+
}
|
|
703
|
+
function contrast(color) {
|
|
704
|
+
const exploded = color?.match(color.length >= 6 ? /[0-9a-fA-F]{2}/g : /[0-9a-fA-F]/g);
|
|
674
705
|
if (!exploded || exploded?.length < 3) return "black";
|
|
675
706
|
const [r, g, b] = exploded.map((hex) => parseInt(hex.length == 1 ? `${hex}${hex}` : hex, 16));
|
|
676
707
|
const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
|
|
677
708
|
return luminance > 0.5 ? "black" : "white";
|
|
678
709
|
}
|
|
710
|
+
function hex2Int(hex) {
|
|
711
|
+
let r = 0, g = 0, b = 0;
|
|
712
|
+
if (hex.length === 4) {
|
|
713
|
+
r = parseInt(hex[1] + hex[1], 16);
|
|
714
|
+
g = parseInt(hex[2] + hex[2], 16);
|
|
715
|
+
b = parseInt(hex[3] + hex[3], 16);
|
|
716
|
+
} else {
|
|
717
|
+
r = parseInt(hex.slice(1, 3), 16);
|
|
718
|
+
g = parseInt(hex.slice(3, 5), 16);
|
|
719
|
+
b = parseInt(hex.slice(5, 7), 16);
|
|
720
|
+
}
|
|
721
|
+
return { r, g, b };
|
|
722
|
+
}
|
|
723
|
+
function hue2rgb(p, q, t) {
|
|
724
|
+
if (t < 0) t += 1;
|
|
725
|
+
if (t > 1) t -= 1;
|
|
726
|
+
if (t < 1 / 6) return p + (q - p) * 6 * t;
|
|
727
|
+
if (t < 1 / 2) return q;
|
|
728
|
+
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
|
|
729
|
+
return p;
|
|
730
|
+
}
|
|
731
|
+
function int2Hex(r, g, b) {
|
|
732
|
+
return "#" + dec2Hex(r) + dec2Hex(g) + dec2Hex(b);
|
|
733
|
+
}
|
|
734
|
+
function shadeColor(hex, amount) {
|
|
735
|
+
let { r, g, b } = hex2Int(hex);
|
|
736
|
+
r /= 255;
|
|
737
|
+
g /= 255;
|
|
738
|
+
b /= 255;
|
|
739
|
+
const max = Math.max(r, g, b), min = Math.min(r, g, b);
|
|
740
|
+
let h, s, l = (max + min) / 2;
|
|
741
|
+
if (max === min) {
|
|
742
|
+
h = s = 0;
|
|
743
|
+
} else {
|
|
744
|
+
const d = max - min;
|
|
745
|
+
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
746
|
+
switch (max) {
|
|
747
|
+
case r:
|
|
748
|
+
h = (g - b) / d + (g < b ? 6 : 0);
|
|
749
|
+
break;
|
|
750
|
+
case g:
|
|
751
|
+
h = (b - r) / d + 2;
|
|
752
|
+
break;
|
|
753
|
+
case b:
|
|
754
|
+
h = (r - g) / d + 4;
|
|
755
|
+
break;
|
|
756
|
+
default:
|
|
757
|
+
h = 0;
|
|
758
|
+
break;
|
|
759
|
+
}
|
|
760
|
+
h /= 6;
|
|
761
|
+
}
|
|
762
|
+
l = Math.max(0, Math.min(1, l + amount));
|
|
763
|
+
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
|
764
|
+
const p = 2 * l - q;
|
|
765
|
+
return int2Hex(hue2rgb(p, q, h + 1 / 3), hue2rgb(p, q, h), hue2rgb(p, q, h - 1 / 3));
|
|
766
|
+
}
|
|
679
767
|
const LETTER_LIST = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
680
768
|
const NUMBER_LIST = "0123456789";
|
|
681
769
|
const SYMBOL_LIST = "~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/";
|
|
@@ -1802,33 +1890,6 @@ ${opts.message || this.desc}`;
|
|
|
1802
1890
|
console.error(CliForeground.RED + str + CliEffects.CLEAR);
|
|
1803
1891
|
}
|
|
1804
1892
|
}
|
|
1805
|
-
function dec2Frac(num, maxDen = 1e3) {
|
|
1806
|
-
let sign = Math.sign(num);
|
|
1807
|
-
num = Math.abs(num);
|
|
1808
|
-
if (Number.isInteger(num)) return sign * num + "";
|
|
1809
|
-
let closest = { n: 0, d: 1, diff: Math.abs(num) };
|
|
1810
|
-
for (let d = 1; d <= maxDen; d++) {
|
|
1811
|
-
let n = Math.round(num * d);
|
|
1812
|
-
let diff = Math.abs(num - n / d);
|
|
1813
|
-
if (diff < closest.diff) {
|
|
1814
|
-
closest = { n, d, diff };
|
|
1815
|
-
if (diff < 1e-8) break;
|
|
1816
|
-
}
|
|
1817
|
-
}
|
|
1818
|
-
let integer = Math.floor(closest.n / closest.d);
|
|
1819
|
-
let numerator = closest.n - integer * closest.d;
|
|
1820
|
-
return (sign < 0 ? "-" : "") + (integer ? integer + " " : "") + (numerator ? numerator + "/" + closest.d : "");
|
|
1821
|
-
}
|
|
1822
|
-
function fracToDec(frac) {
|
|
1823
|
-
let split = frac.split(" ");
|
|
1824
|
-
const whole = split.length == 2 ? Number(split[0]) : 0;
|
|
1825
|
-
split = split.pop().split("/");
|
|
1826
|
-
return whole + Number(split[0]) / Number(split[1]);
|
|
1827
|
-
}
|
|
1828
|
-
function numSuffix(n) {
|
|
1829
|
-
const s = ["th", "st", "nd", "rd"], v = n % 100;
|
|
1830
|
-
return `${n}${s[(v - 20) % 10] || s[v] || s[0]}`;
|
|
1831
|
-
}
|
|
1832
1893
|
function compareVersions(target, vs) {
|
|
1833
1894
|
const [tMajor, tMinor, tPatch] = target.split(".").map((v) => +v.replace(/[^0-9]/g, ""));
|
|
1834
1895
|
const [vMajor, vMinor, vPatch] = vs.split(".").map((v) => +v.replace(/[^0-9]/g, ""));
|
|
@@ -2326,7 +2387,68 @@ ${opts.message || this.desc}`;
|
|
|
2326
2387
|
function findTemplateVars(html) {
|
|
2327
2388
|
const variables = /* @__PURE__ */ new Set();
|
|
2328
2389
|
const arrays = /* @__PURE__ */ new Set();
|
|
2329
|
-
const excluded = /* @__PURE__ */ new Set([
|
|
2390
|
+
const excluded = /* @__PURE__ */ new Set([
|
|
2391
|
+
"let",
|
|
2392
|
+
"const",
|
|
2393
|
+
"var",
|
|
2394
|
+
"function",
|
|
2395
|
+
"if",
|
|
2396
|
+
"while",
|
|
2397
|
+
"do",
|
|
2398
|
+
"this",
|
|
2399
|
+
"typeof",
|
|
2400
|
+
"new",
|
|
2401
|
+
"instanceof",
|
|
2402
|
+
"in",
|
|
2403
|
+
"for",
|
|
2404
|
+
"else",
|
|
2405
|
+
"case",
|
|
2406
|
+
"break",
|
|
2407
|
+
"continue",
|
|
2408
|
+
"switch",
|
|
2409
|
+
"default",
|
|
2410
|
+
"with",
|
|
2411
|
+
"eval",
|
|
2412
|
+
"arguments",
|
|
2413
|
+
"void",
|
|
2414
|
+
"delete",
|
|
2415
|
+
"null",
|
|
2416
|
+
"undefined",
|
|
2417
|
+
"true",
|
|
2418
|
+
"false",
|
|
2419
|
+
"async",
|
|
2420
|
+
"await",
|
|
2421
|
+
"try",
|
|
2422
|
+
"catch",
|
|
2423
|
+
"finally",
|
|
2424
|
+
"throw",
|
|
2425
|
+
"return",
|
|
2426
|
+
"yield",
|
|
2427
|
+
"debugger",
|
|
2428
|
+
"extends",
|
|
2429
|
+
"import",
|
|
2430
|
+
"export",
|
|
2431
|
+
"class",
|
|
2432
|
+
"super",
|
|
2433
|
+
"static",
|
|
2434
|
+
"get",
|
|
2435
|
+
"set",
|
|
2436
|
+
"constructor",
|
|
2437
|
+
"enum",
|
|
2438
|
+
"implements",
|
|
2439
|
+
"interface",
|
|
2440
|
+
"package",
|
|
2441
|
+
"private",
|
|
2442
|
+
"protected",
|
|
2443
|
+
"public",
|
|
2444
|
+
"abstract",
|
|
2445
|
+
"final",
|
|
2446
|
+
"native",
|
|
2447
|
+
"synchronized",
|
|
2448
|
+
"throws",
|
|
2449
|
+
"transient",
|
|
2450
|
+
"volatile"
|
|
2451
|
+
]);
|
|
2330
2452
|
for (const loop of matchAll(html, /\{\{\s*?\*\s*?(.+?)\s+in\s+(.+?)\s*?}}/g)) {
|
|
2331
2453
|
const [element, index = "index"] = loop[1].replaceAll(/[()\s]/g, "").split(",");
|
|
2332
2454
|
excluded.add(element);
|
|
@@ -2916,6 +3038,7 @@ ${err.message || err.toString()}`);
|
|
|
2916
3038
|
exports2.dayOfWeek = dayOfWeek;
|
|
2917
3039
|
exports2.dayOfYear = dayOfYear;
|
|
2918
3040
|
exports2.dec2Frac = dec2Frac;
|
|
3041
|
+
exports2.dec2Hex = dec2Hex;
|
|
2919
3042
|
exports2.decodeJwt = decodeJwt;
|
|
2920
3043
|
exports2.deepCopy = deepCopy;
|
|
2921
3044
|
exports2.deepMerge = deepMerge;
|
|
@@ -2937,12 +3060,15 @@ ${err.message || err.toString()}`);
|
|
|
2937
3060
|
exports2.formatDate = formatDate;
|
|
2938
3061
|
exports2.formatMs = formatMs;
|
|
2939
3062
|
exports2.formatPhoneNumber = formatPhoneNumber;
|
|
2940
|
-
exports2.
|
|
3063
|
+
exports2.frac2Dec = frac2Dec;
|
|
2941
3064
|
exports2.fromCsv = fromCsv;
|
|
2942
3065
|
exports2.gravatar = gravatar;
|
|
3066
|
+
exports2.hex2Int = hex2Int;
|
|
3067
|
+
exports2.hue2rgb = hue2rgb;
|
|
2943
3068
|
exports2.includes = includes;
|
|
2944
3069
|
exports2.insertAt = insertAt;
|
|
2945
3070
|
exports2.instantInterval = instantInterval;
|
|
3071
|
+
exports2.int2Hex = int2Hex;
|
|
2946
3072
|
exports2.ipV6ToV4 = ipV6ToV4;
|
|
2947
3073
|
exports2.isEqual = isEqual;
|
|
2948
3074
|
exports2.kebabCase = kebabCase;
|
|
@@ -2966,6 +3092,7 @@ ${err.message || err.toString()}`);
|
|
|
2966
3092
|
exports2.renderTemplate = renderTemplate;
|
|
2967
3093
|
exports2.reservedIp = reservedIp;
|
|
2968
3094
|
exports2.search = search;
|
|
3095
|
+
exports2.shadeColor = shadeColor;
|
|
2969
3096
|
exports2.sleep = sleep;
|
|
2970
3097
|
exports2.sleepWhile = sleepWhile;
|
|
2971
3098
|
exports2.snakeCase = snakeCase;
|