Nodejs String Format format()

Here you can find the source of format()

Method Source Code

String.prototype.format = function() {
  var formatted = this;
  for( var arg in arguments ) {
    formatted = formatted.replace("{" + arg + "}", arguments[arg]);
  }//from w ww .j  av  a 2s  . co  m
  return formatted;
};

function assert(condition, msg) {
  if (!condition) {
    msg = msg || "Assertion failed";
    throw Error(msg);
  }
}

function FilledArray(n, value) {
  var arr = new Array(n);
  for (var i = 0; i < n; ++i) {
    arr[i] = value;
  }
  return arr;
}

function Mat(n, d) {
  this.n = n;
  this.d = d;
  this.w = FilledArray(n * d, 0);
  this.dw = FilledArray(n * d, 0);
}

function RandMat(n, d, mu, std) {
  var m = new Mat(n, d);
  for (var i = 0; i < m.w.length; ++i) {
    m.w[i] = rand(-std, std);
  }
  return m;
}

function rand(a, b) {
  return Math.random() * (b - a) + a;
}

Related

  1. format()
    define(function () {
        return {
            currentDate: function () {
                var date = new Date();
                var dd = date.getDate();
                var mm = date.getMonth() + 1;
                var yyyy = date.getFullYear();
                var hh = date.getHours();
                var MM = date.getMinutes();
    ...
    
  2. format()
    String.prototype.format = function () {
        var o = Array.prototype.slice.call(arguments);
        return this.replace(/{([^{}]*)}/g,
            function (match, capture) {
                var r = o[capture];
                return (typeof r === 'string' || typeof r === 'number') ? r : match;
        );
    };
    ...
    
  3. format()
    String.prototype.format = function() {
      var args = arguments;
      return this.replace(/\{(\d+)\}/g, function(orig,num) {
        return typeof args[num] != 'undefined' ? args[num] : orig;
      });
    };
    
  4. format()
    String.prototype.format = function() {
        var args = arguments;
        return this.replace(/{(\d+)}/g, function(match, number) {
            return typeof args[number] != 'undefined' ? args[number] : match;
        });
    };
    
  5. format()
    String.prototype.format = function() {
      var placeholderREG = /\{(\d+)\}/g;
      var args = arguments;
      return this.replace(placeholderREG, function(holder, num) {
        return args[num];
      });
    
  6. format()
    'use strict';
    String.prototype.format = function () {
      var args = arguments;
      return this.replace(/\{(\d+)\}/g, function (m, n) {
        return args[n];
      });
    };
    String.prototype.escapeRegExp = function () {
      return this.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
    ...
    
  7. format()
    String.prototype.format = function() {
      var source = this;
      for (var i = 0; i <= arguments.length - 1; i++) {       
        var reg = new RegExp("\\{" + i + "\\}", "gm");             
        source = source.replace(reg, arguments[i]);
      return source;
    };
    Date.prototype.format = function (fmt) { 
    ...
    
  8. format()
    String.prototype.format = function() {
      var formatted = this;
      for (arg in arguments) {
        formatted = formatted.replace('{' + arg + '}', arguments[arg]);
      return formatted;
    };
    
  9. format()
    String.prototype.format = function() {
        var args = arguments;
        return this.replace(/\{(\d+)\}/g,
            function(m, i) {
                return args[i];
            });
    };
    String.prototype.encodeHTML = function () {
        var s = this.valueOf();
    ...