import KeyValueCollection from './header/index' ; import DataParser from './data/index' ; import Schema from './header/schema' ; import ASTParser from './parser/ast-parser' ; import ErrorCodes from './errors/io-error-codes' ; import { isString } from './utils/is' ; import { SCHEMA } from './parser/constants' ; import { InternetObjectError } from './errors/io-error' ; import InternetObjectSerializer from './serializer/index' ; public export class InternetObject < T = any > { param o { string param schema { string constructor ( o , schema ? ) { let header ; let data ; if ( isString ( o ) ) { const parsed = _parse ( o , schema ) ; header = parsed . header ; data = parsed . data ; } else { const parsed = _parseObject ( o , schema ) ; data = parsed . data ; header = parsed . header ; } this . _data = data ; this . _header = header ; } param callSite param substitutions public static doc ( callSite = { } , ... substitutions ) { const text = _getText ( callSite , substitutions ) ; return new InternetObject ( text ) ; } param callSite param substitutions public static defs ( callSite = { } , ... substitutions ) { const text = _getText ( callSite , substitutions ) ; return KeyValueCollection . compile ( text ) ; } private _header : KeyValueCollection ; /**
     * Gets the header section of the object.
     */ public get header ( ) : KeyValueCollection { return this . _header ; } /**
     * Gets the schema associated with the object.
     */ public get schema ( ) { return this . _header . schema ; } public serialize ( ) { return InternetObjectSerializer . serialize ( this ) ; } private _data : T ; /**
     * Gets the data section of the object
     */ public get data ( ) : T { return this . _data ; } } function _getText ( callSite = { } , ... substitutions ) { let template ; try { template = Array . from ( callSite . raw ) ; } catch ( e ) { throw new TypeError ( 'Cannot convert undefined or null to object' ) ; } let text = template . map ( ( chunk , i ) => { if ( callSite . raw . length <= i ) { return chunk ; } return substitutions [ i - 1 ] !== undefined ? substitutions [ i - 1 ] + chunk : chunk ; } ) . join ( '' ) ; return text ; } function _getCompiledSchema ( schema ? ) { if ( ! schema ) return null ; if ( schema instanceof Schema ) { return schema ; } return Schema . compile ( schema ) ; } function _parseObject ( o , schema ? ) { const compiledSchema = _getCompiledSchema ( schema ) ; if ( compiledSchema === null ) { return { data : o , header : new KeyValueCollection ( ) } ; } return { data : compiledSchema . apply ( o ) , header : new KeyValueCollection ( ) . set ( SCHEMA , compiledSchema ) } ; } function _parse ( text , schema ? ) { const parser = new ASTParser ( text ) ; let compiledSchema = null ; let compiledHeader = null ; parser . parse ( ) ; if ( schema ) { if ( isString ( schema ) ) { compiledSchema = Schema . compile ( schema ) ; } else if ( schema instanceof Schema ) { compiledSchema = schema ; } else { throw new InternetObjectError ( ErrorCodes . invalidSchema ) ; } } let header ; if ( parser . header ) { if ( compiledSchema === null ) { header = KeyValueCollection . compile ( parser . header ) ; } else { header = KeyValueCollection . compile ( parser . header , compiledSchema ) ; } if ( header . schema ) { compiledSchema = header . schema ; } } else { header = compiledSchema === null ? new KeyValueCollection ( ) : new KeyValueCollection ( ) . set ( SCHEMA , compiledSchema ) ; } const data = compiledSchema ? compiledSchema . apply ( parser . data , header ) : DataParser . parse ( parser . data , header ) ; return { header , data } ; }