From 84da684bbf5c31410043e92330800a6149748822 Mon Sep 17 00:00:00 2001 From: NoTwoBoy <1244476905@qq.com> Date: Thu, 21 Nov 2024 17:55:05 +0800 Subject: [PATCH 1/4] fix: Split by case should take space into account --- src/index.ts | 6 +++--- test/scule.test.ts | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index f1983fd..67618bf 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, i) => + i && 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..e42835d 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_thisATitle", "Is This a Title"], + ["hello, world!", "Hello, World!"], ])("%s => %s", (input, expected) => { expect(titleCase(input)).toMatchObject(expected); }); From 6ff2a26f4c14d59efb8b3f1fe829581a659da305 Mon Sep 17 00:00:00 2001 From: NoTwoBoy <1244476905@qq.com> Date: Thu, 21 Nov 2024 18:11:35 +0800 Subject: [PATCH 2/4] chore: update test --- test/scule.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/scule.test.ts b/test/scule.test.ts index e42835d..d5d1a62 100644 --- a/test/scule.test.ts +++ b/test/scule.test.ts @@ -140,7 +140,7 @@ describe("titleCase", () => { ["foo", "Foo"], ["foo-bar", "Foo Bar"], ["this-IS-aTitle", "This is a Title"], - ["is_thisATitle", "Is This a Title"], + ["is_this ATitle", "Is This a Title"], ["hello, world!", "Hello, World!"], ])("%s => %s", (input, expected) => { expect(titleCase(input)).toMatchObject(expected); From ac32adf732fd296e588382e1aaa9638ed442646d Mon Sep 17 00:00:00 2001 From: NoTwoBoy <1244476905@qq.com> Date: Fri, 22 Nov 2024 10:13:11 +0800 Subject: [PATCH 3/4] chore: update --- README.md | 2 +- src/index.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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 67618bf..778d810 100644 --- a/src/index.ts +++ b/src/index.ts @@ -186,8 +186,8 @@ export function titleCase< >(str?: T, opts?: UserCaseOptions) { return (Array.isArray(str) ? str : splitByCase(str as string)) .filter(Boolean) - .map((p, i) => - i && titleCaseExceptions.test(p) + .map((p, index) => + index > 0 && titleCaseExceptions.test(p) ? p.toLowerCase() : upperFirst(opts?.normalize ? p.toLowerCase() : p), ) From c0a6e9aaa47b5347289f119c40d035e527c3aaa0 Mon Sep 17 00:00:00 2001 From: NoTwoBoy <1244476905@qq.com> Date: Fri, 22 Nov 2024 10:31:10 +0800 Subject: [PATCH 4/4] chore: types --- src/types.ts | 2 +- test/types.test-d.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) 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/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", () => {