<commit_before>// really basic version of ./expander.js. focused solely
// on string replacements within objects/arrays/strings.

var kindOf = require('../lang/kindOf')
var get = require('../object/get')

function interpolate (template, replacements, syntax) {
  return resolve(template, replacements || template, syntax)

  function resolve (template, data, opts) {
    switch(kindOf(template)) {
      case 'string': return resolveString(template, data, opts)
      case 'object': return resolveObject(template, data, opts)
      case 'array' : return resolveArray(template, data, opts)
      default      : return template
    }
  }

  function resolveObject (obj, data, opts) {
    Object.keys(obj).forEach(function (key, idx, arr) {
      obj[key] = resolve(obj[key], data, opts)
    })
    return obj
  }

  function resolveArray (arr, data, opts) {
    var len = arr.length
    var i = - 1
    while (++i < len) arr[i] = resolve(arr[i], data, opts)
    return arr
  }

  function resolveString (str, data, opts) {
    var stache = opts || /\{\{([^\}]+)\}\}/g // mustache-like

    function replaceFn (match, prop) {
      var template = String(get(data, prop))

      return stache.test(template)
        ? template.replace(stache, replaceFn)
        : template
    }

    return str.replace(stache, replaceFn)
  }
}

module.exports = interpolate<commit_msg>refactor string/expand.js<commit_after>// really basic version of ./expander.js. focused solely
// on string replacements within objects/arrays/strings.

var kindOf = require('../lang/kindOf')
var look   = require('../object/look')

function interpolate (template, replacements, syntax) {
  return resolve(template, replacements || template, syntax)

  function resolve (template, data, opts) {
    switch (kindOf(template)) {
      case 'array' :
      case 'object': return resolveObject(template, data, opts)
      case 'string': return resolveString(template, data, opts)
      default      : return template
    }
  }

  function resolveObject (obj, data, opts) {
    Object.keys(obj).forEach(function (key, idx, arr) {
      obj[key] = resolve(obj[key], data, opts)
    })
    return obj
  }

  function resolveString (str, data, opts) {
    var stache = opts || /\{\{([^\}]+)\}\}/g // mustache-like

    function replaceFn (match, prop) {
      var template = look(data, prop)

      return stache.test(template)
        ? template.replace(stache, replaceFn)
        : template
    }

    return str.replace(stache, replaceFn)
  }
}

module.exports = interpolate
{'fraction_non_alphanumeric': 0.13496448303078137, 'fraction_numerical': 0.0003946329913180742, 'mean_word_length': 3.0953150242326335, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, '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': '6919494', 'n_tokens_mistral': 775, 'n_tokens_neox': 742, 'n_words': 215}