Nodejs String Format format(object)

Here you can find the source of format(object)

Method Source Code

/**/*from  w ww  .j  a  v a  2s. c o m*/
 * Parse a text with a placeholder object
 * @param object
 * @returns {String}
 */
String.prototype.format = function format(object) {
    let parsed = this;
    for (const option in object) {
        if ({}.hasOwnProperty.call(object, option)) {
            const regex = new RegExp(`#{${option}}`, 'g');
            parsed = parsed.replace(regex, object[option]);
        }
    }

    return parsed;
};

export default String.prototype.format;

Related

  1. format(options)
    String.prototype.format = String.prototype.f = function(options) {
        options = options ? options : [];
        if (typeof options != "object") options = [options];
        var s = this,
            i = options.length;
        while (i--) {
            s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), options[i]);
        return s;
    ...
    
  2. format()
    String.prototype.format = String.prototype.format = function() {
      var s = this,
        i = arguments.length;
      while (i--) {
        s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
      return s;
    };
    
  3. format()
    String.prototype.format = String.prototype.format ||
    function () {
        var str = this.toString();
        if (arguments.length) {
            var t = typeof arguments[0];
            var key;
            var args = ("string" === t || "number" === t) ?
                Array.prototype.slice.call(arguments)
                : arguments[0];
    ...
    
  4. format()
    String.prototype.format = String.prototype.format || function () {
      var string = this
      for (var i = 0, j = arguments.length; i < j; i++) {
        string = string.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i])
      return string
    
  5. format()
    String.prototype.format = String.prototype.format || function format() {
        var args = arguments;
        return this.replace(/\{(\d+)\}/g, function($0, $1) {
            return args[+$1];
        });
    };
    
  6. format()
    function format () {
      if (arguments.length === 0) return this;
      var iter;
      if (arguments.length > 1) { 
        iter = arguments;
      } else {
        iter = arguments[0];
      var ret = this;
    ...
    
  7. format()
    (function(){
    function format () {
      if (arguments.length === 0) return this;
      var iter;
      if (arguments.length > 1) { 
        iter = arguments;
      } else {
        iter = arguments[0];
      var ret = this;
      for (var i in iter) {
        ret = ret.replace('{'+i+'}', iter[i]);
      return ret;
    String.prototype.format = format;
    })();
    
  8. toFormattedString(f)
    Date.prototype.toFormattedString = function (f) {
        var nm = this.getMonthName();
        var nd = this.getDayName();
        f = f.replace(/yyyy/g, this.getFullYear());
        f = f.replace(/yy/g, String(this.getFullYear()).substr(2, 2));
        f = f.replace(/MMM/g, nm.substr(0, 3).toUpperCase());
        f = f.replace(/Mmm/g, nm.substr(0, 3));
        f = f.replace(/MM\*/g, nm.toUpperCase());
        f = f.replace(/Mm\*/g, nm);
    ...
    
  9. toFormattedString(f)
    Date.prototype.toFormattedString = function(f) {
        f = f.replace(/yyyy/g, this.getFullYear());
        f = f.replace(/yy/g, String(this.getFullYear()).substr(2, 2));
        f = f.replace(/mm/g, String(this.getMonth() + 1).padLeft('0', 2));
        f = f.replace(/dd/g, String(this.getDate()).padLeft('0', 2));
        f = f.replace(/HH/g, String(this.getHours()).padLeft('0', 2));
        f = f.replace(/MM/g, String(this.getMinutes()).padLeft('0', 2));
        return f;
    };
    ...