forked from live-apps-in/kitty-chan-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
67 lines (56 loc) · 2.13 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export function hexToRgb(hex: string) {
// Remove the '#' character if it's included
hex = hex.replace(/^#/, "");
// Parse the hex string into three parts: red, green, and blue
const r = parseInt(hex.substring(0, 2), 16);
const g = parseInt(hex.substring(2, 4), 16);
const b = parseInt(hex.substring(4, 6), 16);
// Combine the RGB values into a single number
const rgbValue = (r << 16) + (g << 8) + b;
return rgbValue;
}
export function rgbToHex(rgbValue: number): string {
// Convert the decimal RGB value to hexadecimal
const hexColor = "#" + rgbValue.toString(16).padStart(6, "0");
return hexColor;
}
export function filterEmptyFields(embedData: any) {
// Helper function to recursively filter out empty properties
function recursivelyFilter(obj: any) {
for (const key in obj) {
if (typeof obj[key] === "object" && !Array.isArray(obj[key])) {
// If the property is an object (but not an array), recursively filter it
recursivelyFilter(obj[key]);
// Check if all properties within the object are empty
const isEmpty = Object.values(obj[key]).every(
(value) => value === "" || value === undefined
);
if (isEmpty) {
delete obj[key];
}
} else if (obj[key] === "" || obj[key] === undefined) {
// If the property is empty or undefined, delete it
delete obj[key];
}
}
}
// Create a copy of the input data to avoid modifying it directly
const filteredData = { ...embedData };
// Filter out empty fields and include `fields` only when it contains non-empty objects
if (filteredData.fields && filteredData.fields.length > 0) {
filteredData.fields = filteredData.fields.filter(
(field: any) => field.name || field.value
);
if (filteredData.fields.length === 0) {
delete filteredData.fields;
}
}
// Recursively filter empty properties within the main object
recursivelyFilter(filteredData);
return filteredData;
}