first commit
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import { test, expect } from "bun:test";
|
||||
import {
|
||||
formatNumber,
|
||||
formatCurrency,
|
||||
formatDate,
|
||||
formatRelativeTime,
|
||||
plural,
|
||||
} from "../src/index.ts";
|
||||
|
||||
test("formatNumber is locale-aware", () => {
|
||||
expect(formatNumber(1234.5, "en-US")).toBe("1,234.5");
|
||||
expect(formatNumber(1234.5, "de-DE")).toBe("1.234,5");
|
||||
});
|
||||
|
||||
test("formatCurrency", () => {
|
||||
expect(formatCurrency(9.99, "USD", "en-US")).toBe("$9.99");
|
||||
});
|
||||
|
||||
test("formatDate", () => {
|
||||
const out = formatDate("2026-01-15T00:00:00Z", "en-US", { dateStyle: "medium", timeZone: "UTC" });
|
||||
expect(out).toContain("2026");
|
||||
expect(out).toContain("Jan");
|
||||
});
|
||||
|
||||
test("formatRelativeTime", () => {
|
||||
expect(formatRelativeTime(-1, "day", "en-US")).toBe("yesterday");
|
||||
expect(formatRelativeTime(3, "hour", "en-US")).toBe("in 3 hours");
|
||||
});
|
||||
|
||||
test("plural picks the CLDR form and fills #", () => {
|
||||
const forms = { one: "# item", other: "# items" };
|
||||
expect(plural(1, forms, "en")).toBe("1 item");
|
||||
expect(plural(5, forms, "en")).toBe("5 items");
|
||||
expect(plural(0, forms, "en")).toBe("0 items");
|
||||
});
|
||||
@@ -0,0 +1,40 @@
|
||||
import { test, expect } from "bun:test";
|
||||
import { resolveI18n, makeT, resolveLang, translateHtml } from "../src/index.ts";
|
||||
|
||||
const i18n = resolveI18n(
|
||||
{
|
||||
en: { hi: "Hi {name}", nav: { home: "Home" }, ph: "Email" },
|
||||
es: { hi: "Hola {name}", nav: { home: "Inicio" } },
|
||||
},
|
||||
{ default: "en" },
|
||||
);
|
||||
|
||||
test("makeT interpolates params and nested keys", () => {
|
||||
const t = makeT(i18n, "es");
|
||||
expect(t("hi", { name: "Ana" })).toBe("Hola Ana");
|
||||
expect(t("nav.home")).toBe("Inicio");
|
||||
});
|
||||
|
||||
test("makeT falls back to default language, then the key", () => {
|
||||
const t = makeT(i18n, "es");
|
||||
expect(t("ph")).toBe("Email"); // missing in es → en
|
||||
expect(t("nope")).toBe("nope"); // missing everywhere → key
|
||||
});
|
||||
|
||||
test("resolveLang: cookie → Accept-Language → default", () => {
|
||||
expect(resolveLang(i18n, "es", null)).toBe("es");
|
||||
expect(resolveLang(i18n, undefined, "fr,es;q=0.8")).toBe("es");
|
||||
expect(resolveLang(i18n, "xx", "de")).toBe("en"); // invalid cookie + unsupported header
|
||||
});
|
||||
|
||||
test("translateHtml resolves data-t text and t:attr attributes", () => {
|
||||
const t = makeT(i18n, "en");
|
||||
const out = translateHtml('<input t:placeholder="ph"><span data-t="nav.home"></span>', t);
|
||||
expect(out).toContain('placeholder="Email"');
|
||||
expect(out).toContain('<span data-t="nav.home">Home</span>');
|
||||
});
|
||||
|
||||
test("translateHtml is a no-op without markers", () => {
|
||||
const t = makeT(i18n, "en");
|
||||
expect(translateHtml("<p>plain</p>", t)).toBe("<p>plain</p>");
|
||||
});
|
||||
Reference in New Issue
Block a user