<commit_before>// Copyright 2013-2020, University of Colorado Boulder

/**
 * Collection of utility functions related to Strings.
 */

import phetcommon from '../phetcommon.js';

// Unicode embedding marks that we use.
const LTR = '\u202a';
const RTL = '\u202b';
const POP = '\u202c';

var StringUtils = {

  /**
   * NOTE: Please use StringUtils.fillIn instead of this function.
   *
   * http://mobzish.blogspot.com/2008/10/simple-messageformat-for-javascript.html
   * Similar to Java's MessageFormat, supports simple substitution, simple substitution only.
   * The full MessageFormat specification allows conditional formatting, for example to support pluralisation.
   *
   * Example:
   * > StringUtils.format( '{0} + {1}', 2, 3 )
   * "2 + 3"
   *
   * @param {string} pattern pattern string, with N placeholders, where N is an integer
   * @returns {string}
   * @public
   * @deprecated - please use StringUtils.fillIn
   */
  format: function( pattern ) {
    const args = arguments;
    return pattern.replace( /{(\d)}/g, function( r, n ) { return args[ +n + 1 ];} );
  },

  /**
   * Fills in a set of placeholders in a template.
   * Placeholders are specified with pairs of curly braces, e.g. '{{name}} is {{age}} years old'
   * See https://github.com/phetsims/phetcommon/issues/36
   *
   * Example:
   * > StringUtils.fillIn( '{{name}} is {{age}} years old', { name: 'Fred', age: 23 } )
   * "Fred is 23 years old"
   *
   * @param {string} template - the template, containing zero or more placeholders
   * @param {Object} values - a hash whose keys correspond to the placeholder names, e.g. { name: 'Fred', age: 23 }
   *                          Unused keys are silently ignored. All placeholders do not need to be filled.
   * @returns {string}
   * @public
   */
  fillIn: function( template, values ) {

    // To catch attempts to use StringUtils.fillIn like StringUtils.format
    assert && assert( values && typeof values === 'object', 'invalid values: ' + values );

    let newString = template;

    // {string[]} parse out the set of placeholders
    const placeholders = template.match( /\{\{[^{}]+\}\}/g ) || [];

    // replace each placeholder with its corresponding value
    for ( let i = 0; i < placeholders.length; i++ ) {
      const placeholder = placeholders[ i ];

      // key is the portion of the placeholder between the curly braces
      const key = placeholder.replace( '{{', '' ).replace( '}}', '' );
      if ( values[ key ] !== undefined ) {
        newString = newString.replace( placeholder, values[ key ] );
      }
    }

    return newString;<commit_msg>validate template arg to StringUtils.fillIn<commit_after>// Copyright 2013-2020, University of Colorado Boulder

/**
 * Collection of utility functions related to Strings.
 */

import phetcommon from '../phetcommon.js';

// Unicode embedding marks that we use.
const LTR = '\u202a';
const RTL = '\u202b';
const POP = '\u202c';

var StringUtils = {

  /**
   * NOTE: Please use StringUtils.fillIn instead of this function.
   *
   * http://mobzish.blogspot.com/2008/10/simple-messageformat-for-javascript.html
   * Similar to Java's MessageFormat, supports simple substitution, simple substitution only.
   * The full MessageFormat specification allows conditional formatting, for example to support pluralisation.
   *
   * Example:
   * > StringUtils.format( '{0} + {1}', 2, 3 )
   * "2 + 3"
   *
   * @param {string} pattern pattern string, with N placeholders, where N is an integer
   * @returns {string}
   * @public
   * @deprecated - please use StringUtils.fillIn
   */
  format: function( pattern ) {
    const args = arguments;
    return pattern.replace( /{(\d)}/g, function( r, n ) { return args[ +n + 1 ];} );
  },

  /**
   * Fills in a set of placeholders in a template.
   * Placeholders are specified with pairs of curly braces, e.g. '{{name}} is {{age}} years old'
   * See https://github.com/phetsims/phetcommon/issues/36
   *
   * Example:
   * > StringUtils.fillIn( '{{name}} is {{age}} years old', { name: 'Fred', age: 23 } )
   * "Fred is 23 years old"
   *
   * @param {string} template - the template, containing zero or more placeholders
   * @param {Object} values - a hash whose keys correspond to the placeholder names, e.g. { name: 'Fred', age: 23 }
   *                          Unused keys are silently ignored. All placeholders do not need to be filled.
   * @returns {string}
   * @public
   */
  fillIn: function( template, values ) {
    assert && assert( typeof template === 'string', `invalid template: ${template}` );

    // To catch attempts to use StringUtils.fillIn like StringUtils.format
    assert && assert( values && typeof values === 'object', 'invalid values: ' + values );

    let newString = template;

    // {string[]} parse out the set of placeholders
    const placeholders = template.match( /\{\{[^{}]+\}\}/g ) || [];

    // replace each placeholder with its corresponding value
    for ( let i = 0; i < placeholders.length; i++ ) {
      const placeholder = placeholders[ i ];

      // key is the portion of the placeholder between the curly braces
      const key = placeholder.replace( '{{', '' ).replace( '}}', '' );
      if ( values[ key ] !== undefined ) {
        newString = newString.replace( placeholder, values[ key ] );
      }
    }

    return newString;
{'fraction_non_alphanumeric': 0.13373290878441657, 'fraction_numerical': 0.01460947743023038, 'mean_word_length': 2.912087912087912, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '883396', 'n_tokens_mistral': 1633, 'n_tokens_neox': 1526, 'n_words': 557}