> Per Cederberg said:
>
>
> Has anybody created a generic formatting function in JavaScript? Other
> libraries? Or am I the only one seeing the need?
I agree that something like this should be a part of MochiKit; I remember
looking for this functionality a couple of years ago and was surprised it
wasn't already part of the toolkit. That said, I just figured my personal
solution would be sufficient for me...
I am from a different world than many users of MochiKit in that I am a C#
developer. In the .NET framework, we have a static method on the String
class called "Format" that does this and for a number of years I have
included this in all of my JavaScript projects:
--------------------------------------------------------------------------
function format(formatStr/*, 0...n args*/) {
forEach(izip(count(),list(arguments).slice(1)), function(arg) {
formatStr = formatStr.replace(new RegExp("\\{[" + arg[0] + "]{1}\\}" ,
"g"), arg[1]);
});
return formatStr;
}
--------------------------------------------------------------------------
I use it thusly:
format("Hello {0}, please email {1}.", name, email);
Anyway, you get the picture. Personally, I support the idea that something
along these lines should be added to MochiKit, and preferably before the 1.4
release, since it seems simple enough to do for basic functionality and
would be obvious enough that people would probably be looking for it.
That's my two cents, for what it is worth.
Jason
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"MochiKit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---