import { describe, it, expect } from "vitest"; import { formatBytes, formatBytesLong, stringByteFormat } from '../src/utils/FormatBytes.mjs'; let bytes_map = [ { "in": "120MB", "out": "120MB", "out_l": "120MB", }, { "in": -123123123, "out": "-120237.42kB", "out_l": "-117.42 MB", }, { "in": 999999, // KB-1, "out": "0.95MB", "out_l": "976.56 KB", }, { "in": 999999999, // MB-1 "out": "0.93GB", "out_l": "953.67 MB", }, { "in": 254779258, // MB-n "out": "0.24GB", "out_l": "242.98 MB", }, { "in": 999999999999999, // TB-1 "out": "0.89PB", "out_l": "909.49 TB", }, { "in": 588795544887632, // TB-n "out": "0.52PB", "out_l": "535.51 TB", }, { "in": 999999999999999999n, // PB-1 "out": "0.87EB", "out_l": "888.18 PB", }, { "in": 9223372036854775807n, // MAX INT "out": "8EB", "out_l": "8.00 EB", }, // { // "in": 999999999999999999999n, // EB-1 // "out": "", // } ]; describe("formatBytes", () => { it('convert bytes to human readable, round up to next set', () => { // expect(formatBytes(1021152)).toBe('0.97MB'); for (const bytes of bytes_map) { // @ts-ignore expect(formatBytes(bytes.in)).toBe(bytes.out); } }); }); describe("formatBytesLong", () => { it('convert bytes to human readable, keep on current set', () => { expect(formatBytesLong(1021152)).toBe('997.22 KB'); for (const bytes of bytes_map) { // @ts-ignore expect(formatBytesLong(bytes.in)).toBe(bytes.out_l); } }); }); describe("stringByteFormat", () => { it('convert a string with byte suffix to bytes', () => { expect(stringByteFormat('997.22 KB')).toBe(1021153); expect(stringByteFormat('997.22 KB', true)).toBe(1021153.28); }); }); // __END__