Nodejs Utililty Methods String Format

List of utility methods to do String Format

Description

The list of methods to do String Format are organized into topic(s).

Method

format(args, index)
String.prototype.format = function(args, index) {
  return this.replace(/{(\w+)}/g, function(match, number) {
    return typeof args[index[number]] != 'undefined'
      ? args[index[number]]
      : match
    ;
  });
};
format(ctx)
String.prototype.format = function (ctx) {
  return this.replace(/\$\{([\w\.]+)\}/g, function(all, varname) {
    var keys = varname.split('.');
    var val = null;
    for (var i = 0, len = keys.length; i < len; i++) {
      val = ctx[keys[i]];
    return val;
  });
...
format(data)
String.prototype.format = function(data) {
    return this.replace(/{(.+?)}/g, function(match, name) {
        return data[name] || match;
    });
};
format(dict)
String.prototype.format = function(dict) {
  var result = this;
  if(typeof(dict) === "object") {
    Object.keys(dict).forEach(function(key) {
      result = result.replace("{" + key + "}", dict[key]);
    });
    return result;
  var args = [];
...
format(fmt)
String.prototype.format = function (fmt) {
    if (fmt==null) fmt = "yyyy-MM-dd";
    var myDate = new Date(this);
    var o = {
        "M+": myDate.getMonth() + 1,
        "d+": myDate.getDate(),
        "h+": myDate.getHours(),
        "m+": myDate.getMinutes(),
        "s+": myDate.getSeconds(),
...
format(format)
String.prototype.format = function (format) {
    const holders = JSON.parse(format),
        regex = new RegExp('(#{(\\w+)})', 'g');
    let transformed = this,
        currentMatch = '';
    while ((currentMatch = regex.exec(this)) !== null) {
        transformed = transformed.replace(currentMatch[1], holders[currentMatch[2]]);
    return transformed;
...
format(format)
String.format = function (format) {
    var args = Array.prototype.slice.call(arguments, 1);
    return format.replace(/{(\d+)}/g, function (match, number) {
        return typeof args[number] != 'undefined'
        ? args[number]
        : match;
    });
};
String.prototype.format = function () {
...
format(i, safe, arg)
String.prototype.format = function(i, safe, arg) {
  function format() {
    var str = this, len = arguments.length+1;
    for (i=0; i < len; arg = arguments[i++]) {
      safe = typeof arg === 'object' ? JSON.stringify(arg) : arg;
      str = str.replace(RegExp('\\{'+(i-1)+'\\}', 'g'), safe);
    return str;
  format.native = String.prototype.format;
  return format;
}();
format(i, safe, arg)
String.prototype.format = function(i, safe, arg) {
  function format() {
    var str = this, len = arguments.length+1;
    for (i=0; i < len; arg = arguments[i++]) {
      safe = typeof arg === 'object' ? JSON.stringify(arg) : arg;
      str = str.replace(RegExp('\\{'+(i-1)+'\\}', 'g'), safe);
    return str;
  format.native = String.prototype.format;
  return format;
}();
format(o)
String.prototype.format = function (o) {
    return this.replace(/{([^{}]*)}/g,
        function (a, b) {
            var r = o[b];
            return typeof r === 'string' || typeof r === 'number' ? r : a;
    );
};