/**
 * Helper functions for JS reflections.
 */ import { JSON_PROPERTY_DECORATOR_NAME , JsonPropertyDecoratorMetadata } from './DecoratorMetadata' ; declare var Reflect ; /**
 * Reflect Metadata json properties storage name.
 */ export const METADATA_JSON_PROPERTIES_NAME = 'JsonProperties' ; export const METADATA_JSON_IGNORE_NAME = 'JsonIgnore' ; /**
 * Returns the JsonProperty decorator metadata.
 */ export const getJsonPropertyDecoratorMetadata = ( target , key ) : JsonPropertyDecoratorMetadata => { return Reflect . getMetadata ( JSON_PROPERTY_DECORATOR_NAME , target , key ) ; } ; /**
 * Returns the JsonProperty name (if any) associated with the object instance.
 * If any JsonProperty metadata found, it returns the key name as the name of the property.
 */ export const getKeyName = ( target , key ) : string => { const metadata = getJsonPropertyDecoratorMetadata ( target , key ) ; if ( metadata != undefined && metadata . name != undefined ) { return metadata . name ; } else { return key ; } } ; /**
 * Returns the JsonPropertyDecoratorMetadata for the property
 */ export const getJsonPropertyDecorator = ( metadata ) => { return getPropertyDecorator ( JSON_PROPERTY_DECORATOR_NAME , metadata ) ; } ; /**
 * Returns the JsonIgnoreDecoratorMetadata for the property
 */ export const getJsonIgnoreDecorator = ( ) => { return ( target , propertyKey ) => { Reflect . defineMetadata ( METADATA_JSON_IGNORE_NAME , true , target , propertyKey ) ; } ; } ; export const getPropertyDecorator = ( metadataKey , metadata ) => { return Reflect . metadata ( metadataKey , metadata ) ; } ; /**
 * Checks to see if the specified type is a standard JS object type.
 */ export const isSimpleType = ( typeName ) : boolean => { switch ( typeName ) { case Constants . STRING_TYPE : return true ; case Constants . NUMBER_TYPE : return true ; case Constants . BOOLEAN_TYPE : return true ; case Constants . DATE_TYPE : return true ; case Constants . STRING_TYPE_LOWERCASE : return true ; case Constants . NUMBER_TYPE_LOWERCASE : return true ; case Constants . BOOLEAN_TYPE_LOWERCASE : return true ; case Constants . DATE_TYPE_LOWERCASE : return true ; default : return false ; } } ; /**
 * Returns the the instance type name by looking at the constructor name.
 * Stupid IE does not have name property! Hence the hack.
 */ export const extractClassname = ( instance ) : string => { var instr = instance . toString ( ) ; var i1 = instr . indexOf ( 'class' ) ; var i2 = instr . indexOf ( '{' , i1 + 5 ) ; return instr . substring ( i1 + 5 , i2 ) . trim ( ) ; } ; export const getTypeNameFromInstance = ( instance ) : string => { return instance . name || extractClassname ( instance ) ; } ; const getType = ( instance , key ) : any => { return Reflect . getMetadata ( 'design:type' , instance , key ) ; } ; export const isArrayType = ( instance , key ) : boolean => { return Array === getType ( instance , key ) ; } ; export const getTypeName = ( instance , key ) : string => { const type = getType ( instance , key ) ; if ( type != undefined ) { return getTypeNameFromInstance ( type ) ; } return type ; } ; export const Constants = { OBJECT_TYPE : 'Object' , OBJECT_TYPE_LOWERCASE : 'object' , STRING_TYPE : 'String' , STRING_TYPE_LOWERCASE : 'string' , NUMBER_TYPE : 'Number' , NUMBER_TYPE_LOWERCASE : 'number' , BOOLEAN_TYPE : 'Boolean' , BOOLEAN_TYPE_LOWERCASE : 'boolean' , DATE_TYPE : 'Date' , DATE_TYPE_LOWERCASE : 'date' , ARRAY_TYPE : 'Array' , ARRAY_TYPE_LOWERCASE : 'array' , FROM_ARRAY : 'fromArray' } ; export const getCachedType = ( type , cache ) : any => { const typeName = type . getJsonObjectMapperCacheKey != undefined ? type . getJsonObjectMapperCacheKey ( ) : getTypeNameFromInstance ( type ) ; if ( ! cache [ typeName ] ) { cache [ typeName ] = new type ( ) ; } return cache [ typeName ] ; } ;