File size: 4,200 Bytes
df9fdc8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, it, expect } from "vitest";
import { filesize, SPECS } from "../utils/filesize";

describe("filesize", () => {
	describe("basic functionality", () => {
		it("should format bytes correctly with default spec (jedec)", () => {
			expect(filesize(0)).toBe("0.0 b");
			expect(filesize(512)).toBe("512.0 b");
			expect(filesize(1024)).toBe("1.0 Kb");
			expect(filesize(1536)).toBe("1.5 Kb"); // 1024 + 512
			expect(filesize(1048576)).toBe("1.0 Mb"); // 1024^2
			expect(filesize(1073741824)).toBe("1.0 Gb"); // 1024^3
		});

		it("should handle different fixed decimal places", () => {
			expect(filesize(1536, 0)).toBe("2 Kb");
			expect(filesize(1536, 1)).toBe("1.5 Kb");
			expect(filesize(1536, 2)).toBe("1.50 Kb");
			expect(filesize(1536, 3)).toBe("1.500 Kb");
		});

		it("should handle negative numbers by taking absolute value", () => {
			expect(filesize(-1024)).toBe("1.0 Kb");
			expect(filesize(-1536)).toBe("1.5 Kb");
			expect(filesize(-0)).toBe("0.0 b");
		});
	});

	describe("SI specification", () => {
		it("should use base 1000 for SI spec", () => {
			expect(filesize(1000, 1, "si")).toBe("1.0 kb");
			expect(filesize(1500, 1, "si")).toBe("1.5 kb");
			expect(filesize(1000000, 1, "si")).toBe("1.0 Mb");
			expect(filesize(1000000000, 1, "si")).toBe("1.0 Gb");
		});

		it("should handle large numbers with SI spec", () => {
			expect(filesize(1e12, 1, "si")).toBe("1.0 Tb");
			expect(filesize(1e15, 1, "si")).toBe("1.0 Pb");
			expect(filesize(1e18, 1, "si")).toBe("1.0 Eb");
		});
	});

	describe("IEC specification", () => {
		it("should use base 1024 with binary units for IEC spec", () => {
			expect(filesize(1024, 1, "iec")).toBe("1.0 Kib");
			expect(filesize(1536, 1, "iec")).toBe("1.5 Kib");
			expect(filesize(1048576, 1, "iec")).toBe("1.0 Mib");
			expect(filesize(1073741824, 1, "iec")).toBe("1.0 Gib");
		});

		it("should handle large numbers with IEC spec", () => {
			expect(filesize(Math.pow(1024, 4), 1, "iec")).toBe("1.0 Tib");
			expect(filesize(Math.pow(1024, 5), 1, "iec")).toBe("1.0 Pib");
		});
	});

	describe("JEDEC specification", () => {
		it("should use base 1024 with decimal-style units for JEDEC spec", () => {
			expect(filesize(1024, 1, "jedec")).toBe("1.0 Kb");
			expect(filesize(1536, 1, "jedec")).toBe("1.5 Kb");
			expect(filesize(1048576, 1, "jedec")).toBe("1.0 Mb");
			expect(filesize(1073741824, 1, "jedec")).toBe("1.0 Gb");
		});
	});

	describe("edge cases", () => {
		it("should handle very large numbers", () => {
			const veryLarge = Math.pow(1024, 8); // Yottabytes
			expect(filesize(veryLarge, 1, "jedec")).toBe("1.0 Yb");
		});

		it("should handle decimal input", () => {
			expect(filesize(1024.5, 1)).toBe("1.0 Kb");
			expect(filesize(1500.7, 2)).toBe("1.47 Kb");
		});

		it("should handle invalid spec by falling back to jedec", () => {
			expect(filesize(1024, 1, "invalid")).toBe("1.0 Kb");
			expect(filesize(1024, 1, "")).toBe("1.0 Kb");
			expect(filesize(1024, 1, undefined)).toBe("1.0 Kb");
		});
	});

	describe("precision and rounding", () => {
		it("should round correctly with different precision levels", () => {
			const testBytes = 1536; // 1.5 KB
			expect(filesize(testBytes, 0)).toBe("2 Kb");
			expect(filesize(testBytes, 1)).toBe("1.5 Kb");
			expect(filesize(testBytes, 2)).toBe("1.50 Kb");
		});

		it("should handle rounding edge cases", () => {
			const almostTwoKb = 2047; // almost 2KB
			expect(filesize(almostTwoKb, 0)).toBe("2 Kb");
			expect(filesize(almostTwoKb, 3)).toBe("1.999 Kb");
		});
	});

	describe("SPECS constant", () => {
		it("should have correct radix values", () => {
			expect(SPECS.si.radix).toBe(1000);
			expect(SPECS.iec.radix).toBe(1024);
			expect(SPECS.jedec.radix).toBe(1024);
		});

		it("should have correct unit arrays", () => {
			expect(SPECS.si.unit).toEqual([
				"b",
				"kb",
				"Mb",
				"Gb",
				"Tb",
				"Pb",
				"Eb",
				"Zb",
				"Yb"
			]);
			expect(SPECS.iec.unit).toEqual([
				"b",
				"Kib",
				"Mib",
				"Gib",
				"Tib",
				"Pib",
				"Eib",
				"Zib",
				"Yib"
			]);
			expect(SPECS.jedec.unit).toEqual([
				"b",
				"Kb",
				"Mb",
				"Gb",
				"Tb",
				"Pb",
				"Eb",
				"Zb",
				"Yb"
			]);
		});
	});
});