48 lines
1.7 KiB
TypeScript
48 lines
1.7 KiB
TypeScript
export const toPersian = (n: number | string | undefined | null) => {
|
||
if (n === undefined || n === null) return "";
|
||
return n.toString().replace(/\d/g, d => '۰۱۲۳۴۵۶۷۸۹'[parseInt(d)]);
|
||
};
|
||
|
||
export const toEnglishDigits = (str: string) => {
|
||
if (!str) return "";
|
||
return str.replace(/[۰-۹]/g, d => '۰۱۲۳۴۵۶۷۸۹'.indexOf(d).toString());
|
||
};
|
||
|
||
export function cn(...classes: (string | boolean | undefined)[]) {
|
||
return classes.filter(Boolean).join(" ");
|
||
}
|
||
|
||
|
||
export function isProductOutOfStock(product?: {
|
||
stock?: number;
|
||
stockStatus?: "IN_STOCK" | "OUT_OF_STOCK" | "PRE_ORDER" | "DISCONTINUED" | string;
|
||
packageSize?: number | string;
|
||
} | null): boolean {
|
||
if (!product) return false;
|
||
if (product.stockStatus === "OUT_OF_STOCK" || product.stockStatus === "DISCONTINUED") return true;
|
||
if (typeof product.stock === "number" && product.stock <= 0) return true;
|
||
if (product.stock !== undefined && product.stock !== null && Number(product.stock) <= 0) return true;
|
||
if (product.packageSize !== undefined && product.packageSize !== null && Number(product.packageSize) <= 0) return true;
|
||
return false;
|
||
}
|
||
|
||
export function normalizePersianText(input: string | undefined | null): string {
|
||
if (!input) return "";
|
||
return input
|
||
.toString()
|
||
.toLowerCase()
|
||
.replace(/[\u200B-\u200D\uFEFF]/g, "")
|
||
.replace(/[\u200C]/g, " ")
|
||
.replace(/[يیى]/g, "ی")
|
||
.replace(/[ك]/g, "ک")
|
||
.replace(/[ة]/g, "ه")
|
||
.replace(/[أإآ]/g, "ا")
|
||
.replace(/[ؤ]/g, "و")
|
||
.replace(/[ء]/g, "")
|
||
.replace(/[\u064B-\u065F]/g, "")
|
||
.replace(/[۰-۹]/g, d => "0123456789"["۰۱۲۳۴۵۶۷۸۹".indexOf(d)])
|
||
.replace(/[٠-٩]/g, d => "0123456789"["٠١٢٣٤٥٦٧٨٩".indexOf(d)])
|
||
.replace(/\s+/g, " ")
|
||
.trim();
|
||
}
|