manifold/common/util/format.ts

39 lines
1.0 KiB
TypeScript
Raw Normal View History

import { ENV_CONFIG } from '../envs/constants'
const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
maximumFractionDigits: 0,
minimumFractionDigits: 0,
})
export function formatMoney(amount: number) {
2022-03-10 05:17:26 +00:00
const newAmount = Math.round(amount) === 0 ? 0 : amount // handle -0 case
return (
2022-03-10 21:02:30 +00:00
ENV_CONFIG.moneyMoniker + ' ' + formatter.format(newAmount).replace('$', '')
)
}
export function formatWithCommas(amount: number) {
return formatter.format(amount).replace('$', '')
2021-12-15 07:41:50 +00:00
}
export function formatPercent(zeroToOne: number) {
return Math.round(zeroToOne * 100) + '%'
}
2022-01-22 21:47:24 +00:00
export function toCamelCase(words: string) {
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('')
// 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
}