2022-03-09 02:43:30 +00:00
|
|
|
import { ENV_CONFIG } from '../envs/constants'
|
|
|
|
|
2021-12-11 03:47:46 +00:00
|
|
|
const formatter = new Intl.NumberFormat('en-US', {
|
|
|
|
style: 'currency',
|
|
|
|
currency: 'USD',
|
|
|
|
maximumFractionDigits: 0,
|
2021-12-14 07:09:46 +00:00
|
|
|
minimumFractionDigits: 0,
|
2021-12-11 03:47:46 +00:00
|
|
|
})
|
|
|
|
|
2021-12-11 04:09:32 +00:00
|
|
|
export function formatMoney(amount: number) {
|
2022-04-18 22:02:53 +00:00
|
|
|
const newAmount = Math.round(amount) === 0 ? 0 : Math.floor(amount) // handle -0 case
|
2022-05-09 17:38:33 +00:00
|
|
|
return ENV_CONFIG.moneyMoniker + formatter.format(newAmount).replace('$', '')
|
2021-12-11 03:47:46 +00:00
|
|
|
}
|
2021-12-11 04:09:32 +00:00
|
|
|
|
|
|
|
export function formatWithCommas(amount: number) {
|
2022-04-18 22:02:53 +00:00
|
|
|
return formatter.format(Math.floor(amount)).replace('$', '')
|
2021-12-15 07:41:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 18:14:24 +00:00
|
|
|
export function manaToUSD(mana: number) {
|
|
|
|
return (mana / 100).toLocaleString('en-US', {
|
|
|
|
style: 'currency',
|
|
|
|
currency: 'USD',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-22 23:53:23 +00:00
|
|
|
export function formatPercent(zeroToOne: number) {
|
|
|
|
// Show 1 decimal place if <2% or >98%, giving more resolution on the tails
|
|
|
|
const decimalPlaces = zeroToOne < 0.02 || zeroToOne > 0.98 ? 1 : 0
|
|
|
|
return (zeroToOne * 100).toFixed(decimalPlaces) + '%'
|
2021-12-11 04:09:32 +00:00
|
|
|
}
|
2022-01-22 21:47:24 +00:00
|
|
|
|
|
|
|
export function toCamelCase(words: string) {
|
2022-01-26 23:43:28 +00:00
|
|
|
const camelCase = words
|
2022-01-22 21:47:24 +00:00
|
|
|
.split(' ')
|
|
|
|
.map((word) => word.trim())
|
|
|
|
.filter((word) => word)
|
|
|
|
.map((word, index) =>
|
|
|
|
index === 0 ? word : word[0].toLocaleUpperCase() + word.substring(1)
|
|
|
|
)
|
|
|
|
.join('')
|
2022-01-26 23:43:28 +00:00
|
|
|
|
|
|
|
// Remove non-alpha-numeric-underscore chars.
|
|
|
|
const regex = /(?:^|\s)(?:[a-z0-9_]+)/gi
|
|
|
|
return (camelCase.match(regex) || [])[0] ?? ''
|
2022-01-22 21:47:24 +00:00
|
|
|
}
|