Nodejs String Format format(obj)

Here you can find the source of format(obj)

Method Source Code

// String Interpolation
// --------------------
// Supports #name syntax for object argument
// Or $n syntax for anonymous arguments.
String.prototype.format = function(obj) {
  var key, output, arg, replacements;
  output = this.valueOf();//  w w  w .  j a  va  2s .co  m

  if(typeof obj === 'object') {
    for(key in obj) {
      output = output.replace('#' + key, obj[key]);
    }
  } else {
    replacements = 0;
    for(arg in arguments) {
      if(output !== output = output.replace('$' + replacements)) {
        replacements++;
      }
    }
  }

  return output;
}

Related

  1. format(i, safe, arg)
    String.prototype.format = function(i, safe, arg) {
      function format() {
        var str = this, len = arguments.length+1;
        for (i=0; i < len; arg = arguments[i++]) {
          safe = typeof arg === 'object' ? JSON.stringify(arg) : arg;
          str = str.replace(RegExp('\\{'+(i-1)+'\\}', 'g'), safe);
        return str;
      format.native = String.prototype.format;
      return format;
    }();
    
  2. format(o)
    String.prototype.format = function (o) {
        return this.replace(/{([^{}]*)}/g,
            function (a, b) {
                var r = o[b];
                return typeof r === 'string' || typeof r === 'number' ? r : a;
        );
    };
    
  3. format(o)
    String.prototype.format = function(o){
        var self = this;
        for(var i in o)self = self.replace(new RegExp("\\$\\{" + i + "\\}", "g"), o[i]);
        return self;
    };
    
  4. format(obj)
    String.prototype.format = function(obj) {
      var args = arguments;
      var str = this;
      return str.replace(/\{[\w\d_-]+\}/g, function(part) {
        part = part.slice(1, -1);
        var index = parseInt(part, 10);
        if (isNaN(index)) {
          return obj[part];
        } else {
    ...
    
  5. format(obj)
    String.prototype.format = function(obj) {
        var prop,
            regex,
            newFormat = this;
        for (prop in obj) {
            regex = new RegExp('#{' + prop + '}', 'g');
            newFormat = newFormat.replace(regex, obj[prop]);
        return newFormat;
    ...
    
  6. format(obj)
    String.prototype.format = function (obj) {
        var text = this;
        for (var property in obj) {
            var regExp = new RegExp('#{' + property + '}', 'g');
            text = text.replace(regExp, obj[property]);
        return text;
    var options = { name: 'John' };
    ...
    
  7. format(obj)
    String.prototype.format = function (obj) {
        var result = this.toString();
        for (var key in obj) {
            result = result.replace(new RegExp("{{" + key + "}}", "gi"), obj[key].toString());
        return result;
    };
    
  8. format(obj)
    var path = require('path');
    var fs = require('fs');
    String.prototype.format = function(obj) {
        var args = arguments;
        var str = this;
        return str.replace(/\{[\w\d\._-]+\}/g, function(part) {
            part = part.slice(1, -1);
            var index = parseInt(part, 10);
            if (isNaN(index)) {
    ...
    
  9. format(obj)
    String.prototype.format = function(obj) {
      var args = arguments;
      var str = this;
      return str.replace(/\{[\w\d\._-]+\}/g, function(part) {
        part = part.slice(1, -1);
        var index = parseInt(part, 10);
        if (isNaN(index)) {
          return dottedGet(obj, part);
        } else {
    ...