Nodejs String Format format(params)

Here you can find the source of format(params)

Method Source Code

//Problem 1. Format with placeholders
////from  w ww .  java 2s.  c  o  m
//    Write a function that formats a string. The function should have placeholders, as shown in the example
//Add the function to the String.prototype

String.prototype.format = function (params) {
    'use strict';

    var p,
        regex,
        result;

    result = this;

    for (p in params) {
        if (params.hasOwnProperty(p)) {
            regex = new RegExp('#{' + p + '}', 'g');
            result = result.replace(regex, params[p]);
        }
    }

    return result;
}

function problem01_FormatWithPlaceholders() {
    'use strict';
    var options = {name: 'John'};
    console.log('Hello, there! Are you #{name}?'.format(options));

    options = {name: 'John', age: 13};
    console.log('My name is #{name} and I am #{age}-years-old'.format(options));
}

Related

  1. format(opts)
    String.prototype.format = function(opts) {
        var source = this,
            data = Array.prototype.slice.call(arguments, 0),
            toString = Object.prototype.toString;
        if (data.length) {
            data = data.length == 1 ?
                (opts !== null && (/\[object Array\]|\[object Object\]/.test(toString.call(opts))) ? opts : data) : data;
            return source.replace(/%\{(.+?)\}/g, function(match, key) {
                var replacer = data[key];
    ...
    
  2. format(param)
    String.prototype.format = function(param) {
        var formatted = this;
        for(i in param ){
            var regexp = new RegExp('\\{'+i+'\\}', 'gi');
            formatted = formatted.replace(regexp, param[i]);
        return formatted;
    };
    
  3. format(parameters)
    var FORMAT_PARAMETER_PATTERN;
    FORMAT_PARAMETER_PATTERN = /\@\(([a-zA-Z](?:[a-zA-Z0-9_]*))\)/g;
    String.prototype.format = function(parameters) {
      var value;
      value = this.valueOf();
      return value.replace(FORMAT_PARAMETER_PATTERN, function(match, name) {
        var _ref;
        return (_ref = parameters != null ? parameters[name] : void 0) != null ? _ref : "<>";
      });
    ...
    
  4. format(params)
    String.prototype.format = function(params) {
      if (!params) {throw new Error('No arguments passed');}
      return this.replace(/\{([a-zA-Z0-9]+)\}/g, function() {
        var key = arguments[1];
        if (params.hasOwnProperty(key)) {
          return params[key];
        } else {
          return arguments[0];
      });
    };
    
  5. format(params)
    String.prototype.format = function(params){
      var formatted = this;
      var fn = function(key, value){
        var regexp = new RegExp('\\{' + key + '\\}', 'gi');
        formatted = formatted.replace(regexp, value);
      };
      if (typeof(params) == 'object') {
        for (var x in params) {
          fn(x, params[x]);
    ...
    
  6. format(params)
    String.prototype.format = function (params){
        var reg = /{(\d+)}/gm;
        return this.replace(reg,function(match,name){
            return params[~~name];}
        );
    };
    
  7. format(string)
    function ms_to_days(m) {
      return Math.floor(m / (86400 * 1000) + 0.5)
    function date_period_to_ms(dp) {
      var ym = dp.split('-', 2)
      return new Date(ym[0], parseInt(ym[1], 10) - 1, 1);
    Date.prototype.format = function (string) {  
      string = string.replace(/%b/, I18n.t('date.abbr_month_names')[this.getMonth() + 1], 'g');
    ...
    
  8. format(substitutions)
    String.prototype.format = function(substitutions) {
      return this.replace(/\{([A-Za-z0-9]+)\}/g, function(match, key) {
        return substitutions[key] !== undefined ? substitutions[key] : '';
      });
    };
    
  9. format(substitutions)
    String.prototype.format = function(substitutions) {
      return this.replace(/\{([A-Za-z0-9]+)\}/g, function(match, key) { 
        return substitutions[key] !== undefined ? substitutions[key] : '';
      });
    };