Files
gameno-front/src/utils/format.js
T
kavehhn f1a7ad8bf8 feat: show public classes with discount, start countdown, and free spots
Add a landing page classes section fed by the public classes API with pricing and availability details.
2026-08-16 08:11:42 +03:30

36 lines
1.2 KiB
JavaScript
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 moment from 'jalali-moment'
const persianDigits = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']
export function toPersianDigits(value) {
if (value == null) return ''
return String(value).replace(/\d/g, (d) => persianDigits[Number(d)])
}
export function formatJalali(date, format = 'jYYYY/jMM/jDD') {
if (!date) return '—'
return toPersianDigits(moment(date).locale('fa').format(format))
}
export function formatPrice(amount) {
if (amount == null || Number.isNaN(Number(amount))) return '—'
return `${toPersianDigits(Number(amount).toLocaleString('en-US'))} تومان`
}
export function formatDaysUntilStart(days) {
if (days == null || Number.isNaN(Number(days))) return null
const value = Number(days)
if (value > 0) return `${toPersianDigits(value)} روز تا شروع`
if (value === 0) return 'امروز شروع می‌شود'
return `${toPersianDigits(Math.abs(value))} روز از شروع گذشته`
}
export function getApiErrorMessage(error, fallback = 'خطایی رخ داد. دوباره تلاش کنید.') {
return (
error?.response?.data?.error?.message ||
error?.response?.data?.message ||
error?.message ||
fallback
)
}