Files
meezi/web/koja/src/lib/utils.ts
T

73 lines
2.1 KiB
TypeScript
Raw Normal View History

import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export function formatPrice(price: number, locale: string): string {
if (locale === "fa") {
return new Intl.NumberFormat("fa-IR").format(price) + " تومان";
}
return new Intl.NumberFormat("en-US").format(price) + " ﷼";
}
export function formatRating(rating: number): string {
return rating.toFixed(1);
}
// Convert Persian/Arabic digits to Latin
export function toLatinDigits(str: string): string {
return str
.replace(/[۰-۹]/g, (d) => String(d.charCodeAt(0) - 0x06f0))
.replace(/[٠-٩]/g, (d) => String(d.charCodeAt(0) - 0x0660));
}
// Slugify a Persian or Latin string for URLs
export function slugify(str: string): string {
return toLatinDigits(str)
.toLowerCase()
.trim()
.replace(/[\s_]+/g, "-")
.replace(/[^a-z0-9؀-ۿ-]/g, "")
.replace(/-+/g, "-");
}
const DAY_KEYS_FA: Record<string, string> = {
sat: "شنبه",
sun: "یکشنبه",
mon: "دوشنبه",
tue: "سه‌شنبه",
wed: "چهارشنبه",
thu: "پنجشنبه",
fri: "جمعه",
};
const DAY_KEYS_EN: Record<string, string> = {
sat: "Sat",
sun: "Sun",
mon: "Mon",
tue: "Tue",
wed: "Wed",
thu: "Thu",
fri: "Fri",
};
export function getDayLabel(key: string, locale: string): string {
return locale === "fa" ? DAY_KEYS_FA[key] ?? key : DAY_KEYS_EN[key] ?? key;
}
export const PRICE_TIER_LABELS: Record<string, { fa: string; en: string }> = {
budget: { fa: "مقرون‌به‌صرفه", en: "Budget" },
moderate: { fa: "معمولی", en: "Moderate" },
upscale: { fa: "لوکس", en: "Upscale" },
luxury: { fa: "پریمیوم", en: "Luxury" },
};
export const NOISE_LABELS: Record<string, { fa: string; en: string }> = {
quiet: { fa: "آرام", en: "Quiet" },
moderate: { fa: "نسبتاً آرام", en: "Moderate" },
lively: { fa: "شلوغ", en: "Lively" },
loud: { fa: "پرسروصدا", en: "Loud" },
};