packageDocumentation module import { InstanceKey , InstanceKeyJSON } from "../EC" ; import { ValuesDictionary } from "../Utils" ; public export type Value = string | number | boolean | undefined | ValuesMap | ValuesArray | NestedContentValue [ ] ; public export namespace Value { /** Is the value a primitive */ export function isPrimitive ( value ) { return isPrimitiveValue ( value ) ; } /** Is the value an array */ export function isArray ( value ) { return isArrayValue ( value ) ; } /** Is the value a map / struct */ export function isMap ( value ) { return isMapValue ( value ) ; } /** Is the value a nested content value */ export function isNestedContent ( value ) { return isNestedContentValue ( value ) ; } /** Serialize [[Value]] to JSON */ export function fromJSON ( json ) { if ( json === null ) return undefined ; if ( isNestedContentValue ( json ) ) return json . map ( NestedContentValue . fromJSON ) ; if ( isArrayValue ( json ) ) return valuesArrayFromJSON ( json ) ; if ( isMapValue ( json ) ) return valuesMapFromJSON ( json ) ; return json ; } /** Deserialize [[Value]] from JSON */ export function toJSON ( value ) { if ( undefined === value ) return null ; if ( isNestedContentValue ( value ) ) return value . map ( NestedContentValue . toJSON ) ; if ( isArrayValue ( value ) ) return valuesArrayToJSON ( value ) ; if ( isMapValue ( value ) ) return valuesMapToJSON ( value ) ; return value ; } } public export interface ValuesMap extends ValuesDictionary < Value > { } public export interface ValuesArray extends Array < Value > { } public export type DisplayValue = string | undefined | DisplayValuesMap | DisplayValuesArray ; public export namespace DisplayValue { /** Is the value a primitive */ export function isPrimitive ( value ) { return isPrimitiveValue ( value ) ; } /** Is the value an array */ export function isArray ( value ) { return isArrayValue ( value ) ; } /** Is the value a map / struct */ export function isMap ( value ) { return isMapValue ( value ) ; } /** Serialize [[DisplayValue]] to JSON */ export function fromJSON ( json ) { if ( json === null ) return undefined ; if ( isArrayValue ( json ) ) return displayValuesArrayFromJSON ( json ) ; if ( isMapValue ( json ) ) return displayValuesMapFromJSON ( json ) ; return json ; } /** Deserialize [[DisplayValue]] from JSON */ export function toJSON ( value ) { if ( undefined === value ) return null ; if ( isArrayValue ( value ) ) return displayValuesArrayToJSON ( value ) ; if ( isMapValue ( value ) ) return displayValuesMapToJSON ( value ) ; return value ; } } public export interface DisplayValuesMap extends ValuesDictionary < DisplayValue > { } public export interface DisplayValuesArray extends Array < DisplayValue > { } public export interface NestedContentValue { /** Keys of instances whose content is contained in this value */ primaryKeys : InstanceKey [ ] ; /** Content values map */ values : ValuesDictionary < Value > ; /** Content display values map */ displayValues : ValuesDictionary < DisplayValue > ; /** Names of fields whose values are merged */ mergedFieldNames : string [ ] ; } public export namespace NestedContentValue { /** Serialize [[NestedContentValue]] to JSON */ export function toJSON ( json ) { return { primaryKeys : json . primaryKeys . map ( InstanceKey . toJSON ) , values : valuesMapToJSON ( json . values ) , displayValues : displayValuesMapToJSON ( json . displayValues ) , mergedFieldNames : json . mergedFieldNames } ; } /** Deserialize [[NestedContentValue]] from JSON */ export function fromJSON ( json ) { return { primaryKeys : json . primaryKeys . map ( InstanceKey . fromJSON ) , values : valuesMapFromJSON ( json . values ) , displayValues : displayValuesMapFromJSON ( json . displayValues ) , mergedFieldNames : json . mergedFieldNames } ; } } public export type ValueJSON = string | number | boolean | null | ValuesMapJSON | ValuesArrayJSON | NestedContentValueJSON [ ] ; public export interface ValuesMapJSON extends ValuesDictionary < ValueJSON > { } public export interface ValuesArrayJSON extends Array < ValueJSON > { } public export type DisplayValueJSON = string | null | DisplayValuesMapJSON | DisplayValuesArrayJSON ; public export interface DisplayValuesMapJSON extends ValuesDictionary < DisplayValueJSON > { } public export interface DisplayValuesArrayJSON extends Array < DisplayValueJSON > { } public export interface NestedContentValueJSON { primaryKeys : InstanceKeyJSON [ ] ; values : ValuesDictionary < ValueJSON > ; displayValues : ValuesDictionary < DisplayValueJSON > ; mergedFieldNames : string [ ] ; } public export interface DisplayValueGroup { /** Common display value for all grouped raw values */ displayValue : DisplayValue ; /** A list of grouped raw values */ groupedRawValues : Value [ ] ; } public export interface DisplayValueGroupJSON { displayValue : DisplayValueJSON ; groupedRawValues : ValueJSON [ ] ; } public export namespace DisplayValueGroup { /** Serialize [[DisplayValueGroup]] to JSON */ export function toJSON ( group ) { return { displayValue : DisplayValue . toJSON ( group . displayValue ) , groupedRawValues : group . groupedRawValues . map ( Value . toJSON ) } ; } /** Deserialize [[DisplayValueGroup]] from JSON */ export function fromJSON ( json ) { return { displayValue : DisplayValue . fromJSON ( json . displayValue ) , groupedRawValues : json . groupedRawValues . map ( Value . fromJSON ) } ; } } function isNestedContentValue ( v ) { return ( v !== undefined ) && Array . isArray ( v ) && ( ( v . length === 0 ) || ( v [ 0 ] as NestedContentValue ) . primaryKeys !== undefined && ( v [ 0 ] as NestedContentValue ) . values !== undefined && ( v [ 0 ] as NestedContentValue ) . displayValues !== undefined && ( v [ 0 ] as NestedContentValue ) . mergedFieldNames !== undefined ) ; } function isArrayValue ( v ) { return ( v !== undefined ) && Array . isArray ( v ) ; } function isMapValue ( v ) { return ( v !== undefined ) && ( typeof v === "object" ) && ! Array . isArray ( v ) ; } function isPrimitiveValue ( v ) { return ! isArrayValue ( v ) && ! isMapValue ( v ) ; } function valuesArrayFromJSON ( json ) { return json . map ( Value . fromJSON ) ; } function valuesArrayToJSON ( values ) { return values . map ( Value . toJSON ) ; } function valuesMapFromJSON ( json ) { const map = { } ; for ( const key in json ) { if ( json . hasOwnProperty ( key ) ) { map [ key ] = Value . fromJSON ( json [ key ] ) ; } } return map ; } function valuesMapToJSON ( values ) { const map = { } ; for ( const key in values ) { if ( values . hasOwnProperty ( key ) ) { map [ key ] = Value . toJSON ( values [ key ] ) ; } } return map ; } function displayValuesArrayFromJSON ( json ) { return json . map ( DisplayValue . fromJSON ) ; } function displayValuesArrayToJSON ( values ) { return values . map ( DisplayValue . toJSON ) ; } function displayValuesMapFromJSON ( json ) { const map = { } ; for ( const key in json ) { if ( json . hasOwnProperty ( key ) ) { map [ key ] = DisplayValue . fromJSON ( json [ key ] ) ; } } return map ; } function displayValuesMapToJSON ( values ) { const map = { } ; for ( const key in values ) { if ( values . hasOwnProperty ( key ) ) { map [ key ] = DisplayValue . toJSON ( values [ key ] ) ; } } return map ; }