import { DATE_REGEX , REF_STRING_TOKEN , REGEX_STRING_TOKEN , ESCAPED_STRING_TOKEN , STRING_TOKEN , REFERENCE_HEADER_LENGTH , UNREFERENCED_STRING_TOKEN , REGEX_UNREFERENCED_STRING_TOKEN , ESCAPED_UNREFERENCED_STRING_TOKEN , STRING_IDENT_PREFIX , ESCAPE_CHARACTER , REGEX_ESCAPE_CHARACTER } from "../constants" ; import { Context , InvertedIndex , CompressOptions , Compressors } from "./common" ; import { ZipsonWriter } from "./writer" ; import { compressInteger } from "../util" ; /**
 * Compress string to
 */ export function compressString ( compressors , context , obj , invertedIndex , writer , options ) { let foundRef ; const stringIdent = STRING_IDENT_PREFIX + obj ; if ( options . detectUtcTimestamps && obj [ obj . length - 1 ] === 'Z' && obj . match ( DATE_REGEX ) ) { const date = Date . parse ( obj ) ; compressors . date ( compressors , context , date , invertedIndex , writer , options ) ; } else if ( ( foundRef = invertedIndex . stringMap [ stringIdent ] ) !== void 0 ) { writer . write ( `${ REF_STRING_TOKEN }${ foundRef }` ) ; } else { const ref = compressInteger ( invertedIndex . stringCount ) ; const newRef = `${ STRING_TOKEN }${ obj . replace ( REGEX_ESCAPE_CHARACTER , ESCAPE_CHARACTER + ESCAPE_CHARACTER ) . replace ( REGEX_STRING_TOKEN , ESCAPED_STRING_TOKEN ) }${ STRING_TOKEN }` ; if ( ref . length + REFERENCE_HEADER_LENGTH + 1 < newRef . length ) { invertedIndex . stringMap [ stringIdent ] = ref ; invertedIndex . stringCount ++ ; writer . write ( newRef ) ; } else { writer . write ( `${ UNREFERENCED_STRING_TOKEN }${ obj . replace ( REGEX_ESCAPE_CHARACTER , ESCAPE_CHARACTER + ESCAPE_CHARACTER ) . replace ( REGEX_UNREFERENCED_STRING_TOKEN , ESCAPED_UNREFERENCED_STRING_TOKEN ) }${ UNREFERENCED_STRING_TOKEN }` ) ; } } }