Nodejs String Format format()

Here you can find the source of format()

Method Source Code

function extend(obj, src) {
    for (var key in src) {
        if (src.hasOwnProperty(key)) obj[key] = src[key];
    }/*from   www  .  ja va  2s. c  o  m*/
    return obj;
}

// Stolen from StackOverflow
// https://goo.gl/fHfxpH
String.prototype.format = function () {
  var i = 0, args = arguments;
  return this.replace(/{}/g, function () {
    return typeof args[i] != 'undefined' ? args[i++] : '';
  });
};

function makeToast(options) {
    console.log('making toast');
    $.toast(extend({
        bgColor: '#138a36',
        textColor: '#e1e6e1',
        loaderBg: '#333333',
        position: 'bottom-right'
    }, options));
}

Related

  1. format()
    function roundNumber(rnum, rlength) { 
      var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
      return parseFloat(newnumber); 
    String.prototype.format = function() {
      var args = arguments;
      return this.replace(/{(\d+)}/g, function(match, number) {
        return typeof args[number] != 'undefined' ? args[number] : match;
      });
    ...
    
  2. format()
    var _2PI= Math.PI;
    String.prototype.format = function () {
        var args = arguments;
        return this.replace(/\{(\d+)\}/g, function() {
            return args[arguments[1]];
        });
    };
    function getClone(donor) {
        var clone = $(donor).clone();
    ...
    
  3. format()
    String.prototype.format = function()
        var re = /{(\d+)}/g;
        var sb = [];
        var match;
        var s = 0;
        while ((match = re.exec(this)))
            sb.push(this.slice(s, match.index));
    ...
    
  4. format()
    String.prototype.format = function() {
      var formatted = this, len = arguments.length, i = 0, regexp = null;
      for(i; i < len; i++) {
        regexp = new RegExp('\\{' + i + '\\}', 'gi');
        formatted = formatted.replace(regexp, arguments[i]);
      return formatted;
    };
    
  5. format()
    String.prototype.format = String.prototype.f = function(){
      var args = arguments;
      return this.replace(/\{(\d+)\}/g, function(m,n){
        return args[n] ? args[n] : m;
      });
    };
    
  6. format()
    String.prototype.format = function() {
        var args = Array.prototype.slice.call(arguments),
            format = this,
            match;
        if (args.length === 1 && typeof args[0] === "object") {
            args = args[0];
        for (var i = 0; match = /{(\d+|\w+)?}/gm.exec(format); i++) {
            var key = match[1];
    ...
    
  7. format()
    String.prototype.format = function () {
        var s = this;
        for (var i = 0; i < arguments.length; i++) {
            var reg = new RegExp("\\{" + i + "\\}", "gm");
            s = s.replace(reg, arguments[i]);
        return s;
    
  8. 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;
    };
    function selectText() {
    ...
    
  9. format()
    String.prototype.format = function () {
       var args = arguments; 
       return this.replace( /\{(\d|\d\d)\}/g, 
       function ( $0 ) {
          var idx = $0.match(/\d+/);
          return args[idx] ? args[idx] : $0 ;
       } );
    "T123{0}T{0}".format("A","B")
    ...