AI Resource Pro / website · airesourcepro.com / src / lib / utils.ts
| 1 | import { type ClassValue, clsx } from "clsx" |
| 2 | import { twMerge } from "tailwind-merge" |
| 3 | |
| 4 | export function cn(...inputs: ClassValue[]) { |
| 5 | return twMerge(clsx(inputs)) |
| 6 | } |
| 7 | |
| 8 | /** |
| 9 | * Converts a string to a URL-friendly slug |
| 10 | * @param text The text to convert to a slug |
| 11 | * @returns A URL-friendly slug |
| 12 | */ |
| 13 | export function slugify(text: string): string { |
| 14 | return text |
| 15 | .toString() |
| 16 | .toLowerCase() |
| 17 | .trim() |
| 18 | .replace(/\s+/g, '-') // Replace spaces with - |
| 19 | .replace(/&/g, '-and-') // Replace & with 'and' |
| 20 | .replace(/[^\w\-]+/g, '') // Remove all non-word characters |
| 21 | .replace(/\-\-+/g, '-') // Replace multiple - with single - |
| 22 | .replace(/^-+/, '') // Trim - from start of text |
| 23 | .replace(/-+$/, ''); // Trim - from end of text |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Format a number of bytes into a human-readable string |
| 28 | */ |
| 29 | export function formatBytes(bytes: number, decimals: number = 2): string { |
| 30 | if (bytes === 0) return '0 Bytes'; |
| 31 | |
| 32 | const k = 1024; |
| 33 | const dm = decimals < 0 ? 0 : decimals; |
| 34 | const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; |
| 35 | |
| 36 | const i = Math.floor(Math.log(bytes) / Math.log(k)); |
| 37 | |
| 38 | return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; |
| 39 | } |