46 lines
1.2 KiB
JavaScript
46 lines
1.2 KiB
JavaScript
export const convertPersianToEnglish = (text) => {
|
||
if (text === null || text === undefined) {
|
||
return '0'
|
||
}
|
||
|
||
const persianNumbers = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']
|
||
const englishNumbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
|
||
|
||
let result = String(text)
|
||
persianNumbers.forEach((persian, index) => {
|
||
result = result.replace(new RegExp(persian, 'g'), englishNumbers[index])
|
||
})
|
||
|
||
return result
|
||
}
|
||
|
||
export const convertEnglishToPersian = (text) => {
|
||
if (text === null || text === undefined) {
|
||
return '۰'
|
||
}
|
||
|
||
const englishNumbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
|
||
const persianNumbers = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']
|
||
|
||
let result = String(text)
|
||
englishNumbers.forEach((english, index) => {
|
||
result = result.replace(new RegExp(english, 'g'), persianNumbers[index])
|
||
})
|
||
|
||
return result
|
||
}
|
||
|
||
export const formatPrice = (price) => {
|
||
if (price === null || price === undefined) {
|
||
return '۰'
|
||
}
|
||
|
||
const numPrice = Number(price)
|
||
if (isNaN(numPrice)) {
|
||
return '۰'
|
||
}
|
||
|
||
const formattedPrice = numPrice.toLocaleString('fa-IR')
|
||
return convertEnglishToPersian(formattedPrice)
|
||
}
|