mirror of
https://github.com/Sosokker/B2D-Ventures.git
synced 2025-12-19 05:54:06 +01:00
33 lines
854 B
TypeScript
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));
|
|
} |