diff --git a/lib/util.test.tsx b/lib/util.test.tsx index af0f6b3..12a3f9f 100644 --- a/lib/util.test.tsx +++ b/lib/util.test.tsx @@ -1,4 +1,5 @@ import { + capitalize, flatten, float, inclusiveRange, @@ -51,3 +52,10 @@ test("toFriendlyDecimal() works", () => { test("withoutNulls() works", () => { expect(withoutNulls([1, 2, 0, null, 3])).toEqual([1, 2, 0, 3]); }); + +test("capitalize() works", () => { + for (let boop of ["boop", "BOOP", "Boop", "bOoP"]) { + expect(capitalize(boop)).toBe("Boop"); + } + expect(capitalize("")).toBe(""); +}); diff --git a/lib/util.ts b/lib/util.ts index e2b0c3b..9a20323 100644 --- a/lib/util.ts +++ b/lib/util.ts @@ -132,3 +132,11 @@ export function withoutNulls(arr: (T | null)[]): T[] { export function lerp(a: number, b: number, amount: number) { return a + amount * (b - a); } + +/** + * Capitalize the given word, forcing the first letter to + * uppercase and the rest to lowercase. + */ +export function capitalize(value: string) { + return value.slice(0, 1).toUpperCase() + value.slice(1).toLowerCase(); +}