Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/utils/formatting.js
import { currency } from 'components/Common/Ticker.js'; | import { currency } from 'components/Common/Ticker.js'; | ||||
import BigNumber from 'bignumber.js'; | |||||
export const formatDate = (dateString, userLocale = 'en') => { | export const formatDate = (dateString, userLocale = 'en') => { | ||||
const options = { month: 'short', day: 'numeric', year: 'numeric' }; | const options = { month: 'short', day: 'numeric', year: 'numeric' }; | ||||
const dateFormattingError = 'Unable to format date.'; | const dateFormattingError = 'Unable to format date.'; | ||||
try { | try { | ||||
if (dateString) { | if (dateString) { | ||||
return new Date(dateString * 1000).toLocaleDateString( | return new Date(dateString * 1000).toLocaleDateString( | ||||
userLocale, | userLocale, | ||||
options, | options, | ||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | try { | ||||
maximumFractionDigits: currency.cashDecimals, | maximumFractionDigits: currency.cashDecimals, | ||||
}); | }); | ||||
} catch (err) { | } catch (err) { | ||||
console.log(`Error in formatBalance for ${unformattedBalance}`); | console.log(`Error in formatBalance for ${unformattedBalance}`); | ||||
console.log(err); | console.log(err); | ||||
return unformattedBalance; | return unformattedBalance; | ||||
} | } | ||||
}; | }; | ||||
// unformattedBalance will always be a BigNumber, tokenDecimal will always be a number | |||||
export const formatTokenBalance = ( | |||||
unformattedBalance, | |||||
tokenDecimal, | |||||
defaultLocale = 'en', | |||||
) => { | |||||
let formattedTokenBalance; | |||||
let convertedTokenBalance; | |||||
let locale = defaultLocale; | |||||
try { | |||||
if ( | |||||
tokenDecimal === undefined || | |||||
unformattedBalance === undefined || | |||||
typeof tokenDecimal !== 'number' || | |||||
!BigNumber.isBigNumber(unformattedBalance) | |||||
) { | |||||
return undefined; | |||||
} | |||||
if (navigator && navigator.language) { | |||||
locale = navigator.language; | |||||
} | |||||
// Use toFixed to get a string with the correct decimal places | |||||
formattedTokenBalance = new BigNumber(unformattedBalance).toFixed( | |||||
tokenDecimal, | |||||
); | |||||
// formattedTokenBalance is converted into a number as toLocaleString does not work with a string | |||||
convertedTokenBalance = parseFloat( | |||||
formattedTokenBalance, | |||||
).toLocaleString(locale, { | |||||
minimumFractionDigits: tokenDecimal, | |||||
}); | |||||
return convertedTokenBalance; | |||||
} catch (err) { | |||||
console.log(`Error in formatTokenBalance for ${unformattedBalance}`); | |||||
console.log(err); | |||||
return unformattedBalance; | |||||
} | |||||
}; |