\href{https://travis-ci.org/alexei/sprintf.js}{\texttt{ }} \href{https://badge.fury.io/js/sprintf-js}{\texttt{ }} \href{https://david-dm.org/alexei/sprintf.js}{\texttt{ }} \href{https://david-dm.org/alexei/sprintf.js\#info=devDependencies}{\texttt{ }}

{\bfseries{sprintf-\/js}} is a complete open source Java\+Script {\ttfamily sprintf} implementation for the {\bfseries{browser}} and {\bfseries{Node.\+js}}.

{\bfseries{Note\+: as of v1.\+1.\+1 you might need some polyfills for older environments. See \href{\#support}{\texttt{ Support}} section below.}}\hypertarget{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5470}{}\doxysection{Usage}\label{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5470}
\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_codelyzer_node_modules_sprintf_js_README_autotoc_md5471}{}\doxysection{Installation}\label{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5471}
\hypertarget{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5472}{}\doxysubsection{NPM}\label{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5472}
\begin{DoxyVerb}npm install sprintf-js
\end{DoxyVerb}
 \hypertarget{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5473}{}\doxysubsection{Bower}\label{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5473}
\begin{DoxyVerb}bower install sprintf
\end{DoxyVerb}
 \hypertarget{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5474}{}\doxysection{API}\label{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5474}
\hypertarget{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5475}{}\doxysubsection{$<$tt$>$sprintf$<$/tt$>$}\label{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5475}
Returns a formatted string\+: \begin{DoxyVerb}string sprintf(string format, mixed arg1?, mixed arg2?, ...)
\end{DoxyVerb}
 \hypertarget{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5476}{}\doxysubsection{$<$tt$>$vsprintf$<$/tt$>$}\label{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5476}
Same as {\ttfamily sprintf} except it takes an array of arguments, rather than a variable number of arguments\+: \begin{DoxyVerb}string vsprintf(string format, array arguments?)
\end{DoxyVerb}
 \hypertarget{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5477}{}\doxysection{Format specification}\label{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5477}
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{\ttfamily 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 t} — yields {\ttfamily true} or {\ttfamily false}
\item {\ttfamily T} — yields the type of the argument\textsuperscript{\href{\#fn-1}{\texttt{ 1}}}
\item {\ttfamily v} — yields the primitive value of the specified argument
\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_codelyzer_node_modules_sprintf_js_README_autotoc_md5478}{}\doxysection{Features}\label{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5478}
\hypertarget{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5479}{}\doxysubsection{Argument swapping}\label{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5479}
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_codelyzer_node_modules_sprintf_js_README_autotoc_md5480}{}\doxysubsection{Named arguments}\label{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5480}
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_codelyzer_node_modules_sprintf_js_README_autotoc_md5481}{}\doxysubsection{Computed values}\label{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5481}
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 date and time: %s', function() { return new Date().toString() })
\end{DoxyVerb}
 \hypertarget{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5482}{}\doxysubsection{Angular\+JS}\label{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5482}
You can 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_codelyzer_node_modules_sprintf_js_README_autotoc_md5483}{}\doxysection{Support}\label{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5483}
\hypertarget{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5484}{}\doxysubsection{Node.\+js}\label{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5484}
{\ttfamily sprintf-\/js} runs in all active Node versions (4.\+x+).\hypertarget{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5485}{}\doxysubsection{Browser}\label{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5485}
{\ttfamily sprintf-\/js} should work in all modern browsers. As of v1.\+1.\+1, you might need polyfills for the following\+:


\begin{DoxyItemize}
\item {\ttfamily String.\+prototype.\+repeat()} (any IE)
\item {\ttfamily Array.\+is\+Array()} (IE $<$ 9)
\item {\ttfamily Object.\+create()} (IE $<$ 9)
\end{DoxyItemize}

YMMV\hypertarget{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5486}{}\doxysection{License}\label{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5486}
{\bfseries{sprintf-\/js}} is licensed under the terms of the 3-\/clause BSD license.\hypertarget{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5487}{}\doxysection{Notes}\label{md_sfcode_node_modules_codelyzer_node_modules_sprintf_js_README_autotoc_md5487}

\footnotesize \textsuperscript{\href{\#fn-ref-1}{\texttt{ 1}}} {\ttfamily sprintf} doesn\textquotesingle{}t use the {\ttfamily typeof} operator. As such, the value {\ttfamily null} is a {\ttfamily null}, an array is an {\ttfamily array} (not an {\ttfamily object}), a date value is a {\ttfamily date} etc.
\normalsize  