SPB Git

Repository Public

format.test.ts 4.1 KB 69 lines typescript

1import { describe, expect, it } from "vitest";2import { formatByteRate, formatBytes, formatDate, formatDateTime, formatDuration, formatMetric, formatMs, formatPercent, formatRatio, formatRelative, formatTime, formatUtc, fromLocalInput, toLocalInput } from "./format";34const nb = (s: string) => s.replace(/[  ]/g, " "); // normalise the non-breaking spaces Intl inserts56describe("formatters", () => {7  it("formats bytes in the unit customary to the locale (binary multiples)", () => {8    expect(nb(formatBytes("fr", 0))).toBe("0 o");9    expect(nb(formatBytes("fr", 1536))).toBe("1,5 ko");10    expect(nb(formatBytes("fr", 96 * 1024 ** 3))).toBe("96 Go");11    expect(nb(formatBytes("en", 96 * 1024 ** 3))).toBe("96 GB");12    expect(nb(formatBytes("en", 1.5 * 1024 ** 4))).toBe("1.5 TB");13  });14  it("formats rates, percentages and milliseconds", () => {15    expect(nb(formatByteRate("en", 2.5 * 1024 ** 2))).toBe("2.5 MB/s");16    expect(nb(formatByteRate("fr", 512))).toBe("512 o/s");17    expect(nb(formatPercent("fr", 43))).toBe("43 %");18    expect(nb(formatPercent("en", 43.26, 1))).toBe("43.3%");19    expect(nb(formatRatio("en", 0.896))).toBe("90%");20    expect(nb(formatMs("fr", 3.25))).toBe("3,3 ms");21    expect(nb(formatMs("en", 120))).toBe("120 ms");22  });23  it("formats durations with at most two units", () => {24    expect(nb(formatDuration("fr", 0))).toBe("0 s");25    expect(nb(formatDuration("fr", 59))).toBe("59 s");26    expect(nb(formatDuration("fr", 3 * 86400 + 4 * 3600 + 120))).toBe("3 j 4 h");27    expect(nb(formatDuration("en", 725))).toBe("12 min 5 sec");28    expect(nb(formatDuration("en", 3600))).toBe("1 hr");29    expect(nb(formatDuration("en", 90061))).toBe("1 day 1 hr");30    expect(nb(formatDuration("en", -5))).toBe("0 sec");31  });32  it("makes UTC explicit for audit timestamps, identically in both locales and whatever the local zone", () => {33    expect(formatUtc("fr", "2026-11-18T21:05:09Z")).toBe("2026-11-18 21:05:09 UTC");34    expect(formatUtc("en", "2026-11-18T16:05:09-05:00")).toBe("2026-11-18 21:05:09 UTC");35  });36  it("names the zone on local date-times and uses a 24 h clock", () => {37    expect(nb(formatDateTime("en", "2026-11-18T21:05:00Z", { timeZone: "America/Toronto" }))).toBe("Nov 18, 2026 16:05 EST");38    expect(nb(formatDateTime("fr", "2026-07-01T12:00:00Z", { timeZone: "America/Toronto" }))).toBe("1 juill. 2026 08:00 HAE");39    expect(nb(formatDateTime("en", "2026-11-18T21:05:09Z", { timeZone: "UTC", zone: false, seconds: true }))).toBe("Nov 18, 2026 21:05:09");40    expect(formatTime("fr", "2026-11-18T21:05:09Z", { timeZone: "UTC" })).toBe("21:05:09");41    expect(formatTime("en", "2026-11-18T00:05:09Z", { timeZone: "UTC", seconds: false })).toBe("00:05");42    expect(nb(formatDate("fr", "2026-11-18T21:05:09Z", "UTC"))).toBe("18 nov. 2026");43  });44  it("formats relative times", () => {45    const now = Date.parse("2026-11-18T12:00:00Z");46    expect(nb(formatRelative("fr", now - 3 * 60_000, now))).toBe("il y a 3 min");47    expect(nb(formatRelative("en", now - 2 * 3600_000, now))).toBe("2 hr. ago");48    expect(nb(formatRelative("en", now + 3 * 86400_000, now))).toBe("in 3 days");49  });50  it("formats metric values from their catalog unit", () => {51    expect(nb(formatMetric("fr", 37.5, "percent"))).toBe("38 %");52    expect(nb(formatMetric("en", 4.2, "percent"))).toBe("4.2%");53    expect(nb(formatMetric("en", 8 * 1024 ** 3, "bytes"))).toBe("8 GB");54    expect(nb(formatMetric("en", 1024, "bytes_per_s"))).toBe("1 kB/s");55    expect(nb(formatMetric("en", 2.345, "count"))).toBe("2.35");56    expect(nb(formatMetric("en", 90061, "seconds"))).toBe("1 day 1 hr");57    expect(nb(formatMetric("en", 41.5, "celsius"))).toBe("41.5°C");58    expect(nb(formatMetric("en", 12.5, "watts"))).toBe("12.5 W");59    expect(nb(formatMetric("en", 12, "furlongs"))).toBe("12 furlongs");60    expect(nb(formatMetric("fr", 3, undefined))).toBe("3");61  });62  it("round-trips datetime-local values", () => {63    const iso = "2026-11-18T21:05:00.000Z";64    expect(fromLocalInput(toLocalInput(iso))).toBe(iso);65    expect(fromLocalInput("")).toBeNull();66    expect(fromLocalInput("nonsense")).toBeNull();67  });68});69
Files main
Menu

Theme