sprintf.js - An almost feature complete sprintf implementation
In regular intervals I am missing something in every Javascript environment I use. The sprintf function. There are quite a lot of reimplementations for Javascript out there, but I never found one, which really worked as I wanted it to. Therefore I finally decided to hack my own sprintf implementation. I am releasing it under the MIT license, for everybody to use, who might find it useful.
Right below is a copy of the README file which comes right with it.
Scope
This document describes the capabilities and calling conventions of quite complete sprintf implementation for Javascript. It does not explain made design decisions or inner workings of the implementation.
Capabilities
This library does provide an almost complete reimplementation of the sprintf function known from the standard c library.
The following datatypes are supported:
- %%:
- A literal percent sign
- %b:
- A binary number
- %c:
- An ASCII character represented by the given value
- %d:
- A signed decimal number
- %f:
- A floating point value
- %o:
- An octal number
- %s:
- A string
- %x:
- A hexadecimal number (lowercase characters)
- %X:
- A hexadecimal number (uppercase characters)
All of the usual formatting flags are supported as well. Therefore you may specify the algebraic sign, padding, alignment, width and precision. The syntax is equivalent to the one used by the sprintf c function.
Usage
After the corresponding Javascript file has been loaded, the global function sprintf is registered, ready to be called. Furthermore the String object is extended with a printf method. Both of these are different means to execute the same functionality.
You may either use the global function sprintf which returns the newly formatted string if supplied with the format string, as well as all needed arguments:
var formatted = sprintf("The number is %.2f", number);Or you may use the printf method directly on the format string:
var formatted = "The number is %.2f".printf(number);Internally the exactly the same processing takes place. Therefore you may decide freely which syntax you like better.
Download
The library is hosted over at github. Just go there and checkout the repository, if you are interested.