Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/arrays.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, expect, it } from "vitest";
import { chunk, dedupe, groupBy } from "./arrays";

describe("chunk", () => {
it("splits arrays into fixed-size groups and keeps the remainder", () => {
expect(chunk([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]]);
});

it("returns an empty list for non-positive sizes", () => {
expect(chunk([1, 2, 3], 0)).toEqual([]);
expect(chunk([1, 2, 3], -1)).toEqual([]);
});
});

describe("dedupe", () => {
it("keeps first-seen values and removes later duplicates", () => {
expect(dedupe(["a", "b", "a", "c", "b"])).toEqual(["a", "b", "c"]);
});
});

describe("groupBy", () => {
it("groups values by the key returned from the callback", () => {
expect(groupBy(["ant", "bat", "ape"], (value) => value[0])).toEqual({
a: ["ant", "ape"],
b: ["bat"]
});
});
});
39 changes: 39 additions & 0 deletions src/dates.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, expect, it } from "vitest";
import { addDays, daysBetween, isWeekend, startOfDayUTC } from "./dates";

describe("daysBetween", () => {
it("returns the rounded absolute day difference", () => {
const start = new Date("2026-05-01T12:00:00Z");
const end = new Date("2026-05-04T00:00:00Z");

expect(daysBetween(start, end)).toBe(3);
expect(daysBetween(end, start)).toBe(3);
});
});

describe("isWeekend", () => {
it("detects Saturday and Sunday", () => {
expect(isWeekend(new Date("2026-05-23T12:00:00Z"))).toBe(true);
expect(isWeekend(new Date("2026-05-24T12:00:00Z"))).toBe(true);
});

it("returns false for weekdays", () => {
expect(isWeekend(new Date("2026-05-25T12:00:00Z"))).toBe(false);
Comment on lines +16 to +21
});
});

describe("addDays", () => {
it("returns a new date shifted by the requested number of days", () => {
const original = new Date("2026-05-27T10:30:00Z");
const shifted = addDays(original, 5);

expect(shifted.toISOString()).toBe("2026-06-01T10:30:00.000Z");
expect(original.toISOString()).toBe("2026-05-27T10:30:00.000Z");
});
});

describe("startOfDayUTC", () => {
it("normalizes a date to midnight UTC", () => {
expect(startOfDayUTC(new Date("2026-05-27T10:30:00Z")).toISOString()).toBe("2026-05-27T00:00:00.000Z");
});
});
40 changes: 40 additions & 0 deletions src/money.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { describe, expect, it } from "vitest";
import { formatCents, netPayoutCents, platformFeeCents, splitEvenly } from "./money";

describe("formatCents", () => {
it("formats USD and EUR with their symbols", () => {
expect(formatCents(1234)).toBe("$12.34");
expect(formatCents(1234, "EUR")).toBe("€12.34");
});

it("prefixes unknown currency codes", () => {
expect(formatCents(1234, "USDC")).toBe("USDC 12.34");
});
});

describe("platformFeeCents", () => {
it("rounds basis-point fees to the nearest cent", () => {
expect(platformFeeCents(12_345, 250)).toBe(309);
});

it("returns zero for non-positive gross amounts or fee rates", () => {
expect(platformFeeCents(0, 250)).toBe(0);
expect(platformFeeCents(12_345, 0)).toBe(0);
});
});

describe("netPayoutCents", () => {
it("subtracts the platform fee from the gross amount", () => {
expect(netPayoutCents(10_000, 250)).toBe(9_750);
});
});

describe("splitEvenly", () => {
it("distributes remainder cents to the earliest recipients", () => {
expect(splitEvenly(10, 3)).toEqual([4, 3, 3]);
});

it("returns an empty list for non-positive recipient counts", () => {
expect(splitEvenly(10, 0)).toEqual([]);
});
});
33 changes: 32 additions & 1 deletion src/strings.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
import { describe, it, expect } from "vitest";
import { slugify } from "./strings";
import { escapeHtml, slugify, titleCase, truncate } from "./strings";

describe("slugify", () => {
it("lowercases and dashifies basic input", () => {
expect(slugify("Hello World")).toBe("hello-world");
});

it("removes punctuation, trims edge dashes, and collapses repeated dashes", () => {
expect(slugify(" Hello, Tiny--Utils!!! ")).toBe("hello-tiny-utils");
});
});

describe("truncate", () => {
it("returns the original string when it fits within the maximum", () => {
expect(truncate("short", 10)).toBe("short");
});

it("shortens long strings and appends the suffix", () => {
expect(truncate("abcdef", 4)).toBe("abc…");
});

it("returns an empty string for non-positive maximum lengths", () => {
expect(truncate("abcdef", 0)).toBe("");
});
});

describe("titleCase", () => {
it("capitalizes each word and lowercases the rest", () => {
expect(titleCase("hELLO tiny UTILS")).toBe("Hello Tiny Utils");
});
});

describe("escapeHtml", () => {
it("escapes HTML-sensitive characters", () => {
expect(escapeHtml(`<a href="x">Tom & 'Jerry'</a>`)).toBe("&lt;a href=&quot;x&quot;&gt;Tom &amp; &#39;Jerry&#39;&lt;/a&gt;");
});
});