diff --git a/README.md b/README.md index 0e4d7d7..bc62a7e 100644 --- a/README.md +++ b/README.md @@ -119,7 +119,7 @@ lowerFirst("Hello world!"); ### `splitByCase(str, splitters?)` -- Splits string by the splitters provided (default: `['-', '_', '/', '.']`) +- Splits string by the splitters provided (default: `['-', '_', '/', '.', ' ']`) - Splits when case changes from lower to upper or upper to lower - Ignores numbers for case changes - Case is preserved in returned value diff --git a/src/index.ts b/src/index.ts index f1983fd..778d810 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,7 +10,7 @@ import type { } from "./types"; const NUMBER_CHAR_RE = /\d/; -const STR_SPLITTERS = ["-", "_", "/", "."] as const; +const STR_SPLITTERS = ["-", "_", "/", ".", " "] as const; export function isUppercase(char = ""): boolean | undefined { if (NUMBER_CHAR_RE.test(char)) { @@ -186,8 +186,8 @@ export function titleCase< >(str?: T, opts?: UserCaseOptions) { return (Array.isArray(str) ? str : splitByCase(str as string)) .filter(Boolean) - .map((p) => - titleCaseExceptions.test(p) + .map((p, index) => + index > 0 && titleCaseExceptions.test(p) ? p.toLowerCase() : upperFirst(opts?.normalize ? p.toLowerCase() : p), ) diff --git a/src/types.ts b/src/types.ts index 9e7f1dd..061778d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,4 @@ -type Splitter = "-" | "_" | "/" | "."; +type Splitter = "-" | "_" | "/" | "." | " "; type FirstOfString = S extends `${infer F}${string}` ? F : never; diff --git a/test/scule.test.ts b/test/scule.test.ts index bd5ec3b..d5d1a62 100644 --- a/test/scule.test.ts +++ b/test/scule.test.ts @@ -140,6 +140,8 @@ describe("titleCase", () => { ["foo", "Foo"], ["foo-bar", "Foo Bar"], ["this-IS-aTitle", "This is a Title"], + ["is_this ATitle", "Is This a Title"], + ["hello, world!", "Hello, World!"], ])("%s => %s", (input, expected) => { expect(titleCase(input)).toMatchObject(expected); }); diff --git a/test/types.test-d.ts b/test/types.test-d.ts index bd722b1..f659b42 100644 --- a/test/types.test-d.ts +++ b/test/types.test-d.ts @@ -22,6 +22,7 @@ describe("SplitByCase", () => { assertType>(["FOO", "Bar"]); assertType>(["A", "Link"]); assertType>(["FOO", "BAR"]); + assertType>(["FOO", "BAR"]); }); test("custom splitters", () => {