import TypeDef from './typedef' ; import MemberDef from './memberdef' ; import ErrorCodes from '../errors/io-error-codes' ; import DataParser from '../data/index' ; import KeyValueCollection from '../header/index' ; import { appendPath } from '../utils/index' ; import { TypedefRegistry } from './typedef-registry' ; import { doCommonTypeCheck } from './utils' ; import { InternetObjectError } from '../errors/io-error' ; import { ParserTreeValue } from '../parser/index' ; import { isToken , isParserTree , isString , isBoolean , isNumber , isArray , isPlainObject } from '../utils/is' ; /**
 * Represents the AnyTypeDef which is reponsible for parsing,
 * validating, loading and serializing any values.
 */ export default class AnyDef implements TypeDef { /**
     * Returns the type this instance is going to handle.
     * Always returns "any"
     */ getType ( ) { return 'any' ; } /**
     * Parses any value in IO format into JavaScript.
     */ parse ( data , memberDef , vars ? ) { const validatedData = doCommonTypeCheck ( memberDef , data , data ) ; if ( validatedData !== data || validatedData === null || validatedData === undefined ) { return validatedData ; } if ( isToken ( data ) ) { let value = data . value ; if ( vars ) { const valueFound = vars . getV ( value ) ; value = valueFound || value ; } return value ; } if ( isParserTree ( data ) ) { return DataParser . parse ( data , vars ) ; } console . assert ( false , 'Check this case!' ) ; console . warn ( data , memberDef ) ; } /**
     * Loads the JavaScript values.
     */ load ( data , memberDef ) { const validatedData = doCommonTypeCheck ( memberDef , data ) ; if ( validatedData !== data ) return validatedData ; return data ; } /**
     * Serializes any value from JavaScript into IO format.
     */ serialize ( data , memberDef , isRoot = false ) { return _serialize ( data , memberDef , isRoot ) ; } } const _serialize = ( data , memberDef , isRoot ) : string => { const validatedData = doCommonTypeCheck ( memberDef , data ) ; if ( validatedData !== data ) return validatedData ; if ( isString ( data ) ) { return TypedefRegistry . get ( 'string' ) . serialize ( data , { ... memberDef , type : 'string' } ) ; } if ( isNumber ( data ) ) { return TypedefRegistry . get ( 'number' ) . serialize ( data , { ... memberDef , type : 'number' } ) ; } if ( isBoolean ( data ) ) { return TypedefRegistry . get ( 'bool' ) . serialize ( data , { ... memberDef , type : 'bool' } ) ; } if ( isArray ( data ) ) { return _serializeArray ( data , { ... memberDef } , isRoot ) ; } if ( data === null ) { return 'N' ; } if ( data === undefined ) { return '' ; } if ( isPlainObject ( data ) ) { return _serializeObject ( data , memberDef , isRoot ) ; } throw new InternetObjectError ( ErrorCodes . invalidObject ) ; } ; const _serializeObject = ( data , memberDef , isRoot ) : string => { const serialized = [ ] ; Object . keys ( data ) . forEach ( key => { const value = data [ key ] ; const path = memberDef . path ; serialized . push ( _serialize ( value , { ... memberDef , path : appendPath ( key , path ) } , false ) ) ; } ) ; if ( isRoot ) { return serialized . join ( ',' ) . replace ( /,+$/g , '' ) ; } return `{${ serialized . join ( ',' ) . replace ( /,+$/g , '' ) }}` ; } ; const _serializeArray = ( data , memberDef , isRoot ) : string => { const serialized = [ ] ; const path = memberDef . path ; data . forEach ( ( value ) => { serialized . push ( _serialize ( value , { ... memberDef , path : appendPath ( '[' , path ) } , false ) ) ; } ) ; if ( isRoot ) { return serialized . join ( ',' ) ; } return `[${ serialized . join ( ',' ) }]` ; } ;