import numeral from '@elastic/numeral' ; import { Maybe } from '../../../typings/common' ; import { NOT_AVAILABLE_LABEL } from '../../i18n' ; import { isFiniteNumber } from '../is_finite_number' ; export function asDecimal ( value ? ) { if ( ! isFiniteNumber ( value ) ) { return NOT_AVAILABLE_LABEL ; } return numeral ( value ) . format ( '0,0.0' ) ; } export function asPreciseDecimal ( value ? , dp = 3 ) { if ( ! isFiniteNumber ( value ) ) { return NOT_AVAILABLE_LABEL ; } return numeral ( value ) . format ( `0,0.${ '0' . repeat ( dp ) }` ) ; } export function asInteger ( value ? ) { if ( ! isFiniteNumber ( value ) ) { return NOT_AVAILABLE_LABEL ; } return numeral ( value ) . format ( '0,0' ) ; } export function asPercent ( numerator , denominator , fallbackResult = NOT_AVAILABLE_LABEL ) { if ( ! denominator || ! isFiniteNumber ( numerator ) ) { return fallbackResult ; } const decimal = numerator / denominator ; if ( Math . abs ( decimal ) >= 0.1 || decimal === 0 ) { return numeral ( decimal ) . format ( '0%' ) ; } return numeral ( decimal ) . format ( '0.0%' ) ; } export function asDecimalOrInteger ( value , threshold = 10 ) { if ( value === 0 || value >= threshold ) { return asInteger ( value ) ; } return asDecimal ( value ) ; }