import { extractJSONStringValues } from './parser' ; export function collapseLiteralStrings ( data ) { const splitData = data . split ( `"""` ) ; for ( let idx = 1 ; idx < splitData . length - 1 ; idx += 2 ) { splitData [ idx ] = JSON . stringify ( splitData [ idx ] ) ; } return splitData . join ( '' ) ; } const MAX_EXPANDABLE_JSON_SIZE = 5 * 1024 * 1024 ; /**
 * Takes in a string representing some JSON data and expands strings,
 * where needed, to a string literal representation.
 *
 * For example; given a value like: "{ "my_string": "\nhey!\n" }"
 *
 * Will return: "{ "my_string": """
 * hey!
 * """
 * }"
 */ export function expandLiteralStrings ( data ) { if ( data . length > MAX_EXPANDABLE_JSON_SIZE ) { return data ; } const { stringValues } = extractJSONStringValues ( data ) ; if ( stringValues . length === 0 ) { return data ; } let result = data . substring ( 0 , stringValues [ 0 ] . startIndex ) ; for ( let x = 0 ; x < stringValues . length ; x ++ ) { const { startIndex , endIndex } = stringValues [ x ] ; const candidate = data . substring ( startIndex , endIndex + 1 ) ; const skip = ( candidate [ 1 ] === '\\' && candidate [ 2 ] === '"' ) || ( candidate [ candidate . length - 2 ] === '"' && candidate [ candidate . length - 3 ] === '\\' ) ; if ( ! skip && candidate . match ( /\\./ ) ) { result += `"""${ JSON . parse ( candidate ) }"""` ; } else { result += candidate ; } if ( stringValues [ x + 1 ] ) { result += data . substring ( endIndex + 1 , stringValues [ x + 1 ] . startIndex ) ; } } result += data . substring ( stringValues [ stringValues . length - 1 ] . endIndex + 1 ) ; return result ; }