import Constants from './constants' ; export function booleanToNumber ( value ) { return value ? Constants . TRUE : Constants . FALSE ; } export function dynamicList ( list , select , upperCaseFirstChar = false , title ? ) { removeChildren ( select ) ; const array = title !== undefined ? [ title ] . concat ( list ) : list ; array . forEach ( ( item ) => { const option = document . createElement ( 'option' ) ; option . value = ( title && item === title ) ? '' : item ; option . textContent = upperCaseFirstChar ? upperCaseFirst ( item ) : item ; select . appendChild ( option ) ; } ) ; } export function exportToFile ( dataStr , fileName = 'data.txt' ) { const dataUri = 'data:application/json;charset=utf-8,' + encodeURIComponent ( dataStr ) ; const linkElement = document . createElement ( 'a' ) ; linkElement . setAttribute ( 'href' , dataUri ) ; linkElement . setAttribute ( 'download' , fileName ) ; linkElement . click ( ) ; linkElement . remove ( ) ; } export function formatNumber ( number ) { const length = number . toString ( ) . length ; if ( length <= 3 ) { return number . toString ( ) ; } else if ( length <= 6 ) { const n = ( number / 1000 ) . toPrecision ( ) ; const index = n . indexOf ( '.' ) ; return ( ( index >= - 1 && index <= 1 ) ? n . substr ( 0 , 3 ) : n . substr ( 0 , index ) ) + 'k' ; } else if ( length <= 9 ) { const n = ( number / 1000000 ) . toPrecision ( ) ; const index = n . indexOf ( '.' ) ; return ( ( index >= - 1 && index <= 1 ) ? n . substr ( 0 , 3 ) : n . substr ( 0 , index ) ) + 'M' ; } else { return '1G+' ; } } export function getGlobalVariable ( code , id = 'APFData' ) { const script = document . createElement ( 'script' ) ; script . id = id ; script . textContent = `document.getElementById("${ id }").textContent = JSON.stringify(${ code })` ; document . documentElement . appendChild ( script ) ; const result = document . querySelector ( `script#${ id }` ) . textContent ; script . remove ( ) ; return JSON . parse ( result ) ; } export function getParent ( node , level = 1 ) { if ( ! node ) { return null ; } else if ( level > 0 ) { return getParent ( node . parentElement , level - 1 ) ; } else { return node ; } } export function getVersion ( version ) { const versionValues = version . split ( '.' ) ; return { major : parseInt ( versionValues [ 0 ] ) , minor : parseInt ( versionValues [ 1 ] ) , patch : parseInt ( versionValues [ 2 ] ) } ; } export function hmsToSeconds ( timeStr , precision = 3 ) { const [ hh = '0' , mm = '0' , ss = '0' ] = ( timeStr || '0:0:0' ) . split ( ':' ) ; const hour = parseInt ( hh , 10 ) || 0 ; const minute = parseInt ( mm , 10 ) || 0 ; const second = parseFloat ( ss ) || 0 ; return parseFloat ( ( ( hour * 3600 ) + ( minute * 60 ) + second ) . toFixed ( precision ) ) ; } export function injectScript ( file , node , id = '' ) { const th = document . getElementsByTagName ( node ) [ 0 ] ; const s = document . createElement ( 'script' ) ; s . setAttribute ( 'type' , 'text/javascript' ) ; if ( id ) { s . id = id ; } s . setAttribute ( 'src' , file ) ; th . appendChild ( s ) ; } export function isVersionOlder ( version , minimum ) { if ( version . major < minimum . major ) { return true ; } else if ( version . major == minimum . major && version . minor < minimum . minor ) { return true ; } else if ( version . major == minimum . major && version . minor == minimum . minor && version . patch < minimum . patch ) { return true ; } return false ; } export function makeRequest ( method , url ) { return new Promise ( ( resolve , reject ) => { const xhr = new XMLHttpRequest ( ) ; xhr . open ( method , url ) ; xhr . onload = function ( ) { if ( this . status >= 200 && this . status < 300 ) { resolve ( this . response ) ; } else { reject ( { status : this . status , statusText : this . statusText } ) ; } } ; xhr . onerror = function ( ) { reject ( { status : this . status , statusText : this . statusText } ) ; } ; xhr . send ( ) ; } ) ; } export function numberToBoolean ( value ) { return value > Constants . FALSE ; } export function numberWithCommas ( number ) { if ( typeof Intl == 'object' && typeof Intl . NumberFormat == 'function' ) { if ( typeof number === 'string' ) { number = parseInt ( number ) . toString ( ) ; } return number . toLocaleString ( ) ; } else { number = number . toString ( ) ; const decimalIndex = number . indexOf ( '.' ) ; let output = decimalIndex === - 1 ? number : number . slice ( 0 , decimalIndex ) ; for ( let i = output . length - 3 ; i > 0 ; i -= 3 ) { output = output . slice ( 0 , i ) + ',' + output . slice ( i ) ; } if ( decimalIndex !== - 1 ) { output += number . slice ( decimalIndex ) ; } return output ; } } export function readFile ( file ) { return new Promise ( ( resolve , reject ) => { const fr = new FileReader ( ) ; fr . onload = ( ) => { resolve ( fr . result ) ; } ; fr . readAsText ( file ) ; } ) ; } export function removeChildren ( element ) { if ( element . hasChildNodes ) { while ( element . firstChild ) { element . firstChild . remove ( ) ; } } } export function removeFromArray ( array , element ) { return array . filter ( ( e ) => e !== element ) ; } export function secondsToHMS ( seconds ) { return new Date ( seconds * 1000 ) . toISOString ( ) . substr ( 11 , 12 ) ; } export function upperCaseFirst ( str , lowerCaseRest = true ) { let value = str . charAt ( 0 ) . toUpperCase ( ) ; value += lowerCaseRest ? str . toLowerCase ( ) . slice ( 1 ) : str . slice ( 1 ) ; return value ; }