import * as moment from 'moment' ; export const calculateAge = ( start ) => { const timestampUTC = new Date ( parseInt ( start , 10 ) * 1000 ) ; const nowUTC = new Date ( ) . toUTCString ( ) ; const diffMs = + new Date ( nowUTC ) - + timestampUTC ; const diffDays = Math . floor ( diffMs / 86400000 ) ; const diffHrs = Math . floor ( ( diffMs % 86400000 ) / 3600000 ) ; const diffMins = Math . round ( ( ( diffMs % 86400000 ) % 3600000 ) / 60000 ) ; const diffSecs = Math . round ( diffMs / 1000 ) ; return ! ! diffDays ? diffDays + 'd' : ! ! diffHrs ? diffHrs + 'h' : ! ! diffMins ? diffMins + 'm' : diffSecs + 's' ; } ; export const toKB = ( bytes ) => { let bytesInt ; typeof bytes === 'string' ? ( bytesInt = parseInt ( bytes , 10 ) ) : ( bytesInt = bytes ) ; return bytesInt / 1000 + ' kB' ; } ; export const formatApiDateStrings = ( str ) => { return moment ( str ) . format ( 'YYYY / MM / DD  HH:mm UTC' ) ; } ; export const createReducer = ( initialState , handlers ) => { return ( state = initialState , action ) => { if ( handlers . hasOwnProperty ( action . type ) ) { return handlers [ action . type ] ( state , action ) ; } else { return state ; } } ; } ; export const fetchAsync = ( url , options ? ) => { if ( options == null ) { options = { } ; } return fetch ( url , options ) . then ( ( response ) => { if ( response . status >= 200 && response . status < 300 ) { return Promise . resolve ( response . json ( ) ) ; } else { const error = new Error ( response . statusText || response . status ) ; ( error as any ) . response = response ; return Promise . reject ( error ) ; } } ) ; } ;