diff --git a/src/index.ts b/src/index.ts index f1983fd..2fec97a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -184,10 +184,14 @@ export function titleCase< T extends string | readonly string[], UserCaseOptions extends CaseOptions = CaseOptions, >(str?: T, opts?: UserCaseOptions) { - return (Array.isArray(str) ? str : splitByCase(str as string)) + return ( + Array.isArray(str) + ? str + : splitByCase(str as string, [...STR_SPLITTERS, " "]) + ) .filter(Boolean) - .map((p) => - titleCaseExceptions.test(p) + .map((p, i) => + i > 0 && titleCaseExceptions.test(p) ? p.toLowerCase() : upperFirst(opts?.normalize ? p.toLowerCase() : p), ) diff --git a/test/scule.test.ts b/test/scule.test.ts index bd5ec3b..910143d 100644 --- a/test/scule.test.ts +++ b/test/scule.test.ts @@ -140,6 +140,18 @@ describe("titleCase", () => { ["foo", "Foo"], ["foo-bar", "Foo Bar"], ["this-IS-aTitle", "This is a Title"], + ["in-world", "In World"], + ["in world", "In World"], + ["inWorld", "In World"], + ["and-then", "And Then"], + ["and then", "And Then"], + ["andThen", "And Then"], + [["in", "world"], "In World"], + [["and", "then"], "And Then"], + ["the-quick-brown-fox-and-the-dog", "The Quick Brown Fox and the Dog"], + ["the quick brown fox and the dog", "The Quick Brown Fox and the Dog"], + ["a-tale-of-two-cities", "A Tale of Two Cities"], + ["a tale of two cities", "A Tale of Two Cities"], ])("%s => %s", (input, expected) => { expect(titleCase(input)).toMatchObject(expected); });