UNPKG

9.13 kBTypeScriptView Raw
1// Type definitions for sprintf-js 1.1
2// Project: https://github.com/alexei/sprintf.js
3// Definitions by: Jason Swearingen <https://github.com/jasonswearingen>
4// BendingBender <https://github.com/BendingBender>
5// Caner Dagli <https://github.com/cdagli>
6// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7
8/**
9 * Returns a formatted string:
10 *
11 * string sprintf(string format, mixed arg1?, mixed arg2?, ...)
12 *
13 * ### Argument swapping
14 *
15 * You can also swap the arguments. That is, the order of the placeholders doesn't have to match the order of the arguments.
16 * You can do that by simply indicating in the format string which arguments the placeholders refer to:
17 *
18 * sprintf('%2$s %3$s a %1$s', 'cracker', 'Polly', 'wants')
19 *
20 * And, of course, you can repeat the placeholders without having to increase the number of arguments.
21 *
22 * ### Named arguments
23 *
24 * Format strings may contain replacement fields rather than positional placeholders. Instead of referring to a certain argument,
25 * you can now refer to a certain key within an object. Replacement fields are surrounded by rounded parentheses - `(` and `)` -
26 * and begin with a keyword that refers to a key:
27 *
28 * var user = {
29 * name: 'Dolly',
30 * }
31 * sprintf('Hello %(name)s', user) // Hello Dolly
32 *
33 * Keywords in replacement fields can be optionally followed by any number of keywords or indexes:
34 *
35 * var users = [
36 * {name: 'Dolly'},
37 * {name: 'Molly'},
38 * {name: 'Polly'},
39 * ]
40 * sprintf('Hello %(users[0].name)s, %(users[1].name)s and %(users[2].name)s', {users: users}) // Hello Dolly, Molly and Polly
41 *
42 * Note: mixing positional and named placeholders is not (yet) supported
43 *
44 * ### Computed values
45 *
46 * 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.
47 *
48 * sprintf('Current date and time: %s', function() { return new Date().toString() })
49 *
50 * @param format: format string
51 * The placeholders in the format string are marked by `%` and are followed by one or more of these elements, in this order:
52 * * An optional number followed by a `$` sign that selects which argument index to use for the value. If not specified,
53 * arguments will be placed in the same order as the placeholders in the input string.
54 * * An optional `+` sign that forces to preceed the result with a plus or minus sign on numeric values. By default,
55 * only the `-` sign is used on negative numbers.
56 * * An optional padding specifier that says what character to use for padding (if specified). Possible values are
57 * `0` or any other character precedeed by a `'` (single quote). The default is to pad with *spaces*.
58 * * An optional `-` sign, that causes `sprintf` to left-align the result of this placeholder. The default is to right-align the result.
59 * * An optional number, that says how many characters the result should have. If the value to be returned is shorter
60 * than this number, the result will be padded. When used with the `j` (JSON) type specifier, the padding length
61 * specifies the tab size used for indentation.
62 * * An optional precision modifier, consisting of a `.` (dot) followed by a number, that says how many digits should be
63 * displayed for floating point numbers. When used with the `g` type specifier, it specifies the number of significant
64 * digits. When used on a string, it causes the result to be truncated.
65 * * A type specifier that can be any of:
66 * * `%` — yields a literal `%` character
67 * * `b` — yields an integer as a binary number
68 * * `c` — yields an integer as the character with that ASCII value
69 * * `d` or `i` — yields an integer as a signed decimal number
70 * * `e` — yields a float using scientific notation
71 * * `u` — yields an integer as an unsigned decimal number
72 * * `f` — yields a float as is; see notes on precision above
73 * * `g` — yields a float as is; see notes on precision above
74 * * `o` — yields an integer as an octal number
75 * * `s` — yields a string as is
76 * * `t` — yields `true` or `false`
77 * * `T` — yields the type of the argument<sup><a href="#fn-1" name="fn-ref-1">1</a></sup>
78 * * `v` — yields the primitive value of the specified argument
79 * * `x` — yields an integer as a hexadecimal number (lower-case)
80 * * `X` — yields an integer as a hexadecimal number (upper-case)
81 * * `j` — yields a JavaScript object or array as a JSON encoded string
82 * @param args: the arguments for the format string
83 */
84export function sprintf(format: string, ...args: any[]): string;
85
86/**
87 * Same as `sprintf` except it takes an array of arguments, rather than a variable number of arguments:
88 *
89 * string vsprintf(string format, array arguments?)
90 *
91 * ### Argument swapping
92 *
93 * You can also swap the arguments. That is, the order of the placeholders doesn't have to match the order of the arguments.
94 * You can do that by simply indicating in the format string which arguments the placeholders refer to:
95 *
96 * sprintf('%2$s %3$s a %1$s', 'cracker', 'Polly', 'wants')
97 *
98 * And, of course, you can repeat the placeholders without having to increase the number of arguments.
99 *
100 * ### Named arguments
101 *
102 * Format strings may contain replacement fields rather than positional placeholders. Instead of referring to a certain argument,
103 * you can now refer to a certain key within an object. Replacement fields are surrounded by rounded parentheses - `(` and `)` -
104 * and begin with a keyword that refers to a key:
105 *
106 * var user = {
107 * name: 'Dolly',
108 * }
109 * sprintf('Hello %(name)s', user) // Hello Dolly
110 *
111 * Keywords in replacement fields can be optionally followed by any number of keywords or indexes:
112 *
113 * var users = [
114 * {name: 'Dolly'},
115 * {name: 'Molly'},
116 * {name: 'Polly'},
117 * ]
118 * sprintf('Hello %(users[0].name)s, %(users[1].name)s and %(users[2].name)s', {users: users}) // Hello Dolly, Molly and Polly
119 *
120 * Note: mixing positional and named placeholders is not (yet) supported
121 *
122 * ### Computed values
123 *
124 * 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.
125 *
126 * sprintf('Current date and time: %s', function() { return new Date().toString() })
127 *
128 * @param format: format string
129 *
130 * The placeholders in the format string are marked by `%` and are followed by one or more of these elements, in this order:
131 *
132 * * An optional number followed by a `$` sign that selects which argument index to use for the value. If not specified,
133 * arguments will be placed in the same order as the placeholders in the input string.
134 * * An optional `+` sign that forces to preceed the result with a plus or minus sign on numeric values. By default,
135 * only the `-` sign is used on negative numbers.
136 * * An optional padding specifier that says what character to use for padding (if specified). Possible values are
137 * `0` or any other character precedeed by a `'` (single quote). The default is to pad with *spaces*.
138 * * An optional `-` sign, that causes `sprintf` to left-align the result of this placeholder. The default is to right-align the result.
139 * * An optional number, that says how many characters the result should have. If the value to be returned is shorter
140 * than this number, the result will be padded. When used with the `j` (JSON) type specifier, the padding length
141 * specifies the tab size used for indentation.
142 * * An optional precision modifier, consisting of a `.` (dot) followed by a number, that says how many digits should be
143 * displayed for floating point numbers. When used with the `g` type specifier, it specifies the number of significant
144 * digits. When used on a string, it causes the result to be truncated.
145 * * A type specifier that can be any of:
146 * * `%` — yields a literal `%` character
147 * * `b` — yields an integer as a binary number
148 * * `c` — yields an integer as the character with that ASCII value
149 * * `d` or `i` — yields an integer as a signed decimal number
150 * * `e` — yields a float using scientific notation
151 * * `u` — yields an integer as an unsigned decimal number
152 * * `f` — yields a float as is; see notes on precision above
153 * * `g` — yields a float as is; see notes on precision above
154 * * `o` — yields an integer as an octal number
155 * * `s` — yields a string as is
156 * * `t` — yields `true` or `false`
157 * * `T` — yields the type of the argument<sup><a href="#fn-1" name="fn-ref-1">1</a></sup>
158 * * `v` — yields the primitive value of the specified argument
159 * * `x` — yields an integer as a hexadecimal number (lower-case)
160 * * `X` — yields an integer as a hexadecimal number (upper-case)
161 * * `j` — yields a JavaScript object or array as a JSON encoded string
162 * @param args: the arguments for the format string
163 */
164export function vsprintf(format: string, args: any[]): string;