// Generated by Assertloom. Source: examples/strings.ts. Review before committing. import { capitalize, slugify, truncate, isPalindrome, countWords, chunk, parseDuration, retryAsync } from "./strings"; describe("capitalize", () => { it("capitalizes the first character and preserves the rest", () => { expect(capitalize("hello world")).toBe("Hello world"); expect(capitalize("typescript")).toBe("Typescript"); }); it("returns the same single uppercase letter", () => { expect(capitalize("A")).toBe("A"); expect(capitalize("z")).toBe("Z"); }); it("returns an empty string unchanged", () => { expect(capitalize("")).toBe(""); }); }); describe("slugify", () => { it("lowercases and replaces separators with hyphens", () => { expect(slugify("Hello, World!")).toBe("hello-world"); expect(slugify("Foo Bar")).toBe("foo-bar"); }); it("removes accents via unicode normalization", () => { expect(slugify("Crème Brûlée")).toBe("creme-brulee"); expect(slugify("mañana")).toBe("manana"); }); it("trims leading and trailing non alphanumerics", () => { expect(slugify("---Hello---")).toBe("hello"); expect(slugify(" ")).toBe(""); }); }); describe("truncate", () => { it("returns the original text when it fits within maxLength", () => { expect(truncate("hello", 5)).toBe("hello"); expect(truncate("hi", 10)).toBe("hi"); }); it("truncates and appends the default suffix", () => { expect(truncate("abcdefgh", 5)).toBe("ab..."); expect(truncate("longer text", 8)).toBe("longe..."); }); it("supports a custom suffix when truncating", () => { expect(truncate("abcdef", 5, "~")).toBe("abcd~"); expect(truncate("abcdef", 6, "!!")).toBe("abcdef"); }); it("throws when maxLength is shorter than the suffix length", () => { expect(() => truncate("abc", 2)).toThrow(RangeError); expect(() => truncate("abc", 2, "....")).toThrow("maxLength must be at least the suffix length"); }); }); describe("isPalindrome", () => { it("ignores case and punctuation for palindromes", () => { expect(isPalindrome("A man, a plan, a canal: Panama")).toBe(true); expect(isPalindrome("Never odd or even")).toBe(true); }); it("returns false for non-palindromic text", () => { expect(isPalindrome("hello")).toBe(false); expect(isPalindrome("abc")).toBe(false); }); it("treats empty and punctuation-only strings as palindromes", () => { expect(isPalindrome("")).toBe(true); expect(isPalindrome("!!!")).toBe(true); }); }); describe("countWords", () => { it("counts words separated by varied whitespace", () => { expect(countWords("hello world")).toBe(2); expect(countWords("one\ttwo\nthree")).toBe(3); }); it("ignores leading and trailing whitespace", () => { expect(countWords(" hello ")).toBe(1); expect(countWords(" a b ")).toBe(2); }); it("returns zero for empty or whitespace-only strings", () => { expect(countWords("")).toBe(0); expect(countWords(" \n\t ")).toBe(0); }); }); describe("chunk", () => { it("splits an array into evenly sized chunks", () => { expect(chunk([1, 2, 3, 4], 2)).toEqual([[1, 2], [3, 4]]); expect(chunk([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]]); }); it("returns a single chunk when size exceeds the array length", () => { expect(chunk([1, 2], 5)).toEqual([[1, 2]]); expect(chunk([], 3)).toEqual([]); }); it("throws for non-positive or non-integer sizes", () => { expect(() => chunk([1, 2, 3], 0)).toThrow("size must be a positive integer"); expect(() => chunk([1, 2, 3], 1.5 as any)).toThrow("size must be a positive integer"); }); }); describe("parseDuration", () => { it("parses supported duration units into milliseconds", () => { expect(parseDuration("10ms")).toBe(10); expect(parseDuration("2s")).toBe(2000); expect(parseDuration("3m")).toBe(180000); expect(parseDuration("4h")).toBe(14400000); }); it("ignores surrounding whitespace in the input", () => { expect(parseDuration(" 15s ")).toBe(15000); expect(parseDuration("\n7m\t")).toBe(420000); }); it("throws for invalid duration formats", () => { expect(() => parseDuration("5d")).toThrow("Invalid duration: 5d"); expect(() => parseDuration("abc")).toThrow("Invalid duration: abc"); }); }); describe("retryAsync", () => { it("returns the first successful result", async () => { const fn = jest.fn().mockResolvedValue("ok"); await expect(retryAsync(fn)).resolves.toBe("ok"); expect(fn).toHaveBeenCalledTimes(1); }); it("retries until a later attempt succeeds", async () => { const fn = jest.fn() .mockRejectedValueOnce(new Error("fail 1")) .mockRejectedValueOnce(new Error("fail 2")) .mockResolvedValue("done"); await expect(retryAsync(fn, 3)).resolves.toBe("done"); expect(fn).toHaveBeenCalledTimes(3); }); it("throws the last error after all attempts fail", async () => { const error1 = new Error("first"); const error2 = new Error("second"); const fn = jest.fn() .mockRejectedValueOnce(error1) .mockRejectedValueOnce(error2); await expect(retryAsync(fn, 2)).rejects.toThrow("second"); expect(fn).toHaveBeenCalledTimes(2); }); it("handles zero attempts by rejecting with undefined", async () => { const fn = jest.fn(); await expect(retryAsync(fn, 0)).rejects.toBeUndefined(); expect(fn).not.toHaveBeenCalled(); }); });