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-03-15 22:27:51 +00:00
|
|
|
const newAmount = Math.round(amount) === 0 ? 0 : amount // handle -0 case
|
2022-03-09 02:43:30 +00:00
|
|
|
return (
|
2022-03-15 22:27:51 +00:00
|
|
|
ENV_CONFIG.moneyMoniker + ' ' + formatter.format(newAmount).replace('$', '')
|
2022-03-09 02:43:30 +00:00
|
|
|
)
|
2021-12-11 03:47:46 +00:00
|
|
|
}
|
2021-12-11 04:09:32 +00:00
|
|
|
|
|
|
|
export function formatWithCommas(amount: number) {
|
2021-12-24 21:06:01 +00:00
|
|
|
return formatter.format(amount).replace('$', '')
|
2021-12-15 07:41:50 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 16:20:30 +00:00
|
|
|
const decimalPlaces = (x: number) => Math.ceil(-Math.log10(x)) - 2
|
|
|
|
|
|
|
|
export function formatPercent(decimalPercent: number) {
|
|
|
|
const displayedFigs =
|
|
|
|
(decimalPercent >= 0.02 && decimalPercent <= 0.98) ||
|
|
|
|
decimalPercent <= 0 ||
|
|
|
|
decimalPercent >= 1
|
|
|
|
? 0
|
|
|
|
: Math.max(
|
|
|
|
decimalPlaces(decimalPercent),
|
|
|
|
decimalPlaces(1 - decimalPercent)
|
|
|
|
)
|
|
|
|
|
|
|
|
return (decimalPercent * 100).toFixed(displayedFigs) + '%'
|
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
|
|
|
}
|