feat!: release v2 (#6545)

Co-authored-by: Artur <145776561+arturict@users.noreply.github.com>
Co-authored-by: arturict <arturict@users.noreply.github.com>
Co-authored-by: Thomas <thomas@ajnart.fr>
Co-authored-by: Afonso Jorge Ramos <afonsojorgeramos@gmail.com>
Co-authored-by: Danura30082 <danura30082+github@gmail.com>
Co-authored-by: Kushie <122986913+kushiemoon-dev@users.noreply.github.com>
Co-authored-by: ajnart <thomas@ajnart.fr>
Co-authored-by: ajnart <thomas@ajnart.dev>
Co-authored-by: chenjun <chenjun@foxmail.com>
Co-authored-by: Wikibear <wikibear@users.noreply.github.com>
Co-authored-by: Bergasha <bergasha@gmail.com>
Co-authored-by: leafyelin <43306196+leafyelin@users.noreply.github.com>
Co-authored-by: Lucas Araujo <lucas.lra@gmail.com>
Co-authored-by: GreenMonito <86085570+GreenMonito@users.noreply.github.com>
Co-authored-by: Nerdy-Technician <roffo@nerdy-technician.social>
Co-authored-by: Jun <34545896+jcstrong@users.noreply.github.com>
Co-authored-by: Edmond Abraham <54119952+gitedmond@users.noreply.github.com>
Co-authored-by: thrawnx <tight.bulb9384@fastmail.com>
Co-authored-by: Chris <58918656+nordada@users.noreply.github.com>
Co-authored-by: yi111 <153097222+Yi-111-a@users.noreply.github.com>
Co-authored-by: Yanis <naamaneyanis@gmail.com>
Co-authored-by: Wikibear <me@sej.de>
Co-authored-by: alessiottavo <93441886+alessiottavo@users.noreply.github.com>
This commit is contained in:
3286 files changed
+284386 -139176

No files matched your search

+58 -34
View File
@@ -1,35 +1,35 @@
import { describe, expect, it } from "vitest";
import { formatByteRate, formatBytes, formatBytesPair } from "../number";
import { formatBitRate, formatByteRate, formatBytes, formatBytesPair } from "../number";
describe("formatBytes", () => {
it("returns bytes for small values", () => {
expect(formatBytes(0)).toBe("0.0 B");
expect(formatBytes(1)).toBe("1.0 B");
expect(formatBytes(1023)).toBe("1023.0 B");
expect(formatBytes(999)).toBe("999.0 B");
});
it("scales binary units correctly", () => {
expect(formatBytes(1024)).toBe("1.0 KiB");
expect(formatBytes(1024 ** 2)).toBe("1.0 MiB");
expect(formatBytes(1024 ** 3)).toBe("1.0 GiB");
expect(formatBytes(1024 ** 4)).toBe("1.0 TiB");
it("scales decimal units by default", () => {
expect(formatBytes(1000)).toBe("1.0 KB");
expect(formatBytes(1000 ** 2)).toBe("1.0 MB");
expect(formatBytes(1000 ** 3)).toBe("1.0 GB");
expect(formatBytes(1000 ** 4)).toBe("1.0 TB");
});
it("scales decimal units when requested", () => {
expect(formatBytes(1000, { unit: "decimal" })).toBe("1.0 KB");
expect(formatBytes(1000 ** 2, { unit: "decimal" })).toBe("1.0 MB");
expect(formatBytes(1000 ** 3, { unit: "decimal" })).toBe("1.0 GB");
expect(formatBytes(1000 ** 4, { unit: "decimal" })).toBe("1.0 TB");
it("scales binary units when requested", () => {
expect(formatBytes(1024, { unit: "binary" })).toBe("1.0 KiB");
expect(formatBytes(1024 ** 2, { unit: "binary" })).toBe("1.0 MiB");
expect(formatBytes(1024 ** 3, { unit: "binary" })).toBe("1.0 GiB");
expect(formatBytes(1024 ** 4, { unit: "binary" })).toBe("1.0 TiB");
});
it("formats a typical storage value with one decimal", () => {
expect(formatBytes(985828802560)).toBe("918.1 GiB");
expect(formatBytes(985828802560, { unit: "decimal" })).toBe("985.8 GB");
expect(formatBytes(985828802560)).toBe("985.8 GB");
expect(formatBytes(985828802560, { unit: "binary" })).toBe("918.1 GiB");
});
it("caps at the largest unit to avoid overflow", () => {
expect(formatBytes(Number.MAX_SAFE_INTEGER)).toMatch(/PiB$/);
expect(formatBytes(Number.MAX_SAFE_INTEGER)).toMatch(/PB$/);
});
it("returns the zero value for negative or non-finite input", () => {
@@ -40,47 +40,53 @@ describe("formatBytes", () => {
});
describe("formatBytesPair", () => {
it("picks the unit of the larger total and applies it to both values", () => {
const { used, available } = formatBytesPair(985828802560, 2858736793190);
expect(used).toBe("0.9 TiB");
expect(available).toBe("2.6 TiB");
it("picks the unit of the larger value and applies it to both values", () => {
const { used, total } = formatBytesPair(985828802560, 2858736793190);
expect(used).toBe("1.0 TB");
expect(total).toBe("2.9 TB");
});
it("uses a smaller unit when both values fit in it", () => {
const { used, available } = formatBytesPair(1024 * 1024 * 100, 1024 * 1024 * 200);
expect(used).toBe("100.0 MiB");
expect(available).toBe("200.0 MiB");
const { used, total } = formatBytesPair(1024 * 1024 * 100, 1024 * 1024 * 200);
expect(used).toBe("104.9 MB");
expect(total).toBe("209.7 MB");
});
it("does not promote the pair when only their sum crosses a unit boundary", () => {
const { used, total } = formatBytesPair(700 * 1024, 700 * 1024);
expect(used).toBe("716.8 KB");
expect(total).toBe("716.8 KB");
});
it("falls back to zero when both inputs are invalid", () => {
const { used, available } = formatBytesPair(Number.NaN, -1);
const { used, total } = formatBytesPair(Number.NaN, -1);
expect(used).toBe("0.0 B");
expect(available).toBe("0.0 B");
expect(total).toBe("0.0 B");
});
it("uses the same unit across both values when one is invalid", () => {
const { used, available } = formatBytesPair(Number.NaN, 1024 ** 4);
expect(used).toBe("0.0 TiB");
expect(available).toBe("1.0 TiB");
const { used, total } = formatBytesPair(Number.NaN, 1024 ** 4);
expect(used).toBe("0.0 TB");
expect(total).toBe("1.1 TB");
});
it("honours the decimal unit option", () => {
const { used, available } = formatBytesPair(500_000_000_000, 1_500_000_000_000, { unit: "decimal" });
const { used, total } = formatBytesPair(500_000_000_000, 1_500_000_000_000, { unit: "decimal" });
expect(used).toBe("0.5 TB");
expect(available).toBe("1.5 TB");
expect(total).toBe("1.5 TB");
});
});
describe("formatByteRate", () => {
it("appends a /s suffix to the formatted value", () => {
expect(formatByteRate(0)).toBe("0.0 B/s");
expect(formatByteRate(1024)).toBe("1.0 KiB/s");
expect(formatByteRate(1024 ** 3)).toBe("1.0 GiB/s");
expect(formatByteRate(1000)).toBe("1.0 KB/s");
expect(formatByteRate(1000 ** 3)).toBe("1.0 GB/s");
});
it("honours the decimal unit option", () => {
expect(formatByteRate(1000, { unit: "decimal" })).toBe("1.0 KB/s");
expect(formatByteRate(1000 ** 2, { unit: "decimal" })).toBe("1.0 MB/s");
it("honours the binary unit option", () => {
expect(formatByteRate(1024, { unit: "binary" })).toBe("1.0 KiB/s");
expect(formatByteRate(1024 ** 2, { unit: "binary" })).toBe("1.0 MiB/s");
});
it("returns zero for invalid input", () => {
@@ -88,3 +94,21 @@ describe("formatByteRate", () => {
expect(formatByteRate(Number.NaN)).toBe("0.0 B/s");
});
});
describe("formatBitRate", () => {
it("uses decimal SI units with one optional fractional digit", () => {
expect(formatBitRate(999)).toBe("999 b/s");
expect(formatBitRate(1_000)).toBe("1 Kb/s");
expect(formatBitRate(1_000_000)).toBe("1 Mb/s");
expect(formatBitRate(1_500_000)).toBe("1.5 Mb/s");
expect(formatBitRate(1_000_000_000)).toBe("1 Gb/s");
expect(formatBitRate(1_234_567)).toBe("1.2 Mb/s");
});
it("returns zero for non-positive or non-finite rates", () => {
expect(formatBitRate(0)).toBe("0 b/s");
expect(formatBitRate(-1)).toBe("0 b/s");
expect(formatBitRate(Number.NaN)).toBe("0 b/s");
expect(formatBitRate(Number.POSITIVE_INFINITY)).toBe("0 b/s");
});
});