Nodejs String Format format(obj)

Here you can find the source of format(obj)

Method Source Code

var path = require('path');
var fs = require('fs');

/* Python new-style string formatting.
* > "Hello, {0}.".format('Mike');/*from  ww  w. j a va 2s. co  m*/
* Hello, Mike.
* > "How is the weather in {citi}?".format({city: 'Mountain View'})
* How is the weather in Mountain View?
*/
String.prototype.format = function(obj) {
    var args = arguments;
    var str = this;
    // Support either an object, or a series.
    return str.replace(/\{[\w\d\._-]+\}/g, function(part) {
        // Strip off {}.
        part = part.slice(1, -1);
        var index = parseInt(part, 10);
        if (isNaN(index)) {
            return dottedGet(obj, part);
        } else {
            return args[index];
        }
    });
};

Related

  1. 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 {
    ...
    
  2. 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;
    ...
    
  3. format(obj)
    String.prototype.format = function(obj) {
      var key, output, arg, replacements;
      output = this.valueOf();
      if(typeof obj === 'object') {
        for(key in obj) {
          output = output.replace('#' + key, obj[key]);
      } else {
        replacements = 0;
    ...
    
  4. 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' };
    ...
    
  5. 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;
    };
    
  6. 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 {
    ...
    
  7. format(obj)
    function getParameterByName(name) {
        name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
        return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    String.prototype.format = function (obj) {
        return this.replace(/\{\s*([^}\s]+)\s*\}/g, function (m, p1, offset, string) {
            return obj[p1]
    ...
    
  8. format(obj)
    String.prototype.format = function(obj){
        var placeHolders = this.match(/#{\w+}/g),
            prop,
            result = this;
        for (var i = 0; i < placeHolders.length; i++) {
            prop = placeHolders[i].substring(2, placeHolders[i].length - 1);
            if (obj[prop]){
                result = result.replace(placeHolders[i], obj[prop]);
        return result;
    };
    var options = {name: 'John'};
    console.log('Hello, there! Are you #{name}?'.format(options));
    var options2 = {name: 'John', age: 13};
    console.log('My name is #{name} and I am #{age}-years-old'.format(options2));
    
  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 obj[part];
        } else {
    ...