92 lines
2.6 KiB
TypeScript
92 lines
2.6 KiB
TypeScript
/**
|
|
* Utility functions for formatting data
|
|
*/
|
|
|
|
/**
|
|
* Format price with Persian number formatting and تومان suffix
|
|
*/
|
|
export const formatPrice = (price: number): string => {
|
|
if (price === null || price === undefined || isNaN(price)) {
|
|
return '0 تومان';
|
|
}
|
|
return new Intl.NumberFormat('fa-IR').format(price) + ' تومان';
|
|
};
|
|
|
|
/**
|
|
* Format currency amount with Persian number formatting and تومان suffix
|
|
*/
|
|
export const formatCurrency = (amount: number): string => {
|
|
if (amount === null || amount === undefined || isNaN(amount)) {
|
|
return '0 تومان';
|
|
}
|
|
return new Intl.NumberFormat('fa-IR').format(amount) + ' تومان';
|
|
};
|
|
|
|
/**
|
|
* Format date string to Persian locale
|
|
*/
|
|
export const formatDate = (dateString: string | Date): string => {
|
|
if (!dateString) return '-';
|
|
|
|
try {
|
|
const date = typeof dateString === 'string' ? new Date(dateString) : dateString;
|
|
if (isNaN(date.getTime())) return '-';
|
|
|
|
return date.toLocaleDateString('fa-IR');
|
|
} catch {
|
|
return '-';
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Format date and time string to Persian locale
|
|
*/
|
|
export const formatDateTime = (dateString: string | Date): string => {
|
|
if (!dateString) return '-';
|
|
|
|
try {
|
|
const date = typeof dateString === 'string' ? new Date(dateString) : dateString;
|
|
if (isNaN(date.getTime())) return '-';
|
|
|
|
return date.toLocaleDateString('fa-IR', {
|
|
year: 'numeric',
|
|
month: 'long',
|
|
day: 'numeric',
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
});
|
|
} catch {
|
|
return '-';
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Format number with thousands separator (Persian)
|
|
*/
|
|
export const formatNumber = (num: number): string => {
|
|
if (num === null || num === undefined || isNaN(num)) {
|
|
return '0';
|
|
}
|
|
return new Intl.NumberFormat('fa-IR').format(num);
|
|
};
|
|
|
|
/**
|
|
* Format date string to local datetime input format (YYYY-MM-DDTHH:mm)
|
|
*/
|
|
export const formatDateTimeLocal = (dateString?: string | Date): string => {
|
|
if (!dateString) return '';
|
|
try {
|
|
const date = typeof dateString === 'string' ? new Date(dateString) : dateString;
|
|
if (isNaN(date.getTime())) return '';
|
|
const year = date.getFullYear();
|
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
const day = String(date.getDate()).padStart(2, '0');
|
|
const hours = String(date.getHours()).padStart(2, '0');
|
|
const minutes = String(date.getMinutes()).padStart(2, '0');
|
|
return `${year}-${month}-${day}T${hours}:${minutes}`;
|
|
} catch {
|
|
return '';
|
|
}
|
|
};
|
|
|