Nodejs String Format format(param)

Here you can find the source of format(param)

Method Source Code

String.prototype.format = function(param) {
    var formatted = this;
    for(i in param ){
        var regexp = new RegExp('\\{'+i+'\\}', 'gi');
        formatted = formatted.replace(regexp, param[i]);
    }/*from  w ww .jav  a 2  s.co m*/
    return formatted;
};

Related

  1. format(options)
    String.prototype.format = function (options) {
        var regex,
            input = this;
        for (var key in options) {
            regex = new RegExp('#{' + key + '}', 'g');
            input = input.replace(regex, options[key]);
        return input;
    console.log('Original text is: Hello, there! Are you #{name}? and Object is: { name: \'John\' }')
    console.log('Changed text is: ' + 'Hello, there! Are you #{name}?'.format({ name: 'John' }));
    console.log('Original text is: My name is #{name} and I am #{age}-years-old).format(options) and object is: {name: \'Johnny\', age: 21} ');
    console.log('Changed text is: ' + 'My name is #{name} and I am #{age}-years-old)'.format({name: 'Johnny', age: 21}));
    
  2. format(options)
    "use strict";
    var formatPlace = function (options, str) {
        function replacer(match, i) {
            i = i.substring(2, i.length - 1);
            return options[i];
        return str.replace(/(#{[^{}]+})/gm, replacer);
    };
    String.prototype.format = function(options){
    ...
    
  3. format(options)
    String.prototype.format = function(options) {
        var prop,
            result = this;
        for (prop in options) {
            result = result.replace(new RegExp('#{' + prop + '}', 'g'), options[prop]);
        return result;
    };
    var options01 = { name: 'John' };
    ...
    
  4. format(opts)
    String.prototype.format = function(opts) {
        var option,
            regex,
            formatted = this;
        for (option in opts) {
            regex = new RegExp('#{' + option + '}', 'g');
            formatted = formatted.replace(regex, opts[option]);
        return formatted;
    ...
    
  5. 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];
    ...
    
  6. 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 : "<>";
      });
    ...
    
  7. 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];
      });
    };
    
  8. 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]);
    ...
    
  9. format(params)
    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');
    ...