UNPKG

3.24 kBTypeScriptView Raw
1export type Options = {
2 /**
3 Include plus sign for positive numbers. If the difference is exactly zero a space character will be prepended instead for better alignment.
4
5 @default false
6 */
7 readonly signed?: boolean;
8
9 /**
10 - If `false`: Output won't be localized.
11 - If `true`: Localize the output using the system/browser locale.
12 - If `string`: Expects a [BCP 47 language tag](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
13 - If `string[]`: Expects a list of [BCP 47 language tags](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
14
15 @default false
16 */
17 readonly locale?: boolean | string | readonly string[];
18
19 /**
20 Format the number as [bits](https://en.wikipedia.org/wiki/Bit) instead of [bytes](https://en.wikipedia.org/wiki/Byte). This can be useful when, for example, referring to [bit rate](https://en.wikipedia.org/wiki/Bit_rate).
21
22 @default false
23
24 @example
25 ```
26 import prettyBytes from 'pretty-bytes';
27
28 prettyBytes(1337, {bits: true});
29 //=> '1.34 kbit'
30 ```
31 */
32 readonly bits?: boolean;
33
34 /**
35 Format the number using the [Binary Prefix](https://en.wikipedia.org/wiki/Binary_prefix) instead of the [SI Prefix](https://en.wikipedia.org/wiki/SI_prefix). This can be useful for presenting memory amounts. However, this should not be used for presenting file sizes.
36
37 @default false
38
39 @example
40 ```
41 import prettyBytes from 'pretty-bytes';
42
43 prettyBytes(1000, {binary: true});
44 //=> '1000 bit'
45
46 prettyBytes(1024, {binary: true});
47 //=> '1 kiB'
48 ```
49 */
50 readonly binary?: boolean;
51
52 /**
53 The minimum number of fraction digits to display.
54
55 If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
56
57 @default undefined
58
59 @example
60 ```
61 import prettyBytes from 'pretty-bytes';
62
63 // Show the number with at least 3 fractional digits
64 prettyBytes(1900, {minimumFractionDigits: 3});
65 //=> '1.900 kB'
66
67 prettyBytes(1900);
68 //=> '1.9 kB'
69 ```
70 */
71 readonly minimumFractionDigits?: number;
72
73 /**
74 The maximum number of fraction digits to display.
75
76 If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits.
77
78 @default undefined
79
80 @example
81 ```
82 import prettyBytes from 'pretty-bytes';
83
84 // Show the number with at most 1 fractional digit
85 prettyBytes(1920, {maximumFractionDigits: 1});
86 //=> '1.9 kB'
87
88 prettyBytes(1920);
89 //=> '1.92 kB'
90 ```
91 */
92 readonly maximumFractionDigits?: number;
93
94 /**
95 Put a space between the number and unit.
96
97 @default true
98
99 @example
100 ```
101 import prettyBytes from 'pretty-bytes';
102
103 prettyBytes(1920, {space: false});
104 //=> '1.9kB'
105
106 prettyBytes(1920);
107 //=> '1.92 kB'
108 ```
109 */
110 readonly space?: boolean;
111};
112
113/**
114Convert bytes to a human readable string: `1337` → `1.34 kB`.
115
116@param number - The number to format.
117
118@example
119```
120import prettyBytes from 'pretty-bytes';
121
122prettyBytes(1337);
123//=> '1.34 kB'
124
125prettyBytes(100);
126//=> '100 B'
127
128// Display file size differences
129prettyBytes(42, {signed: true});
130//=> '+42 B'
131
132// Localized output using German locale
133prettyBytes(1337, {locale: 'de'});
134//=> '1,34 kB'
135```
136*/
137export default function prettyBytes(
138 number: number,
139 options?: Options
140): string;