Nodejs String Format format()

Here you can find the source of format()

Method Source Code

//first, checks if it isn't implemented yet
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]/* w  w w.j a va 2s. co  m*/
        : match
      ;
    });
  };
}


Date.prototype.format = function(format) //author: meizz
{
  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;
}


//i wnated to have "hey serge".contains("serge")

String.prototype.contains = function(it) { return this.indexOf(it) != -1; };

Related

  1. format()
    'use strict';
    String.prototype.format = function () {
        var args = arguments;
        var unkeyed_index = 0;
        return this.replace(/\{{1,2}(\w*)\}{1,2}/g, function (match, key) {
            if (match.indexOf('{{') !== -1) { 
                return match.replace('{{', '{').replace('}}', '}');
            if (key === '') { 
    ...
    
  2. format()
    String.prototype.format = function() {
        var newStr = this, i = 0;
        while (/%s/.test(newStr)) {
            newStr = newStr.replace("%s", arguments[i++])
        return newStr;
    
  3. format()
    String.prototype.printf = function (obj) {
      var useArguments = false;
      var _arguments = arguments;
      var i = -1;
      if (typeof _arguments[0] == "string") {
        useArguments = true;
      if (obj instanceof Array || useArguments) {
        return this.replace(/\%s/g,
    ...
    
  4. format()
    String.prototype.format = function () {
        'use strict';
        var newString = this;
        function replace(string, search, replacement) {
            return string.replace(new RegExp(search.replace('{', '\\{').replace('}', '\\}'), 'g'), replacement.toString());
        if (arguments[0] !== 'undefined') {
            switch (typeof arguments[0]) {
                case 'object':
    ...
    
  5. 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();
      });
    
  6. format()
    String.prototype.format = function () {
      var args = arguments;
      return this.replace(/\{(\d+)\}/g, function (m, i, o, n) {
        return args[i];
      });
    };
    
  7. 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) {
    ...
    
  8. format()
    String.prototype.format = function() {
        var s = this,
            i = arguments.length;
        while (i--) {
            s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
        return s;
    };
    
  9. format()
    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";
            else {
    ...