infoalloggi-monorepo/apps/infoalloggi/src/lib/utils.ts

46 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-08-04 17:45:44 +02:00
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export function getInitials(name: string) {
name = name.trim();
if (name.includes(" ")) {
const [firstName, lastName] = name.split(" ");
if (firstName && lastName) {
return (firstName.charAt(0) + lastName.charAt(0)).toUpperCase();
}
}
return name.slice(0, 2).toUpperCase();
}
export function getHexColorFromString(str: string): string {
let hash = 0;
if (str.length === 0) return "#000000"; // Return black for empty string
for (let i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
hash = hash & 0xffffffff; // Ensure hash is a 32-bit integer
}
let color = "#";
for (let i = 0; i < 3; i++) {
const value = (hash >> (i * 8)) & 255;
color += value.toString(16).padStart(2, "0");
}
return color;
}
export function formatCurrency(amount: number): string {
return new Intl.NumberFormat("it-IT", {
style: "currency",
currency: "EUR",
minimumFractionDigits: 2,
maximumFractionDigits: 2,
}).format(amount);
}