UNPKG

14.7 kBTypeScriptView Raw
1// Last module patch version validated against: 3.0.1
2
3/**
4 * Specification of locale to use when creating a new FormatLocaleObject
5 */
6export interface FormatLocaleDefinition {
7 /**
8 * The decimal point (e.g., ".")
9 */
10 decimal: string;
11 /**
12 * The group separator (e.g., ","). Note that the thousands property is a misnomer, as
13 * the grouping definition allows groups other than thousands.
14 */
15 thousands: string;
16 /**
17 * The array of group sizes (e.g., [3]), cycled as needed.
18 */
19 grouping: number[];
20 /**
21 * The currency prefix and suffix (e.g., ["$", ""]).
22 */
23 currency: [string, string];
24 /**
25 * An optional array of ten strings to replace the numerals 0-9.
26 */
27 numerals?: string[] | undefined;
28 /**
29 * An optional symbol to replace the `percent` suffix; the percent suffix (defaults to "%").
30 */
31 percent?: string | undefined;
32 /**
33 * Optional; the minus sign (defaults to "−").
34 */
35 minus?: string | undefined;
36 /**
37 * Optional; the not-a-number value (defaults "NaN").
38 */
39 nan?: string | undefined;
40}
41
42/**
43 * A Format Locale Object
44 */
45export interface FormatLocaleObject {
46 /**
47 * Returns a new format function for the given string specifier. The returned function
48 * takes a number as the only argument, and returns a string representing the formatted number.
49 *
50 * @param specifier A Specifier string.
51 * @throws Error on invalid format specifier.
52 */
53 format(specifier: string): (n: number | { valueOf(): number }) => string;
54
55 /**
56 * Returns a new format function for the given string specifier. The returned function
57 * takes a number as the only argument, and returns a string representing the formatted number.
58 * The returned function will convert values to the units of the appropriate SI prefix for the
59 * specified numeric reference value before formatting in fixed point notation.
60 *
61 * @param specifier A Specifier string.
62 * @param value The reference value to determine the appropriate SI prefix.
63 * @throws Error on invalid format specifier.
64 */
65 formatPrefix(specifier: string, value: number): (n: number | { valueOf(): number }) => string;
66}
67
68/**
69 * A Format Specifier
70 *
71 * For details see: {@link https://github.com/d3/d3-format#locale_format}
72 */
73export interface FormatSpecifierObject {
74 /**
75 * fill can be any character. The presence of a fill character is signaled by the align character following it.
76 */
77 fill?: string | undefined;
78 /**
79 * Alignment used for format, as set by choosing one of the following:
80 *
81 * '>' - Forces the field to be right-aligned within the available space. (Default behavior).
82 * '<' - Forces the field to be left-aligned within the available space.
83 * '^' - Forces the field to be centered within the available space.
84 * '=' - Like '>', but with any sign and symbol to the left of any padding.
85 */
86 align?: string | undefined;
87 /**
88 * The sign can be:
89 *
90 * '-' - nothing for positive and a minus sign for negative. (Default behavior.)
91 * '+' - a plus sign for positive and a minus sign for negative.
92 * '(' - nothing for positive and parentheses for negative.
93 * ' ' (space) - a space for positive and a minus sign for negative.
94 */
95 sign?: string | undefined;
96 /**
97 * The symbol can be:
98 *
99 * '$' - apply currency symbols per the locale definition.
100 * '#' - for binary, octal, or hexadecimal notation, prefix by 0b, 0o, or 0x, respectively.
101 * '' (none) - no symbol. (Default behavior.)
102 */
103 symbol?: string | undefined;
104 /**
105 * The zero (0) option enables zero-padding; this implicitly sets fill to 0 and align to =.
106 */
107 zero?: string | undefined;
108 /**
109 * The width defines the minimum field width;
110 * if not specified, then the width will be determined by the content.
111 */
112 width?: string | undefined;
113 /**
114 * The comma (,) option enables the use of a group separator, such as a comma for thousands.
115 */
116 comma?: string | undefined;
117 /**
118 * Depending on the type, the precision either indicates the number of digits that follow the decimal point (types 'f' and '%'),
119 * or the number of significant digits (types '' (none), 'e', 'g', 'r', 's' and 'p'). If the precision is not specified,
120 * it defaults to 6 for all types except '' (none), which defaults to 12.
121 * Precision is ignored for integer formats (types 'b', 'o', 'd', 'x', 'X' and 'c').
122 *
123 * See precisionFixed and precisionRound for help picking an appropriate precision.
124 */
125 precision?: string | undefined;
126 /**
127 * The '~' option trims insignificant trailing zeros across all format types.
128 * This is most commonly used in conjunction with types 'r', 'e', 's' and '%'.
129 */
130 trim?: string | undefined;
131 /**
132 * The available type values are:
133 *
134 * 'e' - exponent notation.
135 * 'f' - fixed point notation.
136 * 'g' - either decimal or exponent notation, rounded to significant digits.
137 * 'r' - decimal notation, rounded to significant digits.
138 * 's' - decimal notation with an SI prefix, rounded to significant digits.
139 * '%' - multiply by 100, and then decimal notation with a percent sign.
140 * 'p' - multiply by 100, round to significant digits, and then decimal notation with a percent sign.
141 * 'b' - binary notation, rounded to integer.
142 * 'o' - octal notation, rounded to integer.
143 * 'd' - decimal notation, rounded to integer.
144 * 'x' - hexadecimal notation, using lower-case letters, rounded to integer.
145 * 'X' - hexadecimal notation, using upper-case letters, rounded to integer.
146 * 'c' - converts the integer to the corresponding unicode character before printing.
147 *
148 * The type '' (none) is also supported as shorthand for '~g' (with a default precision of 12 instead of 6), and
149 * the type 'n' is shorthand for ',g'. For the 'g', 'n' and '' (none) types,
150 * decimal notation is used if the resulting string would have precision or fewer digits; otherwise, exponent notation is used.
151 */
152 type?: string | undefined;
153}
154
155/**
156 * Create a new locale-based object which exposes format(...) and formatPrefix(...)
157 * methods for the specified locale.
158 *
159 * @param locale A Format locale definition.
160 */
161export function formatLocale(locale: FormatLocaleDefinition): FormatLocaleObject;
162
163/**
164 * Create a new locale-based object which exposes format(...) and formatPrefix(...)
165 * methods for the specified locale definition. The specified locale definition will be
166 * set as the new default locale definition.
167 *
168 * @param defaultLocale A Format locale definition to be used as default.
169 */
170export function formatDefaultLocale(defaultLocale: FormatLocaleDefinition): FormatLocaleObject;
171
172/**
173 * Returns a new format function for the given string specifier. The returned function
174 * takes a number as the only argument, and returns a string representing the formatted number.
175 *
176 * Uses the current default locale.
177 *
178 * The general form of a specifier is [[fill]align][sign][symbol][0][width][,][.precision][~][type].
179 * For reference, an explanation of the segments of the specifier string, refer to the FormatSpecifier interface properties.
180 *
181 * @param specifier A Specifier string.
182 * @throws Error on invalid format specifier.
183 */
184export function format(specifier: string): (n: number | { valueOf(): number }) => string;
185
186/**
187 * Returns a new format function for the given string specifier. The returned function
188 * takes a number as the only argument, and returns a string representing the formatted number.
189 * The returned function will convert values to the units of the appropriate SI prefix for the
190 * specified numeric reference value before formatting in fixed point notation.
191 *
192 * Uses the current default locale.
193 *
194 * The general form of a specifier is [[fill]align][sign][symbol][0][width][,][.precision][~][type].
195 * For reference, an explanation of the segments of the specifier string, refer to the FormatSpecifier interface properties.
196 *
197 * @param specifier A Specifier string.
198 * @param value The reference value to determine the appropriate SI prefix.
199 * @throws Error on invalid format specifier.
200 */
201export function formatPrefix(specifier: string, value: number): (n: number | { valueOf(): number }) => string;
202
203/**
204 * A Format Specifier
205 *
206 * For details see: {@link https://github.com/d3/d3-format#locale_format}
207 */
208export class FormatSpecifier {
209 /**
210 * Given the specified specifier object, returning an object with exposed fields that correspond to the format specification mini-language and a toString method that reconstructs the specifier.
211 * @param specifier A specifier object.
212 */
213 constructor(specifier: FormatSpecifierObject);
214 /**
215 * fill can be any character. The presence of a fill character is signaled by the align character following it.
216 */
217 fill: string;
218 /**
219 * Alignment used for format, as set by choosing one of the following:
220 *
221 * '>' - Forces the field to be right-aligned within the available space. (Default behavior).
222 * '<' - Forces the field to be left-aligned within the available space.
223 * '^' - Forces the field to be centered within the available space.
224 * '=' - Like '>', but with any sign and symbol to the left of any padding.
225 */
226 align: ">" | "<" | "^" | "=";
227 /**
228 * The sign can be:
229 *
230 * '-' - nothing for positive and a minus sign for negative. (Default behavior.)
231 * '+' - a plus sign for positive and a minus sign for negative.
232 * '(' - nothing for positive and parentheses for negative.
233 * ' ' (space) - a space for positive and a minus sign for negative.
234 */
235 sign: "-" | "+" | "(" | " ";
236 /**
237 * The symbol can be:
238 *
239 * '$' - apply currency symbols per the locale definition.
240 * '#' - for binary, octal, or hexadecimal notation, prefix by 0b, 0o, or 0x, respectively.
241 * '' (none) - no symbol. (Default behavior.)
242 */
243 symbol: "$" | "#" | "";
244 /**
245 * The zero (0) option enables zero-padding; this implicitly sets fill to 0 and align to =.
246 */
247 zero: boolean;
248 /**
249 * The width defines the minimum field width;
250 * if not specified, then the width will be determined by the content.
251 */
252 width: number | undefined;
253 /**
254 * The comma (,) option enables the use of a group separator, such as a comma for thousands.
255 */
256 comma: boolean;
257 /**
258 * Depending on the type, the precision either indicates the number of digits that follow the decimal point (types 'f' and '%'),
259 * or the number of significant digits (types '' (none), 'e', 'g', 'r', 's' and 'p'). If the precision is not specified,
260 * it defaults to 6 for all types except '' (none), which defaults to 12.
261 * Precision is ignored for integer formats (types 'b', 'o', 'd', 'x', 'X' and 'c').
262 *
263 * See precisionFixed and precisionRound for help picking an appropriate precision.
264 */
265 precision: number | undefined;
266 /**
267 * The '~' option trims insignificant trailing zeros across all format types.
268 * This is most commonly used in conjunction with types 'r', 'e', 's' and '%'.
269 */
270 trim: boolean;
271 /**
272 * The available type values are:
273 *
274 * 'e' - exponent notation.
275 * 'f' - fixed point notation.
276 * 'g' - either decimal or exponent notation, rounded to significant digits.
277 * 'r' - decimal notation, rounded to significant digits.
278 * 's' - decimal notation with an SI prefix, rounded to significant digits.
279 * '%' - multiply by 100, and then decimal notation with a percent sign.
280 * 'p' - multiply by 100, round to significant digits, and then decimal notation with a percent sign.
281 * 'b' - binary notation, rounded to integer.
282 * 'o' - octal notation, rounded to integer.
283 * 'd' - decimal notation, rounded to integer.
284 * 'x' - hexadecimal notation, using lower-case letters, rounded to integer.
285 * 'X' - hexadecimal notation, using upper-case letters, rounded to integer.
286 * 'c' - converts the integer to the corresponding unicode character before printing.
287 *
288 * The type '' (none) is also supported as shorthand for '~g' (with a default precision of 12 instead of 6), and
289 * the type 'n' is shorthand for ',g'. For the 'g', 'n' and '' (none) types,
290 * decimal notation is used if the resulting string would have precision or fewer digits; otherwise, exponent notation is used.
291 */
292 type: "e" | "f" | "g" | "r" | "s" | "%" | "p" | "b" | "o" | "d" | "x" | "X" | "c" | "" | "n";
293 /**
294 * Return the object as a specifier string.
295 */
296 toString(): string;
297}
298
299/**
300 * Parses the specified specifier, returning an object with exposed fields that correspond to the
301 * format specification mini-language and a toString method that reconstructs the specifier.
302 *
303 * The general form of a specifier is [[fill]align][sign][symbol][0][width][,][.precision][~][type].
304 * For reference, an explanation of the segments of the specifier string, refer to the FormatSpecifier interface properties.
305 *
306 * @param specifier A specifier string.
307 * @throws Error on invalid format specifier.
308 */
309export function formatSpecifier(specifier: string): FormatSpecifier;
310
311/**
312 * Returns a suggested decimal precision for fixed point notation given the specified numeric step value.
313 *
314 * @param step The step represents the minimum absolute difference between values that will be formatted.
315 * (This assumes that the values to be formatted are also multiples of step.)
316 */
317export function precisionFixed(step: number): number;
318
319/**
320 * Returns a suggested decimal precision for use with locale.formatPrefix given the specified
321 * numeric step and reference value.
322 *
323 * @param step The step represents the minimum absolute difference between values that will be formatted.
324 * (This assumes that the values to be formatted are also multiples of step.)
325 * @param value Reference value determines which SI prefix will be used.
326 */
327export function precisionPrefix(step: number, value: number): number;
328
329/**
330 * Returns a suggested decimal precision for format types that round to significant digits
331 * given the specified numeric step and max values.
332 *
333 * @param step The step represents the minimum absolute difference between values that will be formatted.
334 * (This assumes that the values to be formatted are also multiples of step.)
335 * @param max max represents the largest absolute value that will be formatted.
336 */
337export function precisionRound(step: number, max: number): number;