import { Injectable } from '@angular/core' ; /**
 * Service for formatting various types of data such as lat/longs, angles,
 * dates, et. al
 */ @ Injectable ( ) export class FormatterService { readonly depthDecimals : number ; readonly distanceDecimals : number ; readonly empty : string ; readonly locationDecimals : number ; readonly mmiArray : Array < string > ; readonly mmiColors : Array < string > ; constructor ( ) { this . depthDecimals = 1 ; this . distanceDecimals = 1 ; this . empty = '-' ; this . locationDecimals = 3 ; this . mmiColors = [ '#FFFFFF' , '#FFFFFF' , '#ACD8E9' , '#ACD8E9' , '#83D0DA' , '#7BC87F' , '#F9F518' , '#FAC611' , '#FA8A11' , '#F7100C' , '#C80F0A' , '#C80F0A' , '#C80F0A' ] ; } param angle param decimals returns angle ( angle , decimals ) { if ( angle || angle === 0 ) { return this . number ( angle , decimals ) + '°' ; } else { return this . empty ; } } param date returns date ( date ) { let year , month , day ; if ( ! date || typeof date . getTime !== 'function' ) { return this . empty ; } year = date . getUTCFullYear ( ) ; month = date . getUTCMonth ( ) + 1 ; day = date . getUTCDate ( ) ; if ( month < 10 ) { month = '0' + month ; } if ( day < 10 ) { day = '0' + day ; } return `${ year }-${ month }-${ day }` ; } param date { Number | String | Date } param minutesOffset { Number } param includeMilliseconds { Boolean } returns dateTime ( date , minutesOffset = 0 , includeMilliseconds = false ) { let milliOffset ; if ( date === null || typeof date === 'undefined' ) { return this . empty ; } if ( ! ( date instanceof Date ) ) { date = new Date ( date ) ; } if ( minutesOffset ) { milliOffset = minutesOffset * 60 * 1000 ; date = new Date ( date . getTime ( ) + milliOffset ) ; } return ( this . date ( date ) + ' ' + this . time ( date , includeMilliseconds ) + ' (UTC' + this . timezoneOffset ( minutesOffset ) + ')' ) ; } param depth param units param error return depth ( depth , units ? , error ? ) { let number , uncertainty ; number = this . number ( depth , this . depthDecimals , this . empty , units ) ; uncertainty = this . uncertainty ( error , this . depthDecimals , '' ) ; return number + uncertainty ; } param distance param units returns distance ( distance , units ) { return this . number ( distance , this . distanceDecimals , this . empty , units ) ; } param latitude param decimals { number } returns latitude ( latitude , decimals = this . locationDecimals ) { let latDir , result ; if ( latitude === null || typeof latitude === 'undefined' ) { return this . empty ; } latDir = latitude >= 0 ? 'N' : 'S' ; result = this . number ( Math . abs ( latitude ) , decimals ) ; return `${ result }°${ latDir }` ; } param source param length param pad returns leftPad ( source , length , pad ) { let i , padLength , padding ; padLength = length - source . length ; padding = [ ] ; if ( padLength < 1 ) { return source ; } for ( i = 0 ; i < padLength ; i ++ ) { padding [ i ] = pad ; } return padding . join ( '' ) + source ; } param latitude { Number } param longitude { Number } param decimals { Number } returns location ( latitude , longitude , decimals = this . locationDecimals , separator = ' ' ) { return ( this . latitude ( latitude , decimals ) + separator + this . longitude ( longitude , decimals ) ) ; } param longitude param decimals returns longitude ( longitude , decimals = this . locationDecimals ) { let lngDir , result ; if ( longitude === null || typeof longitude === 'undefined' ) { return this . empty ; } lngDir = longitude >= 0 ? 'E' : 'W' ; result = this . number ( Math . abs ( longitude ) , decimals ) ; return `${ result }°${ lngDir }` ; } param value param type returns magnitude ( value , type ) { if ( ! value && value !== 0 ) { return this . empty ; } return this . number ( value , null , null , type ) ; } param cdi returns mmiColor ( cdi ) { if ( cdi === null || ! cdi ) { return null ; } cdi = Math . round ( cdi ) ; return this . mmiColors [ cdi ] || null ; } param value param decimals param empty { Any } param units { String } returns number ( value , decimals ? , empty = this . empty , units = '' ) { let factor , result ; if ( typeof value !== 'number' ) { value = parseFloat ( value ) ; } if ( ! value && value !== 0 ) { return empty ; } if ( typeof decimals === 'number' ) { factor = Math . pow ( 10 , decimals ) ; result = ( Math . round ( value * factor ) / factor ) . toFixed ( decimals ) ; } else { result = `${ value }` ; } if ( units ) { result += ' ' + units ; } return result ; } param status returns reviewStatus ( status ) { if ( ! status ) { return this . empty ; } return status . toUpperCase ( ) ; } param date param includeMilliseconds { Boolean } returns time ( date , includeMilliseconds = false ) { let hours , minutes , seconds , milliseconds ; if ( ! date ) { return this . empty ; } hours = date . getUTCHours ( ) ; minutes = date . getUTCMinutes ( ) ; seconds = date . getUTCSeconds ( ) ; milliseconds = '' ; if ( hours < 10 ) { hours = '0' + hours ; } if ( minutes < 10 ) { minutes = '0' + minutes ; } if ( seconds < 10 ) { seconds = '0' + seconds ; } if ( includeMilliseconds ) { milliseconds = date . getUTCMilliseconds ( ) ; if ( milliseconds < 10 ) { milliseconds = '.00' + milliseconds ; } else if ( milliseconds < 100 ) { milliseconds = '.0' + milliseconds ; } else { milliseconds = '.' + milliseconds ; } } return `${ hours }:${ minutes }:${ seconds }${ milliseconds }` ; } param offset { Number } returns timezoneOffset ( offset = 0 ) { let hours , minutes , sign ; if ( ! offset || offset === 0 ) { return '' ; } else if ( offset < 0 ) { sign = '-' ; offset *= - 1 ; } else { sign = '+' ; } hours = parseInt ( '' + offset / 60 , 10 ) ; minutes = parseInt ( '' + ( offset % 60 ) , 10 ) ; if ( hours < 10 ) { hours = '0' + hours ; } if ( minutes < 10 ) { minutes = '0' + minutes ; } return `${ sign }${ hours }:${ minutes }` ; } param error param decimals param empty { Any } param units returns uncertainty ( error , decimals , empty = this . empty , units ? ) { let result ; if ( ! error && error !== 0 ) { return empty ; } result = this . number ( error , decimals , null , units ) ; return `&plusmn; ${ result }` ; } }