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-09-30 20:05:37 +00:00
|
|
|
const newAmount =
|
|
|
|
// handle -0 case
|
|
|
|
Math.round(amount) === 0
|
|
|
|
? 0
|
|
|
|
: // Handle 499.9999999999999 case
|
2022-10-04 08:18:22 +00:00
|
|
|
(amount > 0 ? Math.floor : Math.ceil)(
|
|
|
|
amount + 0.00000000001 * Math.sign(amount)
|
|
|
|
)
|
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
|
|
|
|
2022-06-17 21:28:12 +00:00
|
|
|
export function formatMoneyWithDecimals(amount: number) {
|
|
|
|
return ENV_CONFIG.moneyMoniker + amount.toFixed(2)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-07-19 17:30:13 +00:00
|
|
|
const showPrecision = (x: number, sigfigs: number) =>
|
|
|
|
// convert back to number for weird formatting reason
|
|
|
|
`${Number(x.toPrecision(sigfigs))}`
|
|
|
|
|
2022-05-20 19:25:32 +00:00
|
|
|
// Eg 1234567.89 => 1.23M; 5678 => 5.68K
|
2022-05-24 06:44:16 +00:00
|
|
|
export function formatLargeNumber(num: number, sigfigs = 2): string {
|
2022-05-20 19:25:32 +00:00
|
|
|
const absNum = Math.abs(num)
|
2022-07-19 17:30:13 +00:00
|
|
|
if (absNum < 1) return showPrecision(num, sigfigs)
|
2022-07-16 19:50:07 +00:00
|
|
|
|
2022-07-19 17:30:13 +00:00
|
|
|
if (absNum < 100) return showPrecision(num, 2)
|
|
|
|
if (absNum < 1000) return showPrecision(num, 3)
|
|
|
|
if (absNum < 10000) return showPrecision(num, 4)
|
2022-05-20 19:25:32 +00:00
|
|
|
|
|
|
|
const suffix = ['', 'K', 'M', 'B', 'T', 'Q']
|
2022-07-16 19:50:07 +00:00
|
|
|
const i = Math.floor(Math.log10(absNum) / 3)
|
|
|
|
|
2022-07-19 17:30:13 +00:00
|
|
|
const numStr = showPrecision(num / Math.pow(10, 3 * i), sigfigs)
|
|
|
|
return `${numStr}${suffix[i] ?? ''}`
|
2022-05-20 19:25: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
|
|
|
}
|