24 lines
975 B
TypeScript
24 lines
975 B
TypeScript
const FA_DIGITS = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"];
|
||
|
||
export function toFaDigits(input: string | number): string {
|
||
return String(input).replace(/\d/g, (d) => FA_DIGITS[Number(d)]);
|
||
}
|
||
|
||
// Prices come from the API in Toman.
|
||
export function formatPrice(value: number | null | undefined): string {
|
||
if (value === null || value === undefined) return "";
|
||
return toFaDigits(value.toLocaleString("en-US"));
|
||
}
|
||
|
||
export function discountPercent(price: number, oldPrice: number): number {
|
||
if (!oldPrice || oldPrice <= price) return 0;
|
||
return Math.round(((oldPrice - price) / oldPrice) * 100);
|
||
}
|
||
|
||
// The static-image-slot API documents aspectRatio as "16:9", but CSS's
|
||
// aspect-ratio property needs a slash ("16/9"), not a colon.
|
||
export function toCssAspectRatio(value: string | number | null | undefined): string | undefined {
|
||
if (value === null || value === undefined || value === "") return undefined;
|
||
return String(value).replace(":", "/");
|
||
}
|