Nodejs String Format format(variables, values)

Here you can find the source of format(variables, values)

Method Source Code

/**//from   w  w w  .  ja  v  a 2s. co  m
 * Applies a template to return a text with the variables substituted.
 * @param variables Array of variable names. Ex: ["%var1%","%var2%"].
 * @param values Array of variable values (in the same order of variables). Ex: ["algorithm", "nothing"].
 */
String.prototype.format = function (variables, values) {
    var textTemplate = this;
    variables.forEach(function(item, index) {
        var value = values[index];
        if (typeof value === 'undefined' || value == null) {
            value = "";
        }
        textTemplate = textTemplate.replaceAll(item, value);
    });
    return textTemplate;
};

Related

  1. format(substitutions)
    String.prototype.format = function(substitutions) {
      return this.replace(/\{([A-Za-z0-9]+)\}/g, function(match, key) {
        return substitutions[key] !== undefined ? substitutions[key] : '';
      });
    };
    
  2. format(substitutions)
    String.prototype.format = function(substitutions) {
      return this.replace(/\{([A-Za-z0-9]+)\}/g, function(match, key) { 
        return substitutions[key] !== undefined ? substitutions[key] : '';
      });
    };
    
  3. format(substitutions)
    'use strict';
    String.prototype.format = function(substitutions) {
      return this.replace(/\{([A-Za-z0-9]+)\}/g, function(match, key) {
        return substitutions[key] !== undefined ? substitutions[key] : ''
      })
    
  4. format(substitutions)
    String.prototype.format = function(substitutions) {
        'use strict';
        return this.replace(/\{([A-Za-z0-9]+)\}/g, function(match, key) {
            return substitutions[key] !== undefined ? substitutions[key] : '';
        });
    };
    
  5. format(values)
    var STRING_FORMAT_REGEX = /\{\{([\w\s\.\(\)\'\",-\[\]]+)?\}\}/g;
    String.prototype.format = function(values) {
        return this.replace(STRING_FORMAT_REGEX, function(match, key) {
          return values[key] || eval('(values.' +key+')');
        });
    };
    Date.prototype.format = function(format)
      format = format || "yyyy-MM-dd hh:mm:ss";
    ...
    
  6. format2DigitStringformat2DigitString()
    Number.prototype.format2DigitString = function format2DigitString(){
      if(this >= 0 && this < 10) {
        return '0' + String(this);
      return String(this);
    
  7. formatDateBrazil(dateString)
    String.prototype.formatDateBrazil = function(dateString) {
        var date = dateString.split("T");
        date = date[0].split("-");
        var date = new Date(date[0], (date[1] - 1), date[2]);
        return date;
    };
    
  8. formatForUrl()
    String.prototype.formatForUrl = function () {
      return this.replace(/ /g, "-").replace(/[^a-z0-9\-]/gi, "").toLowerCase();
    };
    
  9. formatMoney()
    String.prototype.formatMoney = function() {
        return "$" + this.substr(0, this.length - 2) + "." + this.substr(this.length - 2);