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: object properties, func, and so on must not convert to camelCase #112

Merged
merged 2 commits into from
Aug 16, 2023
Merged
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
36 changes: 17 additions & 19 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type {CamelCase, PascalCase} from 'type-fest';
// eslint-disable-next-line @typescript-eslint/ban-types
type EmptyTuple = [];

type ObjectOptional = Record<string, unknown> | undefined;

/**
Return a default type if input type is nil.

Expand Down Expand Up @@ -36,7 +38,7 @@ type AppendPath<S extends string, Last extends string> = S extends ''
Convert keys of an object to camelcase strings.
*/
export type CamelCaseKeys<
T extends Record<string, any> | readonly any[],
T extends ObjectOptional | readonly any[],
Deep extends boolean = false,
IsPascalCase extends boolean = false,
Exclude extends readonly unknown[] = EmptyTuple,
Expand All @@ -45,20 +47,17 @@ export type CamelCaseKeys<
> = T extends readonly any[]
// Handle arrays or tuples.
? {
[P in keyof T]: T[P] extends Record<string, any> | readonly any[]
// eslint-disable-next-line @typescript-eslint/ban-types
? {} extends CamelCaseKeys<T[P]>
? T[P]
: CamelCaseKeys<
T[P],
Deep,
IsPascalCase,
Exclude,
StopPaths
>
[P in keyof T]: T[P] extends Record<string, unknown> | readonly any[]
? CamelCaseKeys<
T[P],
Deep,
IsPascalCase,
Exclude,
StopPaths
>
: T[P];
}
: T extends Record<string, any>
: T extends Record<string, unknown>
// Handle objects.
? {
[P in keyof T as [IsInclude<Exclude, P>] extends [true]
Expand All @@ -69,10 +68,8 @@ export type CamelCaseKeys<
true,
]
? T[P]
// eslint-disable-next-line @typescript-eslint/ban-types
: {} extends CamelCaseKeys<T[P]>
Copy link
Contributor Author

@yutak23 yutak23 Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been added in this PR, which is essentially a Record<string, any>, which would have been {} due to the fact that it is a Record<string, any>. This change to unknow makes this condition unnecessary.

// type result is 'true'
type testType<T> = T extends Record<string, any> ? 'true' : 'false';
type result = testType<() => 123>;

? T[P]
: [Deep] extends [true]
: [Deep] extends [true]
? T[P] extends ObjectOptional | readonly any[]
Copy link
Contributor Author

@yutak23 yutak23 Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To support optional properties.
This change allows the following tests to pass.

expectType<ConvertedDeepObjectDataTypeWithDeepTrue>(
	camelcaseKeys(deepInputData, {deep: true}),
);

? CamelCaseKeys<
T[P],
Deep,
Expand All @@ -81,7 +78,8 @@ export type CamelCaseKeys<
StopPaths,
AppendPath<Path, P & string>
>
: T[P];
: T[P]
: T[P];
}
// Return anything else as-is.
: T;
Expand Down Expand Up @@ -190,7 +188,7 @@ camelcaseKeys(commandLineArguments);
```
*/
export default function camelcaseKeys<
T extends Record<string, any> | readonly any[],
T extends Record<string, unknown> | readonly any[],
OptionsType extends Options = Options,
>(
input: T,
Expand Down
49 changes: 49 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ expectType<{readonly fooBar: true}>(camelcaseKeys({'foo bar': true} as const));
expectType<{fooBar: {fooBar: {fooBar: boolean}}}>(
camelcaseKeys({'foo-bar': {foo_bar: {'foo bar': true}}}, {deep: true}),
);
expectType<{fooBar: Array<{fooBar: {fooBar: boolean}}>}>(
camelcaseKeys({'foo-bar': [{foo_bar: {'foo bar': true}}]}, {deep: true}),
);

type ObjectOrUndefined = {
foo_bar: {
Expand Down Expand Up @@ -113,6 +116,8 @@ const someObject: SomeObject = {

expectType<SomeObject>(camelcaseKeys(someObject));
expectType<SomeObject[]>(camelcaseKeys([someObject]));
expectType<{someObject: SomeObject}>(camelcaseKeys({some_object: someObject}));
expectType<Array<{someObject: SomeObject}>>(camelcaseKeys([{some_object: someObject}]));

type SomeTypeAlias = {
someProperty: string;
Expand Down Expand Up @@ -280,6 +285,13 @@ type DeepObjectType = {
bar_baz?: string;
};
};
optional_first_level?: {
foo_bar?: string;
bar_baz?: true;
optional_second_level?: {
foo_bar: number;
};
};
};
type InvalidConvertedDeepObjectDataType = {
fooBar?: string;
Expand All @@ -293,6 +305,13 @@ type InvalidConvertedDeepObjectDataType = {
barBaz?: string;
};
};
optional_first_level?: {
fooBar?: string;
barBaz?: true;
optional_second_level?: {
fooBar: number;
};
};
};
type ConvertedDeepObjectDataType = {
fooBar?: string;
Expand All @@ -306,6 +325,33 @@ type ConvertedDeepObjectDataType = {
bar_baz?: string;
};
};
optionalFirstLevel?: {
foo_bar?: string;
bar_baz?: true;
optional_second_level?: {
foo_bar: number;
};
};
};
type ConvertedDeepObjectDataTypeWithDeepTrue = {
fooBar?: string | undefined;
barBaz?: string | undefined;
baz: string;
firstLevel: {
fooBar?: string | undefined;
barBaz?: string | undefined;
secondLevel: {
fooBar: string;
barBaz?: string | undefined;
};
};
optionalFirstLevel?: {
fooBar?: string;
barBaz?: true;
optionalSecondLevel?: {
fooBar: number;
};
};
};
const deepInputData: DeepObjectType = {
foo_bar: 'foo_bar',
Expand All @@ -320,6 +366,9 @@ const deepInputData: DeepObjectType = {
expectType<ConvertedDeepObjectDataType>(
camelcaseKeys(deepInputData, {deep: false}),
);
expectType<ConvertedDeepObjectDataTypeWithDeepTrue>(
camelcaseKeys(deepInputData, {deep: true}),
);
expectNotType<InvalidConvertedDeepObjectDataType>(
camelcaseKeys(deepInputData, {deep: false}),
);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"ava": "^5.0.1",
"matcha": "^0.7.0",
"tsd": "^0.24.1",
"xo": "^0.52.4"
"xo": "^0.54.2"
Copy link
Contributor Author

@yutak23 yutak23 Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have version up because following issue error (on local testing).

xojs/xo#718

},
"xo": {
"overrides": [
Expand Down