Nodejs String Format format(options)

Here you can find the source of format(options)

Method Source Code

/**//from   w  w  w . ja  v a 2  s .  c  o m
 * Problem 1. Format with placeholders
 * 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 (options) {
    var option,
        regex,
        formatted = this;

    for (option in options) {
        regex = new RegExp('#{' + option + '}', 'g');
        formatted = formatted.replace(regex, options[option]);
    }

    return formatted;
}


console.log('Hello, there! Are you #{name}?'.format({name: 'John'}));
console.log('My name is #{name} and I am #{age}-years-old'.format({name: 'John', age: 13}));

Related

  1. format(options)
    console.log('TASK 1');
    var text = 'My name is #{name}. I have #{color} eyes and I am #{age}-years-old';
    String.prototype.format = function(options) {
        var option,
            result = this;
        for (option in options) {
            result = result.replace(new RegExp('#{' + option + '}', 'g'), options[option]);
        return result;
    ...
    
  2. format(options)
    String.prototype.format = function(options){
      var option,
          result = this;
        for (option in options){
            var regex = new RegExp('#{' + option + '}');
            result = result.replace(regex,options[option]);
        return result;
    };
    ...
    
  3. format(options)
    String.prototype.format = function (options) {
        var option,
            regex,
            formatted = this;
        for (option in options) {
            regex = new RegExp('#{' + option + '}', 'g');
            formatted = formatted.replace(regex, options[option]);
        return formatted;
    ...
    
  4. format(options)
    String.prototype.format = function(options) {
        var option,
            regex,
            formatted = this;
      for (option in options) {
          regex = new RegExp('#{' + option + '}', 'g');
          formatted = formatted.replace(regex, options[option]);
        return formatted;
    ...
    
  5. format(options)
    console.log('1. Format with placeholders');
    String.prototype.format = function(options) {
        var option,
            regex,
            formatted = this;
        for (option in options) {
            regex = new RegExp('#{' + option + '}', 'g');
            formatted = formatted.replace(regex, options[option]);
        return formatted;
    };
    console.log('Hello, there! Are you #{name}?'.format({name: 'John'}));
    console.log('My name is #{name} and I am #{age}-years-old'.format({name: 'John', age: 13}));
    
  6. format(options)
    String.prototype.format = function(options) {
        var option,
            regex,
            formatted = this;
        for (option in options) {
            regex = new RegExp('#{' + option + '}', 'g');
            formatted = formatted.replace(regex, options[option]);
        return formatted;
    ...
    
  7. format(options)
    String.prototype.format = function (options) {
        var option,
            regex,
            formatted = this;
        for (option in options) {
            regex = new RegExp('#{' + option + '}', 'g');
            formatted = formatted.replace(regex, options[option]);
        return formatted;
    ...
    
  8. format(options)
    String.prototype.format=function(options) {
        var string = this;
        for (var i in options) {
            var pattern = '#{' + (i) + '\\}';
            var regex = new RegExp(pattern, 'g');
            string = string.replace(regex, options[i]);
        return string;
    var options = {name: 'John'};
    console.log('Hello, there! Are you #{name}?'.format(options));
    var options = {name: 'John', age: 13};
    console.log('My name is #{name} and I am #{age}-years-old'.format(options));
    
  9. format(options)
    var phraseOne = 'Hello, there! Are you #{name}?';
    var phraseTwo = 'My name is #{name} and I am #{age}-years-old';
    String.prototype.format = function (options) {
        var option,
            fullString = this;
        for (option in options) {
            var placeholder = /#\{\w+}/;
            fullString = fullString.replace(placeholder, options[option]);
        return fullString;
    };
    console.log(phraseOne.format({name: 'John'}));
    console.log(phraseTwo.format({name: 'John', age: 13}));