-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathindex.d.ts
More file actions
183 lines (170 loc) · 4.63 KB
/
index.d.ts
File metadata and controls
183 lines (170 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Unit identifiers for byte sizes (lowercase).
*/
type BytesUnit = "b" | "kb" | "mb" | "gb" | "tb" | "pb";
/**
* Unit identifiers for byte sizes (uppercase).
*/
type BytesUnitUpper = "B" | "KB" | "MB" | "GB" | "TB" | "PB";
/**
* All valid unit identifier variations.
*/
type BytesUnitAny = BytesUnit | BytesUnitUpper;
/**
* Valid byte size string formats that can be parsed.
*
* @example
* '50mb'
* '1.5gb'
* '100 KB'
* '-5mb'
* '+10gb'
*/
type BytesString =
| `${number}${BytesUnitAny}`
| `${number} ${BytesUnitAny}`
| `${number}.${number}${BytesUnitAny}`
| `${number}.${number} ${BytesUnitAny}`
| `+${number}${BytesUnitAny}`
| `+${number} ${BytesUnitAny}`
| `+${number}.${number}${BytesUnitAny}`
| `+${number}.${number} ${BytesUnitAny}`
| `-${number}${BytesUnitAny}`
| `-${number} ${BytesUnitAny}`
| `-${number}.${number}${BytesUnitAny}`
| `-${number}.${number} ${BytesUnitAny}`;
/**
* Options for formatting bytes into a human-readable string.
*/
interface BytesOptions {
/**
* Number of decimal places to include in the output.
*
* @default 2
*
* @example
* bytes(1024, { decimalPlaces: 0 }); // '1KB'
* bytes(1024, { decimalPlaces: 2 }); // '1.00KB'
*/
decimalPlaces?: number;
/**
* Whether to always include the specified number of decimal places,
* even if they are zero.
*
* @default false
*
* @example
* bytes(1024, { fixedDecimals: true, decimalPlaces: 2 }); // '1.00KB'
* bytes(1024, { fixedDecimals: false, decimalPlaces: 2 }); // '1KB'
*/
fixedDecimals?: boolean;
/**
* Character to use as a thousands separator in the output.
*
* @default ''
*
* @example
* bytes(1000000, { thousandsSeparator: ',' }); // '976.56KB' or '1,000,000B'
*/
thousandsSeparator?: string;
/**
* The unit to use for formatting.
* If not specified, the most appropriate unit is chosen automatically.
*
* @example
* bytes(1024, { unit: 'kb' }); // '1KB'
* bytes(1048576, { unit: 'kb' }); // '1024KB'
*/
unit?: BytesUnitAny;
/**
* Character to use as a separator between the numeric value and the unit.
*
* @default ''
*
* @example
* bytes(1024, { unitSeparator: ' ' }); // '1 KB'
* bytes(1024, { unitSeparator: '' }); // '1KB'
*/
unitSeparator?: string;
}
/**
* Convert a human-readable byte size string to bytes as an integer.
*
* If the input is a number, it is returned as-is.
* If the input is an invalid string format, returns `null`.
*
* @param value - The value to parse (e.g., '5MB', '1.5GB', 1024)
* @returns The number of bytes, or `null` if parsing fails
*
* @example
* bytes.parse('1KB'); // 1024
* bytes.parse('1MB'); // 1048576
* bytes.parse('1GB'); // 1073741824
* bytes.parse('invalid'); // null
* bytes.parse(1024); // 1024
*/
declare function parse(value: BytesString): number | null;
declare function parse(value: number): number;
declare function parse(value: BytesString | number): number | null;
/**
* Format bytes as a human-readable string.
*
* If the value is not a finite number, returns `null`.
*
* @param value - The number of bytes to format
* @param options - Formatting options
* @returns A human-readable string (e.g., '5MB', '1.5GB'), or `null` if the value is invalid
*
* @example
* bytes.format(1024); // '1KB'
* bytes.format(1048576); // '1MB'
* bytes.format(1073741824, { decimalPlaces: 2 }); // '1.00GB'
* bytes.format(Infinity); // null
*/
declare function format(
value: number,
options?: BytesOptions
): BytesString | null;
/**
* Convert the given value to/from bytes.
*
* - If `value` is a string, it is parsed to bytes (integer).
* - If `value` is a number, it is formatted to a human-readable string.
* - Returns `null` if the conversion fails.
*
* @param value - A string to parse or a number to format
* @param options - Options for formatting (only used when `value` is a number)
* @returns The converted value, or `null` if conversion fails
*
* @example
* // Parsing string to bytes
* bytes('1KB'); // 1024
* bytes('1MB'); // 1048576
* bytes('invalid'); // null
*
* // Formatting number to string
* bytes(1024); // '1KB'
* bytes(1048576); // '1MB'
* bytes(1073741824, { decimalPlaces: 2, unitSeparator: ' ' }); // '1.00 GB'
*/
declare function bytes(value: BytesString): number | null;
declare function bytes(
value: number,
options?: BytesOptions
): BytesString | null;
declare function bytes(
value: BytesString | number,
options?: BytesOptions
): BytesString | number | null;
declare namespace bytes {
export {
parse,
format,
BytesOptions,
BytesString,
BytesUnit,
BytesUnitUpper,
BytesUnitAny,
};
}
export = bytes;