B2D-Ventures/src/lib/utils.ts
2024-11-01 22:28:01 +07:00

33 lines
854 B
TypeScript

import { type ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
export function sum(list: any[]) {
if (!list || list.length === 0) {
return 0;
}
return list.reduce((total, num) => total + num, 0);
}
export function sumByKey(list: any[], key: string) {
// example usage
// const items = [
// { amount: 10 },
// { amount: 20 },
// { amount: 30 },
// { amount: 40 }
// ];
// const totalAmount = sumByKey(items, 'amount');
// console.log(totalAmount); // Output: 100
return list.reduce((total, obj) => total + (obj[key] || 0), 0);
}
export function toPercentage(part: number, total: number): number {
if (total === 0) return 0; // Prevent division by zero
return Number(((part * 100) / total).toFixed(2));
}