Nodejs String Format format(values)

Here you can find the source of format(values)

Method Source Code

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+')');
    });/* w  ww .  j a va 2s  .c  o  m*/
};

Date.prototype.format = function(format)
{
   format = format || "yyyy-MM-dd hh:mm:ss";
   var o = {
      "M+" : this.getMonth()+1, //month
      "d+" : this.getDate(),    //day
      "h+" : this.getHours(),   //hour
      "m+" : this.getMinutes(), //minute
      "s+" : this.getSeconds(), //second
      "q+" : Math.floor((this.getMonth()+3)/3), //quarter
      "S" : this.getMilliseconds() //millisecond
   };
   if(/(y+)/.test(format)) {
      format=format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
   }

   for(var k in o) {
      if(new RegExp("("+ k +")").test(format)) {
         format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
      }
   }
   return format;
};

Related

  1. 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');
    ...
    
  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)
    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)
    'use strict';
    String.prototype.format = function(substitutions) {
      return this.replace(/\{([A-Za-z0-9]+)\}/g, function(match, key) {
        return substitutions[key] !== undefined ? substitutions[key] : ''
      })
    
  5. 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] : '';
        });
    };
    
  6. format(variables, values)
    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);
        });
    ...
    
  7. format2DigitStringformat2DigitString()
    Number.prototype.format2DigitString = function format2DigitString(){
      if(this >= 0 && this < 10) {
        return '0' + String(this);
      return String(this);
    
  8. 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;
    };
    
  9. formatForUrl()
    String.prototype.formatForUrl = function () {
      return this.replace(/ /g, "-").replace(/[^a-z0-9\-]/gi, "").toLowerCase();
    };