Javascript String fmt()

Description

Javascript String fmt()


String.prototype.$fmt = function() {
  var args = arguments;
  return this.replace(/\{(\d+)\}/g, function(str, m1) {
    if (m1 >= args.length) {
      return str;
    }//from  w ww  .  jav  a2  s.  c  om
    return args[m1];
  });
};

Javascript String fmt()


String.prototype.fmt = function(/* ...args */) {
    var args = arguments;
    return this.replace(/\{\d+\}/g, function(text) {
        var value = args[+text.substring(1, text.length - 1)];
        return value === null ? '' : value;
    });/*www.j  a v  a2  s  .  com*/
};

Javascript String fmt()

function fmt(str, formats) {
  // first, replace any ORDERED replacements.
  var idx  = 0; // the current index for non-numerical replacements
  return str.replace(/%@([0-9]+)?/g, function(s, argIndex) {
    argIndex = (argIndex) ? parseInt(argIndex, 10) - 1 : idx++;
    s = formats[argIndex];/*from   ww w .  ja va 2 s.  co m*/
    return (s === null) ? '(null)' : (s === undefined) ? '' : console.log(s);
  }) ;
}

String.prototype.fmt = fmt;

module.exports = {};



PreviousNext

Related