Nodejs String Format format(fmt)

Here you can find the source of format(fmt)

Method Source Code

String.prototype.format = function (fmt) {
    if (fmt==null) fmt = "yyyy-MM-dd";
    var myDate = new Date(this);
    var o = {//from w  w w.  ja  va  2 s  .  co m
        "M+": myDate.getMonth() + 1,
        "d+": myDate.getDate(),
        "h+": myDate.getHours(),
        "m+": myDate.getMinutes(),
        "s+": myDate.getSeconds(),
        "q+": Math.floor((myDate.getMonth() + 3) / 3),
        "S": myDate.getMilliseconds()
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (myDate.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

Related

  1. format(args)
    String.prototype.format = function (args) {
        var newStr = this;
        for (var key in args) {
            newStr = newStr.replace('{' + key + '}', args[key]);
        return newStr;
    
  2. format(args, index)
    String.prototype.format = function(args, index) {
      return this.replace(/{(\w+)}/g, function(match, number) {
        return typeof args[index[number]] != 'undefined'
          ? args[index[number]]
          : match
        ;
      });
    };
    
  3. format(ctx)
    String.prototype.format = function (ctx) {
      return this.replace(/\$\{([\w\.]+)\}/g, function(all, varname) {
        var keys = varname.split('.');
        var val = null;
        for (var i = 0, len = keys.length; i < len; i++) {
          val = ctx[keys[i]];
        return val;
      });
    ...
    
  4. format(data)
    String.prototype.format = function(data) {
        return this.replace(/{(.+?)}/g, function(match, name) {
            return data[name] || match;
        });
    };
    
  5. format(dict)
    String.prototype.format = function(dict) {
      var result = this;
      if(typeof(dict) === "object") {
        Object.keys(dict).forEach(function(key) {
          result = result.replace("{" + key + "}", dict[key]);
        });
        return result;
      var args = [];
    ...
    
  6. format(format)
    String.prototype.format = function (format) {
        const holders = JSON.parse(format),
            regex = new RegExp('(#{(\\w+)})', 'g');
        let transformed = this,
            currentMatch = '';
        while ((currentMatch = regex.exec(this)) !== null) {
            transformed = transformed.replace(currentMatch[1], holders[currentMatch[2]]);
        return transformed;
    ...
    
  7. format(format)
    String.format = function (format) {
        var args = Array.prototype.slice.call(arguments, 1);
        return format.replace(/{(\d+)}/g, function (match, number) {
            return typeof args[number] != 'undefined'
            ? args[number]
            : match;
        });
    };
    String.prototype.format = function () {
    ...
    
  8. format(i, safe, arg)
    String.prototype.format = function(i, safe, arg) {
      function format() {
        var str = this, len = arguments.length+1;
        for (i=0; i < len; arg = arguments[i++]) {
          safe = typeof arg === 'object' ? JSON.stringify(arg) : arg;
          str = str.replace(RegExp('\\{'+(i-1)+'\\}', 'g'), safe);
        return str;
      format.native = String.prototype.format;
      return format;
    }();
    
  9. format(i, safe, arg)
    String.prototype.format = function(i, safe, arg) {
      function format() {
        var str = this, len = arguments.length+1;
        for (i=0; i < len; arg = arguments[i++]) {
          safe = typeof arg === 'object' ? JSON.stringify(arg) : arg;
          str = str.replace(RegExp('\\{'+(i-1)+'\\}', 'g'), safe);
        return str;
      format.native = String.prototype.format;
      return format;
    }();