deno.land / std@0.224.0 / fmt / bytes_test.ts

bytes_test.ts
View Documentation
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// Copyright 2014-2021 Sindre Sorhus. All rights reserved. MIT license.// Copyright 2021 Yoshiya Hinosawa. All rights reserved. MIT license.
import { format } from "./bytes.ts";import { assertEquals, assertThrows } from "../assert/mod.ts";
const parts = new Intl.NumberFormat().formatToParts(1000.1);const decimal = parts.find(({ type }) => type === "decimal")!.value;const group = parts.find(({ type }) => type === "group")!.value;
Deno.test("format() throws on invalid input", () => { // deno-lint-ignore no-explicit-any assertThrows(() => format("" as any)); // deno-lint-ignore no-explicit-any assertThrows(() => format("1" as any)); assertThrows(() => format(NaN)); // deno-lint-ignore no-explicit-any assertThrows(() => format(true as any)); assertThrows(() => format(Infinity)); assertThrows(() => format(-Infinity)); // deno-lint-ignore no-explicit-any assertThrows(() => format(null as any));});
Deno.test("format() converts bytes to human readable strings", () => { assertEquals(format(0), "0 B"); assertEquals(format(0.4), "0.4 B"); assertEquals(format(0.7), "0.7 B"); assertEquals(format(10), "10 B"); assertEquals(format(10.1), "10.1 B"); assertEquals(format(999), "999 B"); assertEquals(format(1001), "1 kB"); assertEquals(format(1001), "1 kB"); assertEquals(format(1e16), "10 PB"); assertEquals(format(1e30), "1000000 YB");});
Deno.test("format() supports negative number", () => { assertEquals(format(-0.4), "-0.4 B"); assertEquals(format(-0.7), "-0.7 B"); assertEquals(format(-10.1), "-10.1 B"); assertEquals(format(-999), "-999 B"); assertEquals(format(-1001), "-1 kB");});
Deno.test("format() handles locale option", () => { assertEquals(format(-0.4, { locale: "de" }), "-0,4 B"); assertEquals(format(0.4, { locale: "de" }), "0,4 B"); assertEquals(format(1001, { locale: "de" }), "1 kB"); assertEquals(format(10.1, { locale: "de" }), "10,1 B"); assertEquals(format(1e30, { locale: "de" }), "1.000.000 YB");
assertEquals(format(-0.4, { locale: "en" }), "-0.4 B"); assertEquals(format(0.4, { locale: "en" }), "0.4 B"); assertEquals(format(1001, { locale: "en" }), "1 kB"); assertEquals(format(10.1, { locale: "en" }), "10.1 B"); assertEquals(format(1e30, { locale: "en" }), "1,000,000 YB");
assertEquals( format(-0.4, { locale: ["unknown", "de", "en"] }), "-0,4 B", ); assertEquals(format(0.4, { locale: ["unknown", "de", "en"] }), "0,4 B"); assertEquals(format(1001, { locale: ["unknown", "de", "en"] }), "1 kB"); assertEquals( format(10.1, { locale: ["unknown", "de", "en"] }), "10,1 B", ); assertEquals( format(1e30, { locale: ["unknown", "de", "en"] }), "1.000.000 YB", );
assertEquals(format(-0.4, { locale: true }), `-0${decimal}4 B`); assertEquals(format(0.4, { locale: true }), `0${decimal}4 B`); assertEquals(format(1001, { locale: true }), "1 kB"); assertEquals(format(10.1, { locale: true }), `10${decimal}1 B`); assertEquals( format(1e30, { locale: true }), `1${group}000${group}000 YB`, );
assertEquals(format(-0.4, { locale: false }), "-0.4 B"); assertEquals(format(0.4, { locale: false }), "0.4 B"); assertEquals(format(1001, { locale: false }), "1 kB"); assertEquals(format(10.1, { locale: false }), "10.1 B"); assertEquals(format(1e30, { locale: false }), "1000000 YB");
assertEquals(format(-0.4, { locale: undefined }), "-0.4 B"); assertEquals(format(0.4, { locale: undefined }), "0.4 B"); assertEquals(format(1001, { locale: undefined }), "1 kB"); assertEquals(format(10.1, { locale: undefined }), "10.1 B"); assertEquals(format(1e30, { locale: undefined }), "1000000 YB");});
Deno.test("format() handles signed option", () => { assertEquals(format(42, { signed: true }), "+42 B"); assertEquals(format(-13, { signed: true }), "-13 B"); assertEquals(format(0, { signed: true }), " 0 B");});
Deno.test("format() handles bits option", () => { assertEquals(format(0, { bits: true }), "0 b"); assertEquals(format(0.4, { bits: true }), "0.4 b"); assertEquals(format(0.7, { bits: true }), "0.7 b"); assertEquals(format(10, { bits: true }), "10 b"); assertEquals(format(10.1, { bits: true }), "10.1 b"); assertEquals(format(999, { bits: true }), "999 b"); assertEquals(format(1001, { bits: true }), "1 kbit"); assertEquals(format(1001, { bits: true }), "1 kbit"); assertEquals(format(1e16, { bits: true }), "10 Pbit"); assertEquals(format(1e30, { bits: true }), "1000000 Ybit");});
Deno.test("format() handles binary option", () => { assertEquals(format(0, { binary: true }), "0 B"); assertEquals(format(4, { binary: true }), "4 B"); assertEquals(format(10, { binary: true }), "10 B"); assertEquals(format(10.1, { binary: true }), "10.1 B"); assertEquals(format(999, { binary: true }), "999 B"); assertEquals(format(1025, { binary: true }), "1 kiB"); assertEquals(format(1001, { binary: true }), "1000 B"); assertEquals(format(1e16, { binary: true }), "8.88 PiB"); assertEquals(format(1e30, { binary: true }), "827000 YiB");});
Deno.test("format() handles bits and binary option", () => { assertEquals(format(0, { bits: true, binary: true }), "0 b"); assertEquals(format(4, { bits: true, binary: true }), "4 b"); assertEquals(format(10, { bits: true, binary: true }), "10 b"); assertEquals(format(999, { bits: true, binary: true }), "999 b"); assertEquals(format(1025, { bits: true, binary: true }), "1 kibit"); assertEquals(format(1e6, { bits: true, binary: true }), "977 kibit");});
Deno.test("format() handles fractional digits options", () => { assertEquals( format(1900, { maximumFractionDigits: 1 }), `1${decimal}9 kB`, ); assertEquals( format(1900, { minimumFractionDigits: 3 }), `1${decimal}900 kB`, ); assertEquals( format(1911, { maximumFractionDigits: 1 }), `1${decimal}9 kB`, ); assertEquals( format(1111, { maximumFractionDigits: 2 }), `1${decimal}11 kB`, ); assertEquals( format(1019, { maximumFractionDigits: 3 }), `1${decimal}019 kB`, ); assertEquals( format(1001, { maximumFractionDigits: 3 }), `1${decimal}001 kB`, ); assertEquals( format(1000, { minimumFractionDigits: 1, maximumFractionDigits: 3 }), `1${decimal}0 kB`, ); assertEquals( format(3942, { minimumFractionDigits: 1, maximumFractionDigits: 2 }), `3${decimal}94 kB`, ); assertEquals( format(4001, { maximumFractionDigits: 3, binary: true }), `3${decimal}907 kiB`, ); assertEquals( format(18717, { maximumFractionDigits: 2, binary: true }), `18${decimal}28 kiB`, ); assertEquals( format(18717, { maximumFractionDigits: 4, binary: true }), `18${decimal}2783 kiB`, ); assertEquals( format(32768, { minimumFractionDigits: 2, maximumFractionDigits: 3, binary: true, }), `32${decimal}00 kiB`, ); assertEquals( format(65536, { minimumFractionDigits: 1, maximumFractionDigits: 3, binary: true, }), `64${decimal}0 kiB`, );});
std

Version Info

Tagged at
6 months ago