<commit_before>'use strict';

const Velocity = require('velocityjs');
const isPlainObject = require('lodash').isPlainObject;

const debugLog = require('./debugLog');
const { polluteStringPrototype, depolluteStringPrototype } = require('./javaHelpers');

const Compile = Velocity.Compile;
const parse = Velocity.parse;

function tryToParseJSON(string) {
  let parsed;
  try {
    parsed = JSON.parse(string);
  }
  catch (err) {
    // nothing! Some things are not meant to be parsed.
  }

  return parsed || string;
}

function renderVelocityString(velocityString, context) {

  // Add Java helpers to String prototype
  polluteStringPrototype();

  // This line can throw, but this function does not handle errors
  // Quick args explanation:
  // { escape: false } --> otherwise would escape &, < and > chars with html (&amp;, &lt; and &gt;)
  // render(context, null, true) --> null: no custom macros; true: silent mode, just like APIG
  const renderResult = (new Compile(parse(velocityString), { escape: false })).render(context, null, true);

  // Remove Java helpers from String prototype
  depolluteStringPrototype();

  debugLog('Velocity rendered:', renderResult || 'undefined');

  // Haaaa Velocity... this language sure loves strings a lot<commit_msg>fix test on node v4 and 5<commit_after>'use strict';

const Velocity = require('velocityjs');
const isPlainObject = require('lodash').isPlainObject;

const debugLog = require('./debugLog');
const stringPollution = require('./javaHelpers');

const Compile = Velocity.Compile;
const parse = Velocity.parse;

function tryToParseJSON(string) {
  let parsed;
  try {
    parsed = JSON.parse(string);
  }
  catch (err) {
    // nothing! Some things are not meant to be parsed.
  }

  return parsed || string;
}

function renderVelocityString(velocityString, context) {

  // Add Java helpers to String prototype
  stringPollution.polluteStringPrototype();

  // This line can throw, but this function does not handle errors
  // Quick args explanation:
  // { escape: false } --> otherwise would escape &, < and > chars with html (&amp;, &lt; and &gt;)
  // render(context, null, true) --> null: no custom macros; true: silent mode, just like APIG
  const renderResult = (new Compile(parse(velocityString), { escape: false })).render(context, null, true);

  // Remove Java helpers from String prototype
  stringPollution.depolluteStringPrototype();

  debugLog('Velocity rendered:', renderResult || 'undefined');

  // Haaaa Velocity... this language sure loves strings a lot
{'fraction_non_alphanumeric': 0.1212001579155152, 'fraction_numerical': 0.0007895775759968417, 'mean_word_length': 4.192622950819672, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3086837', 'n_tokens_mistral': 731, 'n_tokens_neox': 711, 'n_words': 246}