import { raw } from '../../resources/query' ; import { Parser , StructuredTypeFieldOptions } from '../../types' ; import { ArrayBuffers } from '../../utils/arraybuffers' ; import { Duration , Durations } from '../../utils/durations' ; const EdmParser = < T > ( _d , _s , _e ) : Parser < T | T [ ] > => ( { deserialize ( value , options ) { return Array . isArray ( value ) ? value . map ( ( v ) => _d ( v , options ) ) : _d ( value , options ) ; } , serialize ( value , options ) { return Array . isArray ( value ) ? value . map ( ( v ) => _s ( v , options ) ) : _s ( value , options ) ; } , encode ( value , options ) { return Array . isArray ( value ) ? value . map ( ( v ) => _e ( v , options ) ) : _e ( value , options ) ; } , } ) ; const Identity = ( v ) => v ; const toNumber = ( v ) => Number ( v ) ; const toString = ( v ) => v . toString ( ) ; const toBoolean = ( v ) => Boolean ( v ) ; const toDate = ( v ) => new Date ( v ) ; export const EDM_PARSERS = { 'Edm.Guid' : EdmParser < string > ( Identity , Identity , ( v ) => raw ( v ) ) , 'Edm.Int16' : EdmParser < number > ( toNumber , toNumber , toNumber ) , 'Edm.String' : EdmParser < string > ( toString , toString , toString ) , 'Edm.Boolean' : EdmParser < boolean > ( toBoolean , toBoolean , toBoolean ) , 'Edm.Byte' : EdmParser < number > ( toNumber , toNumber , toNumber ) , 'Edm.SByte' : EdmParser < number > ( toNumber , toNumber , toNumber ) , 'Edm.Int32' : EdmParser < number > ( toNumber , toNumber , toNumber ) , 'Edm.Int64' : EdmParser < number > ( toNumber , toNumber , toNumber ) , 'Edm.Date' : EdmParser < Date > ( ( v ) => new Date ( `${ v }T00:00:00.000Z` ) , ( v ) => toDate ( v ) . toISOString ( ) . substring ( 0 , 10 ) , ( v ) => toDate ( v ) . toISOString ( ) . substring ( 0 , 10 ) ) , 'Edm.TimeOfDay' : EdmParser < Date > ( ( v ) => new Date ( `1970-01-01T${ v }Z` ) , ( v ) => toDate ( v ) . toISOString ( ) . substring ( 11 , 23 ) , ( v ) => toDate ( v ) . toISOString ( ) . substring ( 11 , 23 ) ) , 'Edm.DateTimeOffset' : EdmParser < Date > ( toDate , ( v ) => toDate ( v ) . toISOString ( ) , ( v ) => toDate ( v ) . toISOString ( ) ) , 'Edm.Duration' : EdmParser < Duration > ( ( v ) => Durations . toDuration ( v ) , ( v ) => Durations . toString ( v ) , ( v ) => Durations . toString ( v ) ) , 'Edm.Decimal' : EdmParser < number > ( ( v , o ) => { if ( typeof v === 'string' && o . ieee754Compatible ) { return parseFloat ( v ) ; } return v ; } , ( v , o ) => { if ( o . ieee754Compatible ) { return parseFloat ( v . toPrecision ( o . field . precision ) ) . toFixed ( o . field . scale ) ; } return v ; } , ( v , o ) => { if ( o . ieee754Compatible ) { return parseFloat ( v . toPrecision ( o . field . precision ) ) . toFixed ( o . field . scale ) ; } return v ; } ) , 'Edm.Double' : EdmParser < number > ( ( v ) => ( v === 'INF' ? Infinity : ( v as number ) ) , ( v ) => ( v === Infinity ? 'INF' : v ) , ( v ) => ( v === Infinity ? 'INF' : v ) ) , 'Edm.Single' : EdmParser < number > ( ( v ) => ( v === 'INF' ? Infinity : ( v as number ) ) , ( v ) => ( v === Infinity ? 'INF' : v ) , ( v ) => ( v === Infinity ? 'INF' : v ) ) , 'Edm.Binary' : EdmParser < ArrayBuffer > ( ( v ) => ArrayBuffers . toArrayBuffer ( v ) , ( v ) => ArrayBuffers . toString ( v ) , ( v ) => ArrayBuffers . toString ( v ) ) , } ;