import { InternetObjectValidationError , ErrorArgs , InternetObjectError } from '../errors/io-error' ; import ErrorCodes from '../errors/io-error-codes' ; import { ParserTreeValue , Node } from '../parser/index' ; import { isDateTimeString , isDateString , isTimeString , isToken , isString } from '../utils/is' ; import MemberDef from './memberdef' ; import TypeDef from './typedef' ; import { doCommonTypeCheck } from './utils' ; import KeyValueCollection from '../header/index' ; import { parseDateTime , parseDate , parseTime , dateToDatetimeString , dateToDateString , dateToTimeString } from '../utils/datetime' ; const DATETIME_TYPES = [ 'datetime' , 'date' , 'time' ] ; internal class DateTimeDef implements TypeDef { private _type : string ; constructor ( type = 'datetime' ) { this . _type = type ; } getType ( ) { return this . _type ; } parse ( data , memberDef , vars ? ) { const parse = _getParser ( this . _type ) ; const ofMatchingType = _getTypeChecker ( this . _type ) ; const node = isToken ( data ) ? data : undefined ; let value = node ? node . value : data ; const validatedData = doCommonTypeCheck ( memberDef , value , node ) ; if ( validatedData === undefined || validatedData === null ) { return validatedData ; } if ( vars && isString ( value ) && value . startsWith ( '$' ) ) { const replaced = vars . getV ( value ) ; value = replaced !== undefined ? replaced : value ; } let parsed = false ; if ( ofMatchingType ( value ) ) { value = parse ( value ) ; parsed = true ; } if ( parsed === false || value === null ) { throw new InternetObjectValidationError ( ErrorCodes . invalidDateTime , `Expecting the value of type '${ this . _type }'` ) ; } return value ; } load ( data , memberDef ) { const value = doCommonTypeCheck ( memberDef , data ) ; if ( value === undefined || value === null ) { return value ; } if ( value instanceof Date === false ) { throw new InternetObjectValidationError ( ErrorCodes . invalidDateTime , `Expecting the value of type '${ this . _type }'` ) ; } return value ; } serialize = ( data , memberDef ) : string => { const validatedData = doCommonTypeCheck ( memberDef , data ) ; if ( validatedData === undefined ) return '' ; else if ( validatedData === null ) return 'N' ; if ( data instanceof Date ) { return _getSerializer ( this . _type ) ( data ) ; } throw new InternetObjectValidationError ( ErrorCodes . invalidDateTime , `Expecting the value of type '${ this . _type }'` ) ; } ; } function _serializeDateTime ( date ) { return dateToDatetimeString ( date , true , true ) || '' ; } function _serializeDate ( date ) { return dateToDateString ( date , true ) || '' ; } function _serializeTime ( date ) { return dateToTimeString ( date , true ) || '' ; } function _getSerializer ( type ) { if ( type === 'datetime' ) { return _serializeDateTime ; } else if ( type === 'date' ) { return _serializeDate ; } return _serializeTime ; } function _getParser ( type ) { if ( type === 'datetime' ) { return parseDateTime ; } else if ( type === 'date' ) { return parseDate ; } return parseTime ; } function _getTypeChecker ( type ) { if ( type === 'datetime' ) { return isDateTimeString ; } else if ( type === 'date' ) { return isDateString ; } return isTimeString ; } export default DateTimeDef ;