sprintf() for JavaScript

sprintf() for JavaScript is a lightweight yet powerful JavaScript implementation of sprintf(), based on the PHP specification.
Download

sprintf() for JavaScript Ranking & Summary

Advertisement

  • Rating:
  • License:
  • GPL
  • Price:
  • FREE
  • Publisher Name:
  • Alexei
  • Publisher web site:
  • http://alexei.417.ro/blog/sprintf_for_javascript.html

sprintf() for JavaScript Tags


sprintf() for JavaScript Description

sprintf() for JavaScript is a lightweight yet powerful JavaScript implementation of sprintf(), based on the PHP specification. sprintf() for JavaScript is a lightweight yet powerful JavaScript implementation of sprintf(), based on the PHP specification. There are a lot of libraries out there and, invariably, they do a lot of things that you don't need or in a manner that you don't like. Thus, they feel bloated. One example would be Prototype. Recently, I discovered it has a Template class. At first, I thought it was a template engine similar to Smarty for PHP, but it turned out I was wrong. It's just a method of returning formatted strings wrapped in a fancy class. If you're familiar with C, C++, Objective C, Perl, Python, PHP, bash, awk, etc. you're probably aware of the printf function, used to output formatted strings. Because I wanted for a long time a similar function in JavaScript, I wrote it using PHP's sprintf spec.. It's prototype is simple:string sprintf(string format]])The placeholders in the format string are marked by "%" and are followed by one or more of these elements, in this order:1. An optional "+" sign that forces to preceed the result with a plus or minus sign on numeric values. By default, only the "-" sign is used on negative numbers.2. An optional padding specifier that says what character to use for padding (if specified). Possible values are 0 or any other character precedeed by a '. The default is to pad with spaces.3. An optional "-" sign, that causes sprintf to left-align the result of this placeholder. The default is to right-align the result.4. An optional number, that says how many characters the result should have. If the value to be returned is shorter than this number, the result will be padded.5. An optional precision modifier, consisting of a "." (dot) followed by a number, that says how many digits should be displayed for floating point numbers. When used on a string, it causes the result to be truncated.A type specifier that can be any of: % - print a literal "%" characterb - print an integer as a binary numberc - print an integer as the character with that ASCII valued - print an integer as a signed decimal numbere - print a float as scientific notationu - print an integer as an unsigned decimal numberf - print a float as iso - print an integer as an octal numbers - print a string as isx - print an integer as a hexadecimal number (lower-case)X - print an integer as a hexadecimal number (upper-case)Argument swappingYou can now swap the arguments. That is, the order of the placeholders doesn't have to match the order of the arguments. You can do that by simply indicating in the format string which arguments the placeholders refer to:sprintf("The first 4 letters of the english alphabet are: %4$s, %3$s, %1$s and %2$s", 'c', 'd', 'b', 'a');And, of course, you can repeat the placeholders without having to increase the number of arguments.What's New in This Release:· The width specifier has been fixed.


sprintf() for JavaScript Related Software