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

feat(splitByCase): add whitespace as a string splitter character #102

Open
wants to merge 2 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
5 changes: 2 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_RE = /[\s./_-]/

export function isUppercase(char = ""): boolean | undefined {
if (NUMBER_CHAR_RE.test(char)) {
Expand All @@ -28,7 +28,6 @@ export function splitByCase<
T extends string,
Separator extends readonly string[],
>(str: T, separators?: Separator) {
const splitters = separators ?? STR_SPLITTERS;
const parts: string[] = [];

if (!str || typeof str !== "string") {
Expand All @@ -42,7 +41,7 @@ export function splitByCase<

for (const char of str) {
// Splitter
const isSplitter = (splitters as unknown as string).includes(char);
const isSplitter = Array.isArray(separators) ? separators.includes(char) : STR_SPLITTERS_RE.test(char);
if (isSplitter === true) {
parts.push(buff);
buff = "";
Expand Down
32 changes: 30 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
type Splitter = "-" | "_" | "/" | ".";
type Whitespace =
| "\u{9}" // '\t'
| "\u{A}" // '\n'
| "\u{B}" // '\v'
| "\u{C}" // '\f'
| "\u{D}" // '\r'
| "\u{20}" // ' '
| "\u{85}"
| "\u{A0}"
| "\u{1680}"
| "\u{2000}"
| "\u{2001}"
| "\u{2002}"
| "\u{2003}"
| "\u{2004}"
| "\u{2005}"
| "\u{2006}"
| "\u{2007}"
| "\u{2008}"
| "\u{2009}"
| "\u{200A}"
| "\u{2028}"
| "\u{2029}"
| "\u{202F}"
| "\u{205F}"
| "\u{3000}"
| "\u{FEFF}";

type Splitter = "-" | "_" | "/" | "." | Whitespace;
type FirstOfString<S extends string> = S extends `${infer F}${string}`
? F
: never;
Expand Down Expand Up @@ -164,4 +192,4 @@ export type TrainCase<
export type FlatCase<
T extends string | readonly string[],
Joiner extends string = "",
> = JoinByCase<T, Joiner>;
> = JoinByCase<T, Joiner>;
7 changes: 6 additions & 1 deletion test/scule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ describe("splitByCase", () => {
["fooBar", ["foo", "Bar"]],
["FooBarBaz", ["Foo", "Bar", "Baz"]],
["FooBARb", ["Foo", "BA", "Rb"]],
["foo_bar-baz/qux", ["foo", "bar", "baz", "qux"]],
["foo_bar-baz/qux quux", ["foo", "bar", "baz", "qux", "quux"]],
["foo--bar-Baz", ["foo", "", "bar", "Baz"]],
["FOO_BAR", ["FOO", "BAR"]],
["foo123-bar", ["foo123", "bar"]],
["FOOBar", ["FOO", "Bar"]],
["FOO BAR", ["FOO", "BAR"]],
["foO BAR", ["fo", "O", "BAR"]],
["Foo BAR", ["Foo", "BAR"]],
["Foo bAR", ["Foo", "b", "AR"]],
["FOO Bar", ["FOO", "Bar"]],
["ALink", ["A", "Link"]],
// with custom splitters
[
Expand Down
5 changes: 5 additions & 0 deletions test/types.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ describe("SplitByCase", () => {
assertType<SplitByCase<"FOOBar">>(["FOO", "Bar"]);
assertType<SplitByCase<"ALink">>(["A", "Link"]);
assertType<SplitByCase<"FOO_BAR">>(["FOO", "BAR"]);
assertType<SplitByCase<"FOO BAR">>(["FOO", "BAR"]);
assertType<SplitByCase<"foO BAR">>(["fo", "O", "BAR"]);
assertType<SplitByCase<"Foo BAR">>(["Foo", "BAR"]);
assertType<SplitByCase<"Foo bAR">>(["Foo", "b", "AR"]);
assertType<SplitByCase<"FOO Bar">>(["FOO", "Bar"]);
});

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