omarabdhkem's picture
Upload 136 files
c6072ca verified
Raw History Blame Contribute Delete
4.48 kB
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export function formatCurrency(price: number, locale: string = 'en-US', currency: string = 'USD') {
return new Intl.NumberFormat(locale, {
style: 'currency',
currency,
}).format(price);
}
export function calculateDiscountPercentage(originalPrice: number, salePrice: number) {
return Math.round(((originalPrice - salePrice) / originalPrice) * 100);
}
// Car body types
export const CAR_BODY_TYPES = [
"Sedan",
"SUV",
"Hatchback",
"Pickup",
"Coupe",
"Minivan",
"Wagon",
"Van",
"Convertible",
"Luxury",
"Electric",
"Classic",
];
// Fuel types
export const FUEL_TYPES = [
"Gasoline",
"Diesel",
"Hybrid",
"Electric",
"Plug-in Hybrid",
"Hydrogen",
];
/**
* التحقق من حالة الاتصال بالإنترنت
*/
export function isOnline(): boolean {
return typeof navigator !== 'undefined' && navigator.onLine;
}
/**
* مراقبة حالة الاتصال بالإنترنت
*/
export function monitorOnlineStatus(
onOnline: () => void,
onOffline: () => void
) {
if (typeof window === 'undefined') return () => {};
window.addEventListener('online', onOnline);
window.addEventListener('offline', onOffline);
return () => {
window.removeEventListener('online', onOnline);
window.removeEventListener('offline', onOffline);
};
}
// Years for dropdown
export function getYearOptions(): string[] {
const currentYear = new Date().getFullYear();
const years: string[] = [];
for (let i = currentYear; i >= currentYear - 30; i--) {
years.push(i.toString());
}
return years;
}
/**
* تأخير التنفيذ بعدد محدد من الميلي ثانية
*/
export function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* تحويل التاريخ إلى صيغة مناسبة للعرض
*/
export function formatDate(date: Date, locale: string = 'en-US'): string {
return new Intl.DateTimeFormat(locale, {
year: 'numeric',
month: 'long',
day: 'numeric',
}).format(date);
}
/**
* تقصير النص إذا تجاوز طول معين
*/
export function truncateText(text: string, maxLength: number = 100): string {
if (text.length <= maxLength) return text;
return text.slice(0, maxLength) + '...';
}
/**
* محاولة تنفيذ وظيفة مع إعادة المحاولة عدة مرات
*/
export async function retryOperation<T>(
operation: () => Promise<T>,
maxRetries: number = 3,
delayMs: number = 1000
): Promise<T> {
let lastError: Error;
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
return await operation();
} catch (error) {
lastError = error as Error;
if (attempt < maxRetries - 1) {
await sleep(delayMs);
}
}
}
throw lastError!;
}
/**
* تحويل نص إلى slug (مناسب للروابط)
*/
export function slugify(text: string): string {
return text
.toString()
.toLowerCase()
.trim()
.replace(/\s+/g, '-')
.replace(/[^\w\-]+/g, '')
.replace(/\-\-+/g, '-');
}
/**
* تحقق مما إذا كان الجهاز هو جهاز محمول
*/
export function isMobileDevice(): boolean {
return typeof window !== 'undefined' &&
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
}
/**
* الحصول على معامل التكبير الحالي للمتصفح
*/
export function getBrowserZoomLevel(): number {
return Math.round((window.outerWidth / window.innerWidth) * 100) / 100;
}
/**
* تحويل حجم الملف إلى صيغة مقروءة
*/
export function formatFileSize(bytes: number): string {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
/**
* استخراج رمز اللون من سلسلة نصية
*/
export function extractColors(text: string): string[] {
const colorRegex = /#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})/g;
return text.match(colorRegex) || [];
}
/**
* إنشاء معرف فريد
*/
export function generateUniqueId(): string {
return Date.now().toString(36) + Math.random().toString(36).substr(2, 5);
}