{\bfseries{sprintf.\+js}} is a complete open source Java\+Script sprintf implementation for the {\itshape browser} and {\itshape node.\+js}.

Its prototype is simple\+: \begin{DoxyVerb}string sprintf(string format , [mixed arg1 [, mixed arg2 [ ,...]]])
\end{DoxyVerb}
 The placeholders in the format string are marked by {\ttfamily \%} and are followed by one or more of these elements, in this order\+:


\begin{DoxyItemize}
\item An optional number followed by a {\ttfamily \$} sign that selects which argument index to use for the value. If not specified, arguments will be placed in the same order as the placeholders in the input string.
\item An optional {\ttfamily +} sign that forces to preceed the result with a plus or minus sign on numeric values. By default, only the {\ttfamily -\/} sign is used on negative numbers.
\item An optional padding specifier that says what character to use for padding (if specified). Possible values are {\ttfamily 0} or any other character precedeed by a `'{\ttfamily (single quote). The default is to pad with $\ast$spaces$\ast$.}
\item {\ttfamily An optional}-\/{\ttfamily sign, that causes sprintf to left-\/align the result of this placeholder. The default is to right-\/align the result.}
\item {\ttfamily 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. When used with the}j$<$tt$>$(\+JSON) type specifier, the padding length specifies the tab size used for indentation.
\item An optional precision modifier, consisting of a.{\ttfamily (dot) followed by a number, that says how many digits should be displayed for floating point numbers. When used with the}g\`{} type specifier, it specifies the number of significant digits. When used on a string, it causes the result to be truncated.
\item A type specifier that can be any of\+:
\begin{DoxyItemize}
\item {\ttfamily \%} — yields a literal {\ttfamily \%} character
\item {\ttfamily b} — yields an integer as a binary number
\item {\ttfamily c} — yields an integer as the character with that ASCII value
\item {\ttfamily d} or {\ttfamily i} — yields an integer as a signed decimal number
\item {\ttfamily e} — yields a float using scientific notation
\item {\ttfamily u} — yields an integer as an unsigned decimal number
\item {\ttfamily f} — yields a float as is; see notes on precision above
\item {\ttfamily g} — yields a float as is; see notes on precision above
\item {\ttfamily o} — yields an integer as an octal number
\item {\ttfamily s} — yields a string as is
\item {\ttfamily x} — yields an integer as a hexadecimal number (lower-\/case)
\item {\ttfamily X} — yields an integer as a hexadecimal number (upper-\/case)
\item {\ttfamily j} — yields a Java\+Script object or array as a JSON encoded string
\end{DoxyItemize}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_sprintf_js_README_autotoc_md28680}{}\doxysection{Java\+Script $<$tt$>$vsprintf$<$/tt$>$}\label{md_sfcode_node_modules_sprintf_js_README_autotoc_md28680}
{\ttfamily vsprintf} is the same as {\ttfamily sprintf} except that it accepts an array of arguments, rather than a variable number of arguments\+: \begin{DoxyVerb}vsprintf("The first 4 letters of the english alphabet are: %s, %s, %s and %s", ["a", "b", "c", "d"])
\end{DoxyVerb}
 \hypertarget{md_sfcode_node_modules_sprintf_js_README_autotoc_md28681}{}\doxysection{Argument swapping}\label{md_sfcode_node_modules_sprintf_js_README_autotoc_md28681}
You can also swap the arguments. That is, the order of the placeholders doesn\textquotesingle{}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\+: \begin{DoxyVerb}sprintf("%2$s %3$s a %1$s", "cracker", "Polly", "wants")
\end{DoxyVerb}
 And, of course, you can repeat the placeholders without having to increase the number of arguments.\hypertarget{md_sfcode_node_modules_sprintf_js_README_autotoc_md28682}{}\doxysection{Named arguments}\label{md_sfcode_node_modules_sprintf_js_README_autotoc_md28682}
Format strings may contain replacement fields rather than positional placeholders. Instead of referring to a certain argument, you can now refer to a certain key within an object. Replacement fields are surrounded by rounded parentheses -\/ {\ttfamily (} and {\ttfamily )} -\/ and begin with a keyword that refers to a key\+: \begin{DoxyVerb}var user = {
    name: "Dolly"
}
sprintf("Hello %(name)s", user) // Hello Dolly
\end{DoxyVerb}
 Keywords in replacement fields can be optionally followed by any number of keywords or indexes\+: \begin{DoxyVerb}var users = [
    {name: "Dolly"},
    {name: "Molly"},
    {name: "Polly"}
]
sprintf("Hello %(users[0].name)s, %(users[1].name)s and %(users[2].name)s", {users: users}) // Hello Dolly, Molly and Polly
\end{DoxyVerb}
 Note\+: mixing positional and named placeholders is not (yet) supported\hypertarget{md_sfcode_node_modules_sprintf_js_README_autotoc_md28683}{}\doxysection{Computed values}\label{md_sfcode_node_modules_sprintf_js_README_autotoc_md28683}
You can pass in a function as a dynamic value and it will be invoked (with no arguments) in order to compute the value on-\/the-\/fly. \begin{DoxyVerb}sprintf("Current timestamp: %d", Date.now) // Current timestamp: 1398005382890
sprintf("Current date and time: %s", function() { return new Date().toString() })
\end{DoxyVerb}
 \hypertarget{md_sfcode_node_modules_sprintf_js_README_autotoc_md28684}{}\doxysection{Angular\+JS}\label{md_sfcode_node_modules_sprintf_js_README_autotoc_md28684}
You can now use {\ttfamily sprintf} and {\ttfamily vsprintf} (also aliased as {\ttfamily fmt} and {\ttfamily vfmt} respectively) in your Angular\+JS projects. See {\ttfamily demo/}.\hypertarget{md_sfcode_node_modules_sprintf_js_README_autotoc_md28685}{}\doxysection{Installation}\label{md_sfcode_node_modules_sprintf_js_README_autotoc_md28685}
\hypertarget{md_sfcode_node_modules_sprintf_js_README_autotoc_md28686}{}\doxysubsection{Via Bower}\label{md_sfcode_node_modules_sprintf_js_README_autotoc_md28686}
\begin{DoxyVerb}bower install sprintf
\end{DoxyVerb}
 \hypertarget{md_sfcode_node_modules_sprintf_js_README_autotoc_md28687}{}\doxysubsection{Or as a node.\+js module}\label{md_sfcode_node_modules_sprintf_js_README_autotoc_md28687}
\begin{DoxyVerb}npm install sprintf-js
\end{DoxyVerb}
 \hypertarget{md_sfcode_node_modules_sprintf_js_README_autotoc_md28688}{}\doxysubsubsection{Usage}\label{md_sfcode_node_modules_sprintf_js_README_autotoc_md28688}
\begin{DoxyVerb}var sprintf = require("sprintf-js").sprintf,
    vsprintf = require("sprintf-js").vsprintf

sprintf("%2$s %3$s a %1$s", "cracker", "Polly", "wants")
vsprintf("The first 4 letters of the english alphabet are: %s, %s, %s and %s", ["a", "b", "c", "d"])
\end{DoxyVerb}
 \hypertarget{md_sfcode_node_modules_sprintf_js_README_autotoc_md28689}{}\doxysection{License}\label{md_sfcode_node_modules_sprintf_js_README_autotoc_md28689}
{\bfseries{sprintf.\+js}} is licensed under the terms of the 3-\/clause BSD license. 