Grupo-AFAL/frontend-helpers

View on GitHub
javascript/src/utils/formatters.js

Summary

Maintainability
A
1 hr
Test Coverage
const locale = 'en-US'
const currency = 'USD'
 
export const formatCurrency = number => {
return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(
number
)
}
 
export const formatDecimal = number => {
return new Intl.NumberFormat(locale, {
style: 'decimal',
maximumFractionDigits: 2
}).format(number)
}
 
Similar blocks of code found in 2 locations. Consider refactoring.
export const toFloat = number => {
return parseFloat(number.length === 0 ? '0' : number.replace(/,/g, ''))
}
 
Similar blocks of code found in 2 locations. Consider refactoring.
export const toInt = number => {
return parseInt(number.length === 0 ? '0' : number.replace(/,/g, ''))
}
 
export const toBool = boolean => {
if (['false', 'f', '0'].includes(boolean)) return false
return Boolean(boolean)
}
 
export const indexById = array => {
return array.reduce((acc, item) => {
acc[item.id] = item
return acc
}, {})
}