import { memoize } from 'lodash' ; import moment from 'moment-timezone' ; import { analysePatternForFract , DateNanosFormat , formatWithNanos , } from '../../../common/field_formats/converters/date_nanos_shared' ; import { TextContextTypeConvert } from '../../../common/field_formats/types' ; class DateNanosFormatServer extends DateNanosFormat { textConvert : TextContextTypeConvert = ( val ) => { const pattern = this . param ( 'pattern' ) ; const timezone = this . param ( 'timezone' ) ; const fractPattern = analysePatternForFract ( pattern ) ; const fallbackPattern = this . param ( 'patternFallback' ) ; const timezoneChanged = this . timeZone !== timezone ; const datePatternChanged = this . memoizedPattern !== pattern ; if ( timezoneChanged || datePatternChanged ) { this . timeZone = timezone ; this . memoizedPattern = pattern ; this . memoizedConverter = memoize ( ( value ) => { if ( value === null || value === undefined ) { return '-' ; } let date ; if ( this . timeZone === 'Browser' ) { date = moment . utc ( val ) ; } else { date = moment . utc ( val ) . tz ( this . timeZone ) ; } if ( typeof value !== 'string' && date . isValid ( ) ) { return date . format ( fallbackPattern ) ; } else if ( date . isValid ( ) ) { return formatWithNanos ( date , value , fractPattern ) ; } else { return value ; } } ) ; } return this . memoizedConverter ( val ) ; } ; } export { DateNanosFormatServer as DateNanosFormat } ;