Nodejs String Format formatString(input)

Here you can find the source of formatString(input)

Method Source Code

// Problem 1. Format with placeholders

// Write a function that formats a string. The function should have placeholders, as shown in the example
// Add the function to the String.prototype
// Example:/*from ww  w .j  av  a2  s .c  om*/

// input   
//var options = {name: 'John'};
// 'Hello, there! Are you #{name}?'.format(options);

//var options = {name: 'John', age: 13};
// 'My name is #{name} and I am #{age}-years-old').format(options);

//output
//'Hello, there! Are you John'

//'My name is John and I am 13-years-old'
String.prototype.formatString = function(input) {
    return this.replace(/(?:#{(\w+)})/g, function($0, $1) {
        return input[$1];
    });
};

var options = {
    name: 'John'
};
console.log('Hello, there! Are you #{name}?'.formatString(options));

var secondOption = {
    name: 'John',
    age: 13
};
console.log('My name is #{name} and I am #{age}-years-old'.formatString(secondOption));

Related

  1. formatMoney()
    String.prototype.formatMoney = function() {
        return "$" + this.substr(0, this.length - 2) + "." + this.substr(this.length - 2);
    
  2. formatMoney(c, d, t)
    String.prototype.formatMoney = function(c, d, t){
      var n = this, 
      c = isNaN(c = Math.abs(c)) ? 2 : c, 
      d = d == undefined ? "," : d, 
      t = t == undefined ? "." : t, 
      s = n < 0 ? "-" : "", 
      i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", 
      j = (j = i.length) > 3 ? j % 3 : 0;
     return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
    ...
    
  3. formatPV(args)
    String.prototype.formatPV = function(args) {
      if (!args) return this;
      var str = this;
      for (var arg in args) {
        str = str.replace(
          RegExp("\\{\\{" + arg + "\\}\\}", "gi"), args[arg]);
      return str;
    };
    ...
    
  4. formatPhone()
    String.prototype.formatPhone = function () {
        var re = /\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})/g;
        var subst = '$1-$2-$3';
        return this.replace(re, subst);
    };
    
  5. formatSeconds()
    String.prototype.formatSeconds = function () {
        var sec_num = parseInt(this, 10);
        var days    = Math.floor(sec_num / 86400);
        var hours   = Math.floor((sec_num - (days * 86400)) / 3600);
        var minutes = Math.floor((sec_num - (days * 86400  + hours * 3600)) / 60);
        var seconds = sec_num - (days * 86400) - (hours * 3600) - (minutes * 60);
        var time = '';
        if (days > 0) { time += days + 'd '; }
        time += hours + 'h ' + minutes + 'm ' + seconds + 's';
    ...
    
  6. formatString(num, comma)
    Number.prototype.formatString = function(num, comma) {
      return this.toFixed(num).toString().replace(/\.([0-9]*)$/, comma+"$1");
    
  7. format()
    String.prototype.format = String.prototype.f = function () {
        var s = this,
            i = arguments.length;
        while (i--) {
            s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
        return s;
    };
    
  8. format()
    String.prototype.format = String.prototype.f = function () {
        var args = arguments;
        return this.replace(/\{\{|\}\}|\{(\d+)\}/g, function (m, n) {
            if (m == "{{") { return "{"; }
            if (m == "}}") { return "}"; }
            return args[n];
        });
    };
    
  9. format()
    String.prototype.format = String.prototype.f = function () {
        var s = this,
            i = arguments.length;
        while (i--) {
            s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
        return s;
    };