Nodejs String Format format(substitutions)

Here you can find the source of format(substitutions)

Method Source Code

// Do simple substitutions on any String object. (Monkeypatching FTW.)
// Accepts either an array or an object of subsitutions.
// If you supply an Array:
//   `'Hello {0}.'.format(['world'])` will return: 'Hello world.'
// If you supply an Object:
//   `'Hello {world}.'.format({world: 'Earth'})` will return: 'Hello Earth.'
String.prototype.format = function(substitutions) {
  return this.replace(/\{([A-Za-z0-9]+)\}/g, function(match, key) {
    return substitutions[key] !== undefined ? substitutions[key] : '';
  });//from  www  .  java  2  s . com
};

Related

  1. format(params)
    String.prototype.format = function(params) {
      if (!params) {throw new Error('No arguments passed');}
      return this.replace(/\{([a-zA-Z0-9]+)\}/g, function() {
        var key = arguments[1];
        if (params.hasOwnProperty(key)) {
          return params[key];
        } else {
          return arguments[0];
      });
    };
    
  2. format(params)
    String.prototype.format = function(params){
      var formatted = this;
      var fn = function(key, value){
        var regexp = new RegExp('\\{' + key + '\\}', 'gi');
        formatted = formatted.replace(regexp, value);
      };
      if (typeof(params) == 'object') {
        for (var x in params) {
          fn(x, params[x]);
    ...
    
  3. format(params)
    String.prototype.format = function (params) {
        'use strict';
        var p,
            regex,
            result;
        result = this;
        for (p in params) {
            if (params.hasOwnProperty(p)) {
                regex = new RegExp('#{' + p + '}', 'g');
    ...
    
  4. format(params)
    String.prototype.format = function (params){
        var reg = /{(\d+)}/gm;
        return this.replace(reg,function(match,name){
            return params[~~name];}
        );
    };
    
  5. format(string)
    function ms_to_days(m) {
      return Math.floor(m / (86400 * 1000) + 0.5)
    function date_period_to_ms(dp) {
      var ym = dp.split('-', 2)
      return new Date(ym[0], parseInt(ym[1], 10) - 1, 1);
    Date.prototype.format = function (string) {  
      string = string.replace(/%b/, I18n.t('date.abbr_month_names')[this.getMonth() + 1], 'g');
    ...
    
  6. format(substitutions)
    String.prototype.format = function(substitutions) {
      return this.replace(/\{([A-Za-z0-9]+)\}/g, function(match, key) { 
        return substitutions[key] !== undefined ? substitutions[key] : '';
      });
    };
    
  7. 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] : ''
      })
    
  8. 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] : '';
        });
    };
    
  9. 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";
    ...