import { ODATA_TYPE } from "../utils/constants" ; import { AnnotationLevel } from "./EntityProperty" ; import { IEdmType } from "./IEdmType" ; export class EdmDouble implements IEdmType { public static validate ( value ) { if ( value === "NaN" || value === "Infinity" || value === "-Infinity" ) { return value ; } if ( typeof value === "string" ) { const val = Number . parseFloat ( value ) ; if ( ! Number . isNaN ( val ) ) { return val ; } } if ( typeof value !== "number" ) { throw TypeError ( `Not a valid EdmDouble string.` ) ; } return value ; } public typedValue : number | string ; public constructor ( public value ) { this . typedValue = EdmDouble . validate ( value ) ; } public toJsonPropertyValuePair ( name ) { return [ name , this . value ] ; } public toJsonPropertyValueString ( name ) { if ( typeof this . typedValue === "number" ) { return `"${ name }":${ Number . isInteger ( this . value ) ? this . typedValue . toFixed ( 1 ) : this . value }` ; } else { return `"${ name }":${ JSON . stringify ( this . typedValue ) }` ; } } public toJsonPropertyTypePair ( name , annotationLevel , isSystemProperty , force = false ) { if ( isSystemProperty ) { throw RangeError ( `EdmDouble type shouldn't be a system property.` ) ; } if ( force || ( typeof this . typedValue === "string" && ( annotationLevel === AnnotationLevel . MINIMAL || annotationLevel === AnnotationLevel . FULL ) ) ) { return [ `${ name }${ ODATA_TYPE }` , "Edm.Double" ] ; } } public toJsonPropertyTypeString ( name , annotationLevel , isSystemProperty ) { const res = this . toJsonPropertyTypePair ( name , annotationLevel , isSystemProperty ) ; if ( ! res ) { return ; } const [ key , value ] = res ; return `"${ key }":"${ value }"` ; } }