Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ snakeCase("foo-barBaz");
// foo_bar_baz
```

### `flatCase`

Splits string and joins by flatcase convention:

```ts
flatCase("foo-barBaz");
// foobarbaz
```

### `trainCase`

Split string and joins by Train-Case (a.k.a. HTTP-Header-Case) convention:

```ts
trainCase("FooBARb");
// Foo-Ba-Rb
```

### `upperFirst(str)`

Converts first character to upper case:
Expand Down
30 changes: 30 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type {
SnakeCase,
SplitByCase,
CaseOptions,
TrainCase,
FlatCase,
} from "./types";

const NUMBER_CHAR_RE = /\d/;
Expand Down Expand Up @@ -144,4 +146,32 @@ export function snakeCase<T extends string | readonly string[]>(str?: T) {
return kebabCase(str || "", "_") as SnakeCase<T>;
}

export function flatCase(): "";
export function flatCase<T extends string | readonly string[]>(
str: T,
): FlatCase<T>;
export function flatCase<T extends string | readonly string[]>(str?: T) {
return kebabCase(str || "", "") as FlatCase<T>;
}

export function trainCase(): "";
export function trainCase<T extends string | readonly string[]>(
str: T,
): TrainCase<T>;
export function trainCase<T extends string | readonly string[]>(str?: T) {
return str
? ((
(Array.isArray(str) ? str : splitByCase(str as string)) as Array<string>
)
// eslint-disable-next-line unicorn/no-array-reduce -- map and filter would require iterating twice
.reduce((acc, p) => {
acc +=
acc.length > 0
? p && `-${upperFirst(p.toLowerCase())}`
: p && upperFirst(p.toLowerCase());
return acc;
}, "") as TrainCase<T>)
: "";
}

export * from "./types";
17 changes: 17 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,20 @@ export type SnakeCase<T extends string | readonly string[]> = JoinByCase<
T,
"_"
>;

export type TrainCase<T> = string extends T
? string
: string[] extends T
? string
: T extends string
? SplitByCase<T> extends readonly string[]
? CapitalizedWords<SplitByCase<T>, "-">
: never
: T extends readonly string[]
? CapitalizedWords<T, "-">
: never;

export type FlatCase<
T extends string | readonly string[],
Joiner extends string = "",
> = JoinByCase<T, Joiner>;
31 changes: 31 additions & 0 deletions test/scule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
upperFirst,
lowerFirst,
snakeCase,
trainCase,
flatCase,
} from "../src";

describe("splitByCase", () => {
Expand Down Expand Up @@ -103,3 +105,32 @@ describe("lowerFirst", () => {
expect(lowerFirst(input)).toMatchObject(expected);
});
});

describe("trainCase", () => {
test.each([
["", ""],
["f", "F"],
["foo", "Foo"],
["foo-bAr", "Foo-B-Ar"],
["FooBARb", "Foo-Ba-Rb"],
["foo_bar-baz/qux", "Foo-Bar-Baz-Qux"],
["FOO_BAR", "Foo-Bar"],
["foo--bar-Baz", "Foo-Bar-Baz"],
])("%s => %s", (input, expected) => {
expect(trainCase(input)).toMatchObject(expected);
});
});

describe("flatCase", () => {
test.each([
["", ""],
["foo", "foo"],
["foo-bAr", "foobar"],
["FooBARb", "foobarb"],
["foo_bar-baz/qux", "foobarbazqux"],
["FOO_BAR", "foobar"],
["foo--bar-Baz", "foobarbaz"],
])("%s => %s", (input, expected) => {
expect(flatCase(input)).toMatchObject(expected);
});
});