Nodejs String Format format()

Here you can find the source of format()

Method Source Code

String.prototype.format = function () {
   var str = this,
      args = arguments;//w  w  w. j av  a  2  s . c om
   return str.replace(String.prototype.format.regex, function(item) {
      var intVal = parseInt(item.substring(1, item.length - 1)),
         replace;
      if (intVal >= 0) {
         replace = args[intVal];
      } else if (intVal === -1) {
         replace = "{";
      } else if (intVal === -2) {
         replace = "}";
      } else {
         replace = "";
      }
      return replace;
   });
};
String.prototype.format.regex = new RegExp("{-?[0-9]+}", "g");

Related

  1. format()
    String.prototype.format = function () {
        var formatted = this;
        if (arguments != null) {
            for (var i = 0; i < arguments.length; i++) {
                while (formatted.indexOf("{" + i + "}") > -1) {
                    formatted = formatted.replace("{" + i + "}", arguments[i]);
        return formatted;
    };
    
  2. format()
    String.prototype.format = function() {
        var args = arguments;
        return this.replace(/{(\d+)}/g, function(match, number) {
          return typeof args[number] != 'undefined'
            ? args[number]
            : match
            ;
        });
    };
    ...
    
  3. format()
    String.prototype.format = function() {
        var res = this;
        for (var i = 0; i < arguments.length; i++) {
            res = res.replace(new RegExp("\\{" + (i) + "\\}", "g"), arguments[i]);
        return res;
    };
    
  4. format()
    String.prototype.format = function() {
      var args = arguments;
      return this.replace(/\$\{([A-Za-z0-9_]+)\}/g, function(base, element, position) {
        for (var i = 0; i < args.length; i++) {
          if (args[i] != null && typeof(args[i][element]) != "undefined")
            return args[i][element];
        if (typeof(args[element - 1]) == "undefined")
          return base;
    ...
    
  5. format()
    String.prototype.format = function(){
      var fn = String.format;
      switch (arguments.length) {
        case 1:
          return fn(this, arguments[0]);
        case 2:
          return fn(this, arguments[0], arguments[1]);
        case 3:
          return fn(this, arguments[0], arguments[1], arguments[2]);
    ...
    
  6. format()
    String.prototype.format = function() {
      var args = arguments;
      return this.replace(/{(\d+)}/g, function(match, number) { 
        return typeof args[number] != 'undefined'
          ? args[number]
          : match
        ;
      });
    };
    ...
    
  7. format()
    'use strict';
    String.prototype.format = function() {
      var self = this;
      for(var i = 0; i < arguments.length; i += 1) {
        var reg = new RegExp("\\{" + i + "\\}", "gm");
        self = self.replace(reg, arguments[i]);
      return self;
    };
    ...
    
  8. format()
    String.prototype.format = function() {
        var args = arguments;
        return this.replace(/\$\{p(\d)\}/g, (match, id) => {
            return args[id]
        })
    String.prototype.sid = function() {
        return `${this.substring(0, 7)}...`
    
  9. format()
    String.prototype.format = function(){
      return this.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
    };