Nodejs String Format format()

Here you can find the source of format()

Method Source Code

/*//from  w  w w.  j  a  v a2  s .  co  m
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
      ;
    });
  };
} else {
  console.log(String.prototype.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,
    function (a, b) {
      i++;
      if (useArguments) {
        if (typeof _arguments[i] == 'string') {
          return _arguments[i];
        }
        else {
          throw new Error("Arguments element is an invalid type. "+typeof _arguments[i]);
        }
      }
      return obj[i];
    });
  }
  else {
    return this.replace(/{([^{}]*)}/g,
    function (a, b) {
      var r = obj[b];
      return typeof r === 'string' || typeof r === 'number' ? r : a;
    });
  }
};

Related

  1. format()
    String.prototype.format=function(){
      if(arguments.length==0) return this;
      var args=[], i=0;
      for(;i<arguments.length;i++) args[i]=arguments[i];
      return this.replace(/\%[sSfdDTt\%]/g, function(m){
        if(m=='%%') return '%';
        var v=args.shift();
        switch(m.substr(1,1)){
          case 's': return v.toString();
    ...
    
  2. format()
    String.prototype.format=function(){
      if(arguments.length==0) return this;
      var args=[], i=0;
      for(;i<arguments.length;i++) args[i]=arguments[i];
      return this.replace(/\%[sSfdDTt\%]/g, function(m){
        if(m=='%%') return '%';
        var v=args.shift();
        switch(m.substr(1,1)){
          case 's': return v.toString();
    ...
    
  3. format()
    String.prototype.format = function (){
      var values = [].slice.call(arguments, 0, arguments.length),
          idx, 
          length = values.length,
          regex, 
          str = this;
      if(str.match(/\{\d+\}/g).length !== length){
        throw{
          name: 'ArgumentMissMatch',
    ...
    
  4. 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 === '') { 
    ...
    
  5. format()
    String.prototype.format = function() {
        var newStr = this, i = 0;
        while (/%s/.test(newStr)) {
            newStr = newStr.replace("%s", arguments[i++])
        return newStr;
    
  6. 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':
    ...
    
  7. 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();
      });
    
  8. 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
          ;
        });
    ...
    
  9. format()
    String.prototype.format = function () {
      var args = arguments;
      return this.replace(/\{(\d+)\}/g, function (m, i, o, n) {
        return args[i];
      });
    };