Nodejs String Format format(i, safe, arg)

Here you can find the source of format(i, safe, arg)

Method Source Code

// Inspired by http://bit.ly/juSAWl
// Augment String.prototype to allow for easier formatting.  This implementation 
// doesn't completely destroy any existing String.prototype.format functions,
// and will stringify objects/arrays.
String.prototype.format = function(i, safe, arg) {

  function format() {
    var str = this, len = arguments.length+1;

    // For each {0} {1} {n...} replace with the argument in that position.  If 
    // the argument is an object or an array it will be stringified to JSON.
    for (i=0; i < len; arg = arguments[i++]) {
      safe = typeof arg === 'object' ? JSON.stringify(arg) : arg;
      str = str.replace(RegExp('\\{'+(i-1)+'\\}', 'g'), safe);
    }//from   w ww . j a v a2 s  . c o  m
    return str;
  }

  // Save a reference of what may already exist under the property native.  
  // Allows for doing something like: if("".format.native) { /* use native */ }
  format.native = String.prototype.format;

  // Replace the prototype property
  return format;

}();

Related

  1. format(data)
    String.prototype.format = function(data) {
        return this.replace(/{(.+?)}/g, function(match, name) {
            return data[name] || match;
        });
    };
    
  2. format(dict)
    String.prototype.format = function(dict) {
      var result = this;
      if(typeof(dict) === "object") {
        Object.keys(dict).forEach(function(key) {
          result = result.replace("{" + key + "}", dict[key]);
        });
        return result;
      var args = [];
    ...
    
  3. format(fmt)
    String.prototype.format = function (fmt) {
        if (fmt==null) fmt = "yyyy-MM-dd";
        var myDate = new Date(this);
        var o = {
            "M+": myDate.getMonth() + 1,
            "d+": myDate.getDate(),
            "h+": myDate.getHours(),
            "m+": myDate.getMinutes(),
            "s+": myDate.getSeconds(),
    ...
    
  4. format(format)
    String.prototype.format = function (format) {
        const holders = JSON.parse(format),
            regex = new RegExp('(#{(\\w+)})', 'g');
        let transformed = this,
            currentMatch = '';
        while ((currentMatch = regex.exec(this)) !== null) {
            transformed = transformed.replace(currentMatch[1], holders[currentMatch[2]]);
        return transformed;
    ...
    
  5. format(format)
    String.format = function (format) {
        var args = Array.prototype.slice.call(arguments, 1);
        return format.replace(/{(\d+)}/g, function (match, number) {
            return typeof args[number] != 'undefined'
            ? args[number]
            : match;
        });
    };
    String.prototype.format = function () {
    ...
    
  6. format(i, safe, arg)
    String.prototype.format = function(i, safe, arg) {
      function format() {
        var str = this, len = arguments.length+1;
        for (i=0; i < len; arg = arguments[i++]) {
          safe = typeof arg === 'object' ? JSON.stringify(arg) : arg;
          str = str.replace(RegExp('\\{'+(i-1)+'\\}', 'g'), safe);
        return str;
      format.native = String.prototype.format;
      return format;
    }();
    
  7. format(o)
    String.prototype.format = function (o) {
        return this.replace(/{([^{}]*)}/g,
            function (a, b) {
                var r = o[b];
                return typeof r === 'string' || typeof r === 'number' ? r : a;
        );
    };
    
  8. format(o)
    String.prototype.format = function(o){
        var self = this;
        for(var i in o)self = self.replace(new RegExp("\\$\\{" + i + "\\}", "g"), o[i]);
        return self;
    };
    
  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 {
    ...