Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Split by case should take space into account #98

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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),
)
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Splitter = "-" | "_" | "/" | ".";
type Splitter = "-" | "_" | "/" | "." | " ";
type FirstOfString<S extends string> = S extends `${infer F}${string}`
? F
: never;
Expand Down
2 changes: 2 additions & 0 deletions test/scule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
1 change: 1 addition & 0 deletions test/types.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe("SplitByCase", () => {
assertType<SplitByCase<"FOOBar">>(["FOO", "Bar"]);
assertType<SplitByCase<"ALink">>(["A", "Link"]);
assertType<SplitByCase<"FOO_BAR">>(["FOO", "BAR"]);
assertType<SplitByCase<"FOO BAR">>(["FOO", "BAR"]);
});

test("custom splitters", () => {
Expand Down