import { OBJECT_START_TOKEN , OBJECT_END_TOKEN } from "../constants" ; import { Context , InvertedIndex , CompressOptions , Compressors } from "./common" ; import { ZipsonWriter } from "./writer" ; import { isObject } from "./util" ; /**
 * Compress object to writer
 */ export function compressObject ( compressors , context , obj , invertedIndex , writer , options ) { writer . write ( OBJECT_START_TOKEN ) ; const keys = Object . keys ( obj ) ; let templateObject = new compressors . template . Object ( obj [ keys [ 0 ] ] , obj [ keys [ 1 ] ] ) ; if ( templateObject . isTemplating ) { templateObject . compressTemplate ( compressors , context , invertedIndex , writer , options ) ; } for ( let i = 0 ; i < keys . length ; i ++ ) { if ( i > 1 && templateObject . isTemplating ) { templateObject . isNextTemplateable ( obj [ keys [ i ] ] , writer ) ; } if ( templateObject . isTemplating ) { compressors . string ( compressors , context , keys [ i ] , invertedIndex , writer , options ) ; templateObject . compressTemplateValues ( compressors , context , invertedIndex , writer , options , obj [ keys [ i ] ] ) ; } else { const key = keys [ i ] ; const val = obj [ key ] ; if ( val !== undefined ) { compressors . string ( compressors , context , key , invertedIndex , writer , options ) ; compressors . any ( compressors , context , val , invertedIndex , writer , options ) ; } } } ; if ( templateObject . isTemplating ) { templateObject . end ( writer ) ; } writer . write ( OBJECT_END_TOKEN ) ; }