Initial commit: fabric-trade-user Vue 3 app with mobile and desktop layouts
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
const persianDigits = '۰۱۲۳۴۵۶۷۸۹'
|
||||
const numberFormatter = new Intl.NumberFormat('fa-IR', { useGrouping: true })
|
||||
|
||||
export function normalizeDigits(input: string): string {
|
||||
return input.replace(/[۰-۹]/g, (digit) => String(persianDigits.indexOf(digit)))
|
||||
}
|
||||
|
||||
export function extractNumericString(input: string): string {
|
||||
return normalizeDigits(input).replace(/[^\d]/g, '')
|
||||
}
|
||||
|
||||
export function parseFormattedNumber(value: string): number {
|
||||
const numeric = extractNumericString(value)
|
||||
return numeric ? Number(numeric) : 0
|
||||
}
|
||||
|
||||
export function formatNumber(value: number): string {
|
||||
if (!Number.isFinite(value)) return '—'
|
||||
return numberFormatter.format(value)
|
||||
}
|
||||
|
||||
export function formatCurrency(value: number): string {
|
||||
return `${formatNumber(value)} تومان`
|
||||
}
|
||||
|
||||
export function formatMeterage(value: number): string {
|
||||
return `${formatNumber(value)} متر`
|
||||
}
|
||||
|
||||
import { parseISODate } from '@/utils/jalali'
|
||||
|
||||
const persianDateFormatter = new Intl.DateTimeFormat('fa-IR-u-ca-persian', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
})
|
||||
|
||||
const persianDateTimeFormatter = new Intl.DateTimeFormat('fa-IR-u-ca-persian', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
})
|
||||
|
||||
function toDateValue(date: string | Date): Date {
|
||||
if (typeof date !== 'string') return date
|
||||
if (/^\d{4}-\d{2}-\d{2}$/.test(date)) return parseISODate(date)
|
||||
return new Date(date)
|
||||
}
|
||||
|
||||
export function formatDate(date: string | Date): string {
|
||||
return persianDateFormatter.format(toDateValue(date))
|
||||
}
|
||||
|
||||
export function formatDateTime(date: string | Date): string {
|
||||
return persianDateTimeFormatter.format(toDateValue(date))
|
||||
}
|
||||
|
||||
export function formatTime(date: string | Date): string {
|
||||
const d = typeof date === 'string' ? new Date(date) : date
|
||||
return new Intl.DateTimeFormat('fa-IR', {
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
}).format(d)
|
||||
}
|
||||
|
||||
export function toPersianDigits(input: string): string {
|
||||
return input.replace(/\d/g, (d) => persianDigits[Number(d)]!)
|
||||
}
|
||||
|
||||
export function normalizePhone(phone: string): string {
|
||||
const digits = phone.replace(/\D/g, '')
|
||||
if (digits.startsWith('98')) return '0' + digits.slice(2)
|
||||
if (digits.startsWith('9') && digits.length === 10) return '0' + digits
|
||||
return digits
|
||||
}
|
||||
|
||||
export function generateId(): string {
|
||||
return crypto.randomUUID()
|
||||
}
|
||||
|
||||
export function generateOrderNumber(): string {
|
||||
const year = new Intl.DateTimeFormat('fa-IR-u-ca-persian', { year: 'numeric' }).format(new Date())
|
||||
const persianYear = year.replace(/[^\d]/g, '') || '1403'
|
||||
const seq = Math.floor(Math.random() * 9000) + 1000
|
||||
return `ORD-${persianYear}-${seq}`
|
||||
}
|
||||
Reference in New Issue
Block a user