Skip to content

Commit

Permalink
Fix change-case/keys types (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingajda authored Apr 3, 2024
1 parent 531c83c commit fec0211
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
10 changes: 9 additions & 1 deletion packages/change-case/src/keys.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { describe, it, expect } from "vitest";
import { camelCase } from "./keys";
import * as changeCase from "./index.js";

const TEST_CASES: [unknown, number | undefined, unknown][] = [
type TestCase = [
unknown,
number | undefined,
unknown,
(changeCase.Options | changeCase.PascalCaseOptions)?,
];

const TEST_CASES: TestCase[] = [
[
{
first_name: "bob",
Expand Down
16 changes: 11 additions & 5 deletions packages/change-case/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import * as changeCase from "./index.js";
const isObject = (object: unknown) =>
object !== null && typeof object === "object";

function changeKeysFactory(
function changeKeysFactory<
Options extends changeCase.Options = changeCase.Options,
>(
changeCase: (input: string, options?: changeCase.Options) => string,
): (object: unknown, depth?: number) => unknown {
): (object: unknown, depth?: number, options?: Options) => unknown {
return function changeKeys(
object: unknown,
depth = 1,
options?: changeCase.Options,
options?: Options,
): unknown {
if (depth === 0 || !isObject(object)) return object;

Expand All @@ -32,14 +34,18 @@ function changeKeysFactory(
};
}

export const camelCase = changeKeysFactory(changeCase.camelCase);
export const camelCase = changeKeysFactory<changeCase.PascalCaseOptions>(
changeCase.camelCase,
);
export const capitalCase = changeKeysFactory(changeCase.capitalCase);
export const constantCase = changeKeysFactory(changeCase.constantCase);
export const dotCase = changeKeysFactory(changeCase.dotCase);
export const trainCase = changeKeysFactory(changeCase.trainCase);
export const noCase = changeKeysFactory(changeCase.noCase);
export const kebabCase = changeKeysFactory(changeCase.kebabCase);
export const pascalCase = changeKeysFactory(changeCase.pascalCase);
export const pascalCase = changeKeysFactory<changeCase.PascalCaseOptions>(
changeCase.pascalCase,
);
export const pathCase = changeKeysFactory(changeCase.pathCase);
export const sentenceCase = changeKeysFactory(changeCase.sentenceCase);
export const snakeCase = changeKeysFactory(changeCase.snakeCase);

0 comments on commit fec0211

Please sign in to comment.