import { imageBaseUrl , imageType , hd2 , hd3 , weekList , } from '../constants/constants' ; param { String } title returns { String } export const formatJSONDate = ( jsonDate ) : string => new Date ( + new Date ( new Date ( jsonDate ) . toJSON ( ) ) + 8 * 3600 * 1000 ) . toISOString ( ) . replace ( /T/g , ' ' ) . replace ( /\.[\d]{3}Z/ , '' ) ; param { String } title returns { String } export const hourTo12 = ( date ) : string => { if ( date ) { const oDate = new Date ( date ) ; const month = oDate . getUTCMonth ( ) + 1 ; const day = oDate . getUTCDate ( ) ; const hour = oDate . getUTCHours ( ) ; const minute = oDate . getMinutes ( ) < 10 ? `0${ oDate . getMinutes ( ) }` : oDate . getMinutes ( ) ; if ( hour > 12 ) { return `${ month }/${ day }, ${ hour - 12 }:${ minute } PM` ; } else { return `${ month }/${ day }, ${ hour }:${ minute } AM` ; } } else { const oDate = new Date ( ) ; const month = oDate . getMonth ( ) + 1 ; const day = oDate . getDate ( ) ; const hour = oDate . getHours ( ) ; const minute = oDate . getMinutes ( ) < 10 ? `0${ oDate . getMinutes ( ) }` : oDate . getMinutes ( ) ; if ( hour > 12 ) { return `${ month }/${ day }, ${ hour - 12 }:${ minute } PM` ; } else { return `${ month }/${ day }, ${ hour }:${ minute } AM` ; } } } ; param { String } title returns { String } export const hourTo12Lite = ( hour ) => { if ( hour > 12 ) { return `${ hour - 12 } PM` ; } else { return `${ hour } AM` ; } } ; param { String } number returns { String } export const upperFirstLetter = ( str ) : string => { if ( str . includes ( '_' ) ) { str = str . split ( '_' ) . join ( ' ' ) ; } return str . toLowerCase ( ) . replace ( /( |^)[a-z]/g , L => L . toUpperCase ( ) ) ; } ; param { String } type param { String | Number } iconName returns { String } export const getImageUrl = ( type , iconName ) : string => { const base = `${ imageBaseUrl }/${ imageType [ type ] }` ; if ( type === 'Temperature' ) { return `${ base }/60x60/${ iconName }${ hd2 }` ; } else if ( type === 'Precipitation' ) { return `${ base }/54x60/rain_ico_${ iconName }${ hd2 }` ; } else { return `${ base }/ic_moonphase_${ iconName }${ hd3 }` ; } } ; param { Number } number returns { Number } export const getRainfallIconName = ( number ) : number => parseInt ( ( number / 10 ) . toString ( ) , 10 ) * 10 ; param { String } value returns { String } export const windDirectFormat = ( value ) : string => { if ( typeof value === 'string' ) { const arr = value . match ( /\b(\w)/g ) ; if ( arr ) { if ( arr . length > 1 ) { return arr . join ( '' ) ; } else { return value ; } } } return value ; } ; param { Number } windSpeed returns { Number } export const getWindSpeed = ( windSpeed ) : number => { const windSpeedKey = parseInt ( ( windSpeed / 5 ) . toString ( ) , 10 ) ; let result = 3.5625 ; switch ( true ) { case windSpeedKey === 0 : result = 3.5625 ; break ; case windSpeedKey === 1 : result = result - 0.4375 ; break ; case windSpeedKey === 2 : result = result - 0.4375 * 2 ; break ; case windSpeedKey === 3 : result = result - 0.4375 * 3 ; break ; case windSpeedKey > 3 : result = result - 0.4375 * 4 ; break ; default : result = 3.5625 ; } return result ; } ; param { Number } timestamp returns { String } export const formatSunRiseAndSetDate = ( timestamp ) : string => { let minute = '' ; const hour = new Date ( timestamp * 1000 ) . getUTCHours ( ) % 12 || 12 ; const _minute = new Date ( timestamp * 1000 ) . getUTCMinutes ( ) ; if ( _minute < 10 ) { minute = `0${ _minute }` ; } else { minute = _minute . toString ( ) ; } return `${ hour }:${ minute }` ; } ; param { String } type param { Number } sunrise param { Number } sunset returns { String } export const sunPosition = ( type , sunrise , sunset ) : string | number => { const oDate = new Date ( ) ; const now = oDate . getHours ( ) * 60 * 60 + oDate . getMinutes ( ) * 60 ; const proportion = ( now - sunrise ) / ( sunset - sunrise ) ; if ( proportion < 0.1 || proportion > 0.9 ) { return 0 ; } else { if ( type === 'sun' ) { return ( proportion * 150 + 15 ) . toFixed ( 0 ) ; } else { return ( proportion * 218 - 24 ) . toFixed ( 0 ) ; } } } ; param { Number } weekNum returns { String } export const formatWeek = ( weekNum ) : string => weekList [ weekNum ] ;