2

Are there any popular JavaScript libraries like jQuery, Underscore.js, or similar that implement 'Hello {0}!'.format('World'); or equivalent?

Is this a bad practice in JavaScript as opposed to string concatenation that it is not included, or is it just overlooked or considered too specialized for inclusion in popular libraries? Or, is there another reason?

(Please don't answer with an implementation of String.prototype.format, I'm asking for functionality in a popular library I may be overlooking.)

kendaleiv
  • 5,793
  • 3
  • 28
  • 38
  • Some kind of JS templating engine? They aren't part of most popular libraries due to being too localized I'd say. There are some [plugins](http://api.jquery.com/jquery.tmpl/) for those and even many [dedicated libraries](http://garann.github.com/template-chooser/) for templating, but if all you need is something as simple as that, why not go with the prototype implementation? – Fabrício Matté Oct 05 '12 at 23:05
  • http://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format – Andreas Oct 05 '12 at 23:15

2 Answers2

1

MooTools has a .substitute function on the string prototype that looks like it might do what you want. See: http://mootools.net/docs/core/Types/String#String:substitute.

Prototype.js has a similar .interpolate function. See: http://api.prototypejs.org/language/String/prototype/interpolate/

Some would argue though that modifying primitive prototypes is bad practice as it can break some common, but incorrectly formed, for each loops.

Google's closure library on the other hand, defines a global goog.string.format function, not on the string prototype, that does about the same thing. See: http://closure-library.googlecode.com/svn/docs/namespace_goog_string.html#goog.string.format

Joshua Dwire
  • 5,415
  • 5
  • 29
  • 50
  • Google brings up a good point though when they say in their docs: "DO NOT use it instead of built-in conversions in simple cases such as 'Cost: %.2f' as it would introduce unneccessary latency oposed to 'Cost: ' + cost.toFixed(2).", and this would apply to other libraries as well. – Joshua Dwire Oct 06 '12 at 03:49
0

Actually, you can write such a method easily for yourself. Since ECMAscript is not a strictly typed language you can make your life even more easy. Example:

String.prototype.sFormat = function _simpleFormat( map ) {
    var myString    = this.toString(),
        args        = map instanceof Array ? map : Array.prototype.slice.call( arguments );

    while( ~myString.indexOf( '%r' ) ) {
        myString = myString.replace( '%r', args.shift() );
    }

    return myString;
};

From that point on, we can go like

var foo = "Hello",
    bar = 42;

"%r world, the answer is %r".sFormat( foo, bar ); // "Hello world, the answer is 42"
jAndy
  • 231,737
  • 57
  • 305
  • 359
  • @ChrisMarisic: I don't feel that is a reasonable downvote nonetheless. Btw, OP added the italic part later on. I still think this answer has some value, especially because its so easy to implement. Thanks bro ;) – jAndy Oct 08 '12 at 22:01