Nodejs String Format format(o)

Here you can find the source of format(o)

Method Source Code

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;
        }//from   w  w w.j ava 2  s  . co m
    );
};

Related

  1. 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(),
    ...
    
  2. 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;
    ...
    
  3. 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 () {
    ...
    
  4. 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;
    }();
    
  5. 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;
    }();
    
  6. 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;
    };
    
  7. 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 {
    ...
    
  8. format(obj)
    String.prototype.format = function(obj) {
        var prop,
            regex,
            newFormat = this;
        for (prop in obj) {
            regex = new RegExp('#{' + prop + '}', 'g');
            newFormat = newFormat.replace(regex, obj[prop]);
        return newFormat;
    ...
    
  9. format(obj)
    String.prototype.format = function(obj) {
      var key, output, arg, replacements;
      output = this.valueOf();
      if(typeof obj === 'object') {
        for(key in obj) {
          output = output.replace('#' + key, obj[key]);
      } else {
        replacements = 0;
    ...