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
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
? p.toLowerCase()
: upperFirst(opts?.normalize ? p.toLowerCase() : p),
)
Expand Down
12 changes: 12 additions & 0 deletions test/scule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down