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
18 changes: 17 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,23 @@ export function camelCase<
T extends string | readonly string[],
UserCaseOptions extends CaseOptions = CaseOptions,
>(str?: T, opts?: UserCaseOptions) {
return lowerFirst(pascalCase(str || "", opts)) as CamelCase<
if (!str) {
return "";
}
const rawParts = Array.isArray(str) ? str : splitByCase(str as string);
const parts = rawParts.filter(Boolean);
if (parts.length === 0) {
return "";
}
const [first = "", ...rest] = parts;
const firstFormatted =
opts?.normalize || first === first.toUpperCase()
? first.toLowerCase()
: lowerFirst(first);
const restFormatted = rest.map((p) =>
upperFirst(opts?.normalize ? p.toLowerCase() : p),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
);
return (firstFormatted + restFormatted.join("")) as CamelCase<
Comment thread
coderabbitai[bot] marked this conversation as resolved.
T,
UserCaseOptions["normalize"]
>;
Expand Down
16 changes: 15 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ type CapitalizedWords<
Normalize
>
: Accumulator;
type CamelCaseWords<
T extends readonly string[],
Normalize extends boolean | undefined = false,
> = T extends readonly [infer F extends string, ...infer R extends string[]]
? F extends ""
? CamelCaseWords<R, Normalize>
: `${Normalize extends true ? Lowercase<F> : F extends Uppercase<F> ? Lowercase<F> : Uncapitalize<F>}${CapitalizedWords<R, "", Normalize>}`
: "";
type JoinLowercaseWords<
T extends readonly string[],
Joiner extends string,
Expand Down Expand Up @@ -133,7 +141,13 @@ export type CamelCase<
? string
: string[] extends T
? string
: Uncapitalize<PascalCase<T, Normalize>>;
: T extends string
? SplitByCase<T> extends readonly string[]
? CamelCaseWords<SplitByCase<T>, Normalize>
: never
: T extends readonly string[]
? CamelCaseWords<T, Normalize>
: never;

export type KebabCase<
T extends string | readonly string[],
Expand Down
20 changes: 19 additions & 1 deletion test/scule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,27 @@ describe("pascalCase", () => {

describe("camelCase", () => {
test.each([
["", ""],
["-", ""],
[[], ""],
["FooBarBaz", "fooBarBaz"],
["FOO_BAR", "fooBar"],
["APIBaseURL", "apiBaseURL"],
["XMLParser", "xmlParser"],
["HTTPSResponse", "httpsResponse"],
["-foo", "foo"],
["--foo-bar--", "fooBar"],
[["fooBar", "Baz"], "fooBarBaz"],
[["API", "Base", "URL"], "apiBaseURL"],
])("%s => %s", (input, expected) => {
expect(camelCase(input)).toMatchObject(expected);
});

test.each([
["FooBarBaz", "fooBarBaz"],
["FOO_BAR", "fooBar"],
["API_Base_URL", "apiBaseUrl"],
["MY_API_KEY", "myApiKey"],
])("%s (normalized) => %s", (input, expected) => {
expect(camelCase(input, { normalize: true })).toMatchObject(expected);
});
});
Expand Down
4 changes: 4 additions & 0 deletions test/types.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,14 @@ describe("CamelCase", () => {
assertType<CamelCase<"FooBARb", true>>("fooBaRb");
assertType<CamelCase<"foo_bar-baz/qux", true>>("fooBarBazQux");
assertType<CamelCase<"FOO_BAR", true>>("fooBar");
assertType<CamelCase<"APIBaseURL">>("apiBaseURL");
assertType<CamelCase<"APIBaseURL", true>>("apiBaseUrl");
});

test("array", () => {
assertType<CamelCase<["Foo", "Bar"], true>>("fooBar");
assertType<CamelCase<["fooBar", "Baz"]>>("fooBarBaz");
assertType<CamelCase<["API", "Base", "URL"]>>("apiBaseURL");
});
});

Expand Down