import { memoize } from 'lodash' ; import { asDecimal } from './formatters' ; import { Maybe } from '../../typings' ; function asKilobytes ( value ) { return `${ asDecimal ( value / 1000 ) } KB` ; } function asMegabytes ( value ) { return `${ asDecimal ( value / 1e6 ) } MB` ; } function asGigabytes ( value ) { return `${ asDecimal ( value / 1e9 ) } GB` ; } function asTerabytes ( value ) { return `${ asDecimal ( value / 1e12 ) } TB` ; } function asBytes ( value ) { return `${ asDecimal ( value ) } B` ; } const bailIfNumberInvalid = ( cb ) => { return ( val ) => { if ( val === null || val === undefined || isNaN ( val ) ) { return '' ; } return cb ( val ) ; } ; } ; export const getFixedByteFormatter = memoize ( ( max ) => { const formatter = unmemoizedFixedByteFormatter ( max ) ; return bailIfNumberInvalid ( formatter ) ; } ) ; export const asDynamicBytes = bailIfNumberInvalid ( ( value ) => { return unmemoizedFixedByteFormatter ( value ) ( value ) ; } ) ; const unmemoizedFixedByteFormatter = ( max ) => { if ( max > 1e12 ) { return asTerabytes ; } if ( max > 1e9 ) { return asGigabytes ; } if ( max > 1e6 ) { return asMegabytes ; } if ( max > 1000 ) { return asKilobytes ; } return asBytes ; } ;