Nodejs String Format format()

Here you can find the source of format()

Method Source Code

/*//from   w w w  .ja va2  s.c o  m
 * myShoutBox
 * 
 * Simple shout box client and server
 *
 * Copyright (C) 2013 Petar Zivkovic
 * 
 * This code is licensed under the LGPL v3
 * See the LICENSE file in the root folder of the project
 * 
 */

'use strict';

/*
 * sprintf like function
 * ex. '{0} am {1}'.format('I', 'here'); outputs 'I am here'
 *
 * Arguments:
 *      none
 * Return value:
 *      modified string
 */
String.prototype.format = function() {
   var content = this;
   for (var i=0; i<arguments.length; i++) {
      var placeholder = '{' + i + '}';
      content = content.replace(placeholder, arguments[i]);
   }
   return content;
};

Related

  1. format()
    function log(s){ 
      if(console)
        console.log(s);
    function callStack(){
      log(new Error().stack);
    String.prototype.format = function() {
      var args = arguments;
    ...
    
  2. format()
    'use strict'
    String.format = String.f = function () {
      var s = arguments[0]
      for (var i = 0; i < arguments.length; i++) {
        var regexp = new RegExp('\\{' + i + '\\}', 'gm')
        s = s.replace(regexp, (arguments[i + 1] === null ? '' : arguments[i + 1]))
      return s
    String.prototype.format = String.prototype.f = function () {
      var s = this
      for (var i = 0; i < arguments.length; i++) {
        var regexp = new RegExp('\\{' + i + '\\}', 'gm')
        s = s.replace(regexp, (arguments[i] === null ? '' : arguments[i]))
      return s
    
  3. format()
    String.prototype.format = function () {
        var args = arguments;
        return this.replace(/\{\{|\}\}|\{(\d+)\}/g, function (m, n) {
            if (m == "{{") { return "{"; }
            if (m == "}}") { return "}"; }
            return args[n];
        });
    };
    
  4. format()
    function package(nspace)
        var nspaceComponents = nspace.split(".");
        var parent = window;
        for (var i = 0; i < nspaceComponents.length; ++i) {
            if (typeof parent[nspaceComponents[i]] === "undefined") {
                parent[nspaceComponents[i]] = {};
            parent = parent[nspaceComponents[i]];
    ...
    
  5. format()
    String.prototype.format = function () {
        var args = arguments;
        return this.replace(/{(\d+)}/g, function (match, number) {
            return typeof args[number] != 'undefined'
                ? args[number]
                : match
                ;
        });
    };
    ...
    
  6. format()
    var width = 584
    var height = 329
    String.prototype.format = function() {
      var formatted = this;
      for (var i = 0; i < arguments.length; i++) {
        var regexp = new RegExp('\\{'+i+'\\}', 'gi');
        formatted = formatted.replace(regexp, arguments[i]);
      return formatted;
    ...
    
  7. format()
    var applicationID = '62B33EBF';
    var namespace = 'urn:x-cast:com.google.cast.sample.helloworld';
    var session = null;
    function actualizarTiempo() {
      d = new Date();
      horas = d.getHours() < 10?"0{0}".format(d.getHours()):d.getHours();
      minutos = d.getMinutes() < 10?"0{0}".format(d.getMinutes()):d.getMinutes();
      return "Actualizado a las {0}:{1}".format(horas, minutos);
    String.prototype.format = function() {
      var formatted = this;
      for (var i = 0; i < arguments.length; i++) {
        var regexp = new RegExp('\\{'+i+'\\}', 'gi');
        formatted = formatted.replace(regexp, arguments[i]);
      return formatted;
    };
    
  8. 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;
      });
    ...
    
  9. 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();
    ...