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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209 |
x8
x8
x8
x212
x219
x219
x212
x212
x213
x213
x212
x212
x212
x212
x248
x248
x248
x212
x212
x212
x212
x212
x212
x551
x551
x564
x564
x564
x564
x564
x212
x285
x285
x285
x372
x212
x204
x204
x204
x204
x204
x387
x387
x318
x305
x317
x317
x305
x310
x310
x318
x204
x8
x8
x8
x8
x204
x226
x204
x596
x596
x550
x204 |
|
// Copyright 2014-2021 Sindre Sorhus. All rights reserved. MIT license.
// Copyright 2021 Yoshiya Hinosawa. All rights reserved. MIT license.
// Copyright 2021 Giuseppe Eletto. All rights reserved. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
/**
* Convert bytes to a human-readable string: 1337 → 1.34 kB
*
* Based on {@link https://github.com/sindresorhus/pretty-bytes | pretty-bytes}.
* A utility for displaying file sizes for humans.
*
* ```ts
* import { format } from "@std/fmt/bytes";
* import { assertEquals } from "@std/assert";
*
* assertEquals(format(1337), "1.34 kB");
* assertEquals(format(100), "100 B");
* ```
* @module
*/
type LocaleOptions = {
minimumFractionDigits?: number;
maximumFractionDigits?: number;
};
/** Options for {@linkcode format}. */
export interface FormatOptions {
/**
* Uses bits representation.
*
* @default {false}
*/
bits?: boolean;
/**
* Uses binary bytes (e.g. kibibyte).
*
* @default {false}
*/
binary?: boolean;
/**
* Include plus sign for positive numbers.
*
* @default {false}
*/
signed?: boolean;
/**
* Uses localized number formatting. If it is set to true, uses default
* locale on the system. If it's set to string, uses that locale. The given
* string should be a
* {@link https://en.wikipedia.org/wiki/IETF_language_tag | BCP 47 language tag}.
* You can also give the list of language tags.
*/
locale?: boolean | string | string[];
/**
* The minimum number of fraction digits to display. If neither
* {@linkcode minimumFractionDigits} or {@linkcode maximumFractionDigits}
* are set.
*
* @default {3}
*/
minimumFractionDigits?: number;
/**
* The maximum number of fraction digits to display. If neither
* {@linkcode minimumFractionDigits} or {@linkcode maximumFractionDigits}
* are set.
*
* @default {3}
*/
maximumFractionDigits?: number;
}
/**
* Convert bytes to a human-readable string: 1337 → 1.34 kB
*
* Based on {@link https://github.com/sindresorhus/pretty-bytes | pretty-bytes}.
* A utility for displaying file sizes for humans.
*
* @param num The bytes value to format
* @param options The options for formatting
* @returns The formatted string
*
* @example Basic usage
* ```ts
* import { format } from "@std/fmt/bytes";
* import { assertEquals } from "@std/assert";
*
* assertEquals(format(1337), "1.34 kB");
* assertEquals(format(100), "100 B");
* ```
*
* @example Include bits representation
*
* ```ts
* import { format } from "@std/fmt/bytes";
* import { assertEquals } from "@std/assert";
*
* assertEquals(format(1337, { bits: true }), "1.34 kbit");
* ```
*
* @example Include sign
*
* ```ts
* import { format } from "@std/fmt/bytes";
* import { assertEquals } from "@std/assert";
*
* assertEquals(format(42, { signed: true }), "+42 B");
* assertEquals(format(-42, { signed: true }), "-42 B");
* ```
*
* @example Change locale
*
* ```ts
* import { format } from "@std/fmt/bytes";
* import { assertEquals } from "@std/assert";
*
* assertEquals(format(1337, { locale: "de" }), "1,34 kB");
* ```
*/
export function format(
num: number,
options: FormatOptions = {},
): string {
if (!Number.isFinite(num)) {
throw new TypeError(`Expected a finite number, got ${typeof num}: ${num}`);
}
const UNITS_FIRSTLETTER = (options.bits ? "b" : "B") + "kMGTPEZY";
if (options.signed && num === 0) {
return ` 0 ${UNITS_FIRSTLETTER[0]}`;
}
const prefix = num < 0 ? "-" : (options.signed ? "+" : "");
num = Math.abs(num);
const localeOptions = getLocaleOptions(options);
if (num < 1) {
const numberString = toLocaleString(num, options.locale, localeOptions);
return prefix + numberString + " " + UNITS_FIRSTLETTER[0];
}
const exponent = Math.min(
Math.floor(
options.binary ? Math.log(num) / Math.log(1024) : Math.log10(num) / 3,
),
UNITS_FIRSTLETTER.length - 1,
);
num /= Math.pow(options.binary ? 1024 : 1000, exponent);
if (!localeOptions) {
num = Number(num.toPrecision(3));
}
const numberString = toLocaleString(
num,
options.locale,
localeOptions,
);
let unit = UNITS_FIRSTLETTER[exponent];
if (exponent > 0) {
unit += options.binary ? "i" : "";
unit += options.bits ? "bit" : "B";
}
return prefix + numberString + " " + unit;
}
function getLocaleOptions(
{ maximumFractionDigits, minimumFractionDigits }: FormatOptions,
): LocaleOptions | undefined {
if (
maximumFractionDigits === undefined && minimumFractionDigits === undefined
) {
return;
}
const ret: LocaleOptions = {};
if (maximumFractionDigits !== undefined) {
ret.maximumFractionDigits = maximumFractionDigits;
}
if (minimumFractionDigits !== undefined) {
ret.minimumFractionDigits = minimumFractionDigits;
}
return ret;
}
/**
* Formats the given number using `Number#toLocaleString`.
* - If locale is a string, the value is expected to be a locale-key (for example: `de`).
* - If locale is true, the system default locale is used for translation.
* - If no value for locale is specified, the number is returned unmodified.
*/
function toLocaleString(
num: number,
locale: boolean | string | string[] | undefined,
options: LocaleOptions | undefined,
): string {
if (typeof locale === "string" || Array.isArray(locale)) {
return num.toLocaleString(locale, options);
} else if (locale === true || options !== undefined) {
return num.toLocaleString(undefined, options);
}
return num.toString();
}
|