UNPKG

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