Nodejs String Format format()

Here you can find the source of format()

Method Source Code

String.prototype.format = function () {
    var args = arguments;
    var input = this;        
    var output = "";

    if (input.length > 0) {

        if (args[0] === undefined || args[0] === null) {
            output = "string without any arguments";
        }/*from ww w  .ja v  a 2 s. c  o  m*/
        else {

            if (typeof args[0] === 'string' || args[0] instanceof String) {
                output = "string with " + args[0] + " argument";
            }
            if (args[0] === 1) {
                output = "string with the number " + args[0] + " in it.";
            }
            if (args[0] != null && args[1] != null && args[2] != null) {
                output = "string with " + args[0] + ", " + args[1] + ", " + args[2] + " or more arguments";
            }

        }
    }

    return output;
}

Related

  1. format()
    String.prototype.format = function() {
      var args = Array.prototype.slice.call(arguments);
      var str = this;
      return str.replace(/%s/g, function(match, index) {
        if (str.charAt(index-1) == "%") {
          return match;
        } else {
          return args.shift();
      });
    
  2. format()
    if (!String.prototype.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 args = arguments;
      return this.replace(/\{(\d+)\}/g, function (m, i, o, n) {
        return args[i];
      });
    };
    
  4. format()
    String.prototype.format = function () {
      var i = 0, args = arguments;
      return this.replace(/{(.*?)}/g, function (match, tagInner) {
        replacement = typeof args[i] != 'undefined' ? args[i++] : '';
        if(tagInner == "") {
            return replacement;
        } else {
            var match = tagInner.match(/^:.(\d+)f$/);
            if(match) {
    ...
    
  5. format()
    String.prototype.format = function() {
        var s = this,
            i = arguments.length;
        while (i--) {
            s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
        return s;
    };
    
  6. format()
    String.prototype.format = function() {
      var stringToReturn = this.toString();
      for(var i = 0; i < arguments.length; i++) {
        var regex = new RegExp("\\{" + i + "\\}", "g");
        stringToReturn = stringToReturn.replace(regex, arguments[i]);
      return stringToReturn;
    };
    
  7. format()
    String.format = function() {
        "use strict"
        var theString = arguments[0];
        for (var i = 1; i < arguments.length; i++) {
            var regEx = new RegExp("\\{" + (i - 1) + "\\}", "gm");
            theString = theString.replace(regEx, arguments[i]);
        return theString;
    };
    ...
    
  8. format()
    'use strict';
    String.prototype.format = function () { 
        var content = this;
        for (var i = 0; i < arguments.length; i++) {
            var replacement = '{' + i + '}';
            content = content.replace(replacement, arguments[i]);
        return content;
    };
    ...
    
  9. format(...args)
    String.prototype.format = function (...args) {
      return this.replace(/\{(\d+)\}/g, function (m, i) {
        return args[i]
      })