import { isSingleQuoted , removeDoubleQuotes , removeSingleQuotes } from "../../util/strings" ; import { isTleExpression } from "./isTleExpression" ; import { Parser } from "./TLE" ; example example example export function getFriendlyExpressionFromJsonString ( jsonString ) { if ( isTleExpression ( jsonString ) ) { const quotedBracketedJsonString = `"${ jsonString }"` ; return getFriendlyExpressionFromTleExpressionCore ( quotedBracketedJsonString ) ; } return jsonString ; } example example example export function getFriendlyExpressionFromTleExpression ( tleExpression ) { if ( isSingleQuoted ( tleExpression ) ) { return removeSingleQuotes ( tleExpression ) ; } const quotedBracketedJsonString = `"[${ tleExpression }]"` ; return getFriendlyExpressionFromTleExpressionCore ( quotedBracketedJsonString ) ; } function getFriendlyExpressionFromTleExpressionCore ( doubleQuotedBracketedJsonString ) { const pr = Parser . parse ( doubleQuotedBracketedJsonString ) ; if ( pr . expression && pr . errors . length === 0 ) { const friendlyExpression = pr . expression . format ( { friendly : true } ) ; if ( isSingleQuoted ( friendlyExpression ) ) { return removeSingleQuotes ( friendlyExpression ) ; } else if ( isParamOrVarInterpolation ( friendlyExpression ) ) { return friendlyExpression ; } else { return `[${ friendlyExpression }]` ; } } else { const unquotedExpression = removeDoubleQuotes ( doubleQuotedBracketedJsonString ) ; return unquotedExpression . replace ( /(\bvariables\b|\bparameters\b)\('([^']+)'\)/gi , '$${$2}' ) ; } } /**
 * Returns true only if the given expression is a parameter or variable interpolation such as "${var}"
 */ export function isParamOrVarInterpolation ( s ) { return ! ! s . match ( /^\${[^{}]+}$/ ) ; }