platform-helpers/string-extensions.js

/**
 * Extends the native String class to allow .NET string concatenation style
 * @module StringExtensions
 * @returns {string} A concatenated string with the replaced values. Keeps the original replacer if no replaceable data is found
 * @example
 * const rootRequire = require('rpcm-root-require');
 * rootRequire('/platform-helpers/string-extensions');
 *
 * const actual = 'The quick brown {0} jumps {1} the {2} dog!'.format('fox', 'over', 'lazy');
 * console.log(actual);
 * // logs: The quick brown fox jumps over the lazy dog!
 */
String.prototype.format = function () {
  return this.replace(/{(\d+)}/g, (match, number) => {
    return typeof arguments[number] !== 'undefined' ? arguments[number] : match;
  });
};