Nodejs String Format format(obj)

Here you can find the source of format(obj)

Method Source Code

String.prototype.format = function(obj) {
  var args = arguments;
  var str = this;
  // Support either an object, or a series.
  return str.replace(/\{[\w\d_-]+\}/g, function(part) {
    // Strip off {}.
    part = part.slice(1, -1);//w w w . j  a  v  a 2s.c  om
    var index = parseInt(part, 10);
    if (isNaN(index)) {
      return obj[part];
    } else {
      return args[index];
    }
  });
};

function formatDate(date) {
  var now = new Date();
  var date_disp;

  date_disp = date.getHours() + ':';
  date_disp += (date.getMinutes() < 10) ? '0' : '';
  date_disp += date.getMinutes();
  date_disp += ' - ';

  if (now - date < 24 * 60 * 60 * 1000) {
    date_disp += 'Today';
  } else if (now - date < 48 * 60 * 60 * 1000) {
    date_disp += 'Yesterday';
  } else {
    date_disp += date.getFullYear() + '-';
    var month = date.getMonth() + 1;
    if (month < 10) {
      date_disp += '0';
    }
    date_disp += month + '-' + date.getDate();
  }

  return date_disp;
}

Related

  1. format(obj)
    String.prototype.format = function (obj) {
        var result = this.toString();
        for (var key in obj) {
            result = result.replace(new RegExp("{{" + key + "}}", "gi"), obj[key].toString());
        return result;
    };
    
  2. format(obj)
    var path = require('path');
    var fs = require('fs');
    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)) {
    ...
    
  3. 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 dottedGet(obj, part);
        } else {
    ...
    
  4. format(obj)
    function getParameterByName(name) {
        name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
        return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    String.prototype.format = function (obj) {
        return this.replace(/\{\s*([^}\s]+)\s*\}/g, function (m, p1, offset, string) {
            return obj[p1]
    ...
    
  5. format(obj)
    String.prototype.format = function(obj){
        var placeHolders = this.match(/#{\w+}/g),
            prop,
            result = this;
        for (var i = 0; i < placeHolders.length; i++) {
            prop = placeHolders[i].substring(2, placeHolders[i].length - 1);
            if (obj[prop]){
                result = result.replace(placeHolders[i], obj[prop]);
        return result;
    };
    var options = {name: 'John'};
    console.log('Hello, there! Are you #{name}?'.format(options));
    var options2 = {name: 'John', age: 13};
    console.log('My name is #{name} and I am #{age}-years-old'.format(options2));
    
  6. format(object)
    String.prototype.format = function (object) {
        var self=this;
        for (var item in object) {
            var replaceItem = object[item],
                regExp = new RegExp('#{' + item + '}');
            self=self.replace(regExp, replaceItem);
        return self;
    var object = {
        name: 'John',
        age: 15,
        gender: 'male'
    };
    var str = 'My name is #{name}, I am #{age}-years-old and my gender is #{gender}'.format(object);
    console.log('TASK 1:\n'+str);
    
  7. format(options)
    function solve(args) {
    String.prototype.format = function(options) {
        var prop,
            result = this;
        for (prop in options) {
            result = result.replace(new RegExp('#{' + prop + '}', 'g'), options[prop]);
        return result;
    };
    ...
    
  8. format(options)
    String.prototype.format = function(options) {
      var option,
        toReplace,
        str = this;
      for (option in options) {
        toReplace = new RegExp('#{' + option + '}');
        str = str.replace(toReplace, options[option]);
      console.log(str);
    ...
    
  9. 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;
    ...