Files
simple-admin/src/shared/lib/formatPrice.ts
Samandar Turgunboyev a9e99f9755 added tour
2025-10-27 12:54:47 +05:00

36 lines
1.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import i18n from "@/shared/config/i18n";
import { LanguageRoutes } from "@/shared/config/i18n/type";
/**
* Format price. With label.
* @param amount Price
* @param withLabel Show label. Default false
* @returns string. Ex. 1 000 000 som
*/
const formatPrice = (amount: number | string, withLabel = false): string => {
const locale = i18n.language;
const label = withLabel
? locale === LanguageRoutes.RU
? " сум"
: locale === LanguageRoutes.UZ
? " som"
: " som"
: "";
// Agar qiymat bosh yoki null/undefined bolsa — hech narsa korsatmaymiz
if (amount === "" || amount === null || amount === undefined) return "";
// Faqat raqamlarni qoldiramiz
const numeric = String(amount).replace(/\D/g, "");
// Agar hech qanday raqam kiritilmagan bolsa, bosh qaytaramiz
if (numeric === "") return "";
// Raqamni 3 xonadan ajratamiz
const formatted = numeric.replace(/\B(?=(\d{3})+(?!\d))/g, " ");
return formatted + label;
};
export default formatPrice;