Nodejs String Format format()

Here you can find the source of format()

Method Source Code

/**/*from   w  ww . j av a2  s  .c o  m*/
 * String?format??,"test {0} js.format('123') => test 123 js"
 * @type {String.f}
 */
String.prototype.format = String.prototype.f = function () {
    var s = this,
        i = arguments.length;

    while (i--) {
        s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
    }
    return s;
};

Related

  1. formatSeconds()
    String.prototype.formatSeconds = function () {
        var sec_num = parseInt(this, 10);
        var days    = Math.floor(sec_num / 86400);
        var hours   = Math.floor((sec_num - (days * 86400)) / 3600);
        var minutes = Math.floor((sec_num - (days * 86400  + hours * 3600)) / 60);
        var seconds = sec_num - (days * 86400) - (hours * 3600) - (minutes * 60);
        var time = '';
        if (days > 0) { time += days + 'd '; }
        time += hours + 'h ' + minutes + 'm ' + seconds + 's';
    ...
    
  2. formatString(input)
    String.prototype.formatString = function(input) {
        return this.replace(/(?:#{(\w+)})/g, function($0, $1) {
            return input[$1];
        });
    };
    var options = {
        name: 'John'
    };
    console.log('Hello, there! Are you #{name}?'.formatString(options));
    ...
    
  3. formatString(num, comma)
    Number.prototype.formatString = function(num, comma) {
      return this.toFixed(num).toString().replace(/\.([0-9]*)$/, comma+"$1");
    
  4. format()
    String.prototype.format = String.prototype.f = function () {
        var s = this,
            i = arguments.length;
        while (i--) {
            s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
        return s;
    };
    
  5. format()
    String.prototype.format = String.prototype.f = function () {
        var args = arguments;
        return this.replace(/\{\{|\}\}|\{(\d+)\}/g, function (m, n) {
            if (m == "{{") { return "{"; }
            if (m == "}}") { return "}"; }
            return args[n];
        });
    };
    
  6. format()
    String.prototype.format = String.prototype.f = function() {
        var s = this,
            i = arguments.length;
        while (i--) {
            s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
        return s;
    };
    
  7. format()
    String.prototype.format = String.prototype.f = function() {
      var s = this,
          i = arguments.length;
      while (i--) {
        s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
      return s;
    };
    
  8. format()
    'use strict';
    String.prototype.format = String.prototype.f = function() {
        var s = this,
            i = arguments.length;
        while (i--) {
            s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
        return s;
    };
    ...
    
  9. format(options)
    String.prototype.format = String.prototype.f = function(options) {
        options = options ? options : [];
        if (typeof options != "object") options = [options];
        var s = this,
            i = options.length;
        while (i--) {
            s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), options[i]);
        return s;
    ...