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: update type for input and optiopns and update README #122

Merged
merged 7 commits into from
Jan 13, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ type AppendPath<S extends string, Last extends string> = S extends ''
Convert keys of an object to camelcase strings.
*/
export type CamelCaseKeys<
T extends ObjectUnion | readonly any[],
T extends ObjectUnion | ReadonlyArray<Record<string, unknown>>,
Deep extends boolean = false,
IsPascalCase extends boolean = false,
PreserveConsecutiveUppercase extends boolean = false,
Exclude extends readonly unknown[] = EmptyTuple,
StopPaths extends readonly string[] = EmptyTuple,
Path extends string = '',
> = T extends readonly any[]
> = T extends ReadonlyArray<Record<string, unknown>>
// Handle arrays or tuples.
? {
[P in keyof T]: T[P] extends Record<string, unknown> | readonly any[]
[P in keyof T]: T[P] extends Record<string, unknown> | ReadonlyArray<Record<string, unknown>>
? CamelCaseKeys<
T[P],
Deep,
Expand All @@ -72,7 +72,7 @@ export type CamelCaseKeys<
]
? T[P]
: [Deep] extends [true]
? T[P] extends ObjectUnion | readonly any[]
? T[P] extends ObjectUnion | ReadonlyArray<Record<string, unknown>>
? CamelCaseKeys<
T[P],
Deep,
Expand Down Expand Up @@ -231,7 +231,7 @@ camelcaseKeys(commandLineArguments);
```
*/
export default function camelcaseKeys<
T extends Record<string, unknown> | readonly any[],
T extends Record<string, unknown> | ReadonlyArray<Record<string, unknown>>,
OptionsType extends Options = Options,
>(
input: T,
Expand Down
22 changes: 2 additions & 20 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ expectType<Array<{fooBar: boolean}>>(camelFooBarArray);

expectType<Array<{fooBar: boolean}>>(camelcaseKeys([{'foo-bar': true}]));

expectType<string[]>(camelcaseKeys(['name 1', 'name 2']));

expectType<string[]>(camelcaseKeys(['name 1', 'name 2'], {deep: true}));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have removed the unsupported array format test.

expectType<readonly [{readonly fooBar: true}, {readonly fooBaz: true}]>(
camelcaseKeys([{'foo-bar': true}, {'foo-baz': true}] as const),
);
Expand Down Expand Up @@ -440,31 +436,17 @@ expectType<{
}),
);

expectType<[
() => 'foo',
{foo: string},
Promise<unknown>,
]>(
camelcaseKeys([
() => 'foo',
{foo: 'bar'},
new Promise(resolve => {
resolve(true);
}),
]),
);

// Test for function with inferred type
// eslint-disable-next-line @typescript-eslint/comma-dangle
function camelcaseKeysDeep<
T extends Record<string, unknown> | readonly unknown[]
T extends Record<string, unknown> | ReadonlyArray<Record<string, unknown>>
>(response: T): CamelCaseKeys<T, true> {
return camelcaseKeys(response, {deep: true});
}

// eslint-disable-next-line @typescript-eslint/comma-dangle
function camelcaseKeysPascalCase<
T extends Record<string, unknown> | readonly unknown[]
T extends Record<string, unknown> | ReadonlyArray<Record<string, unknown>>
>(response: T): CamelCaseKeys<T, false, true> {
return camelcaseKeys(response, {pascalCase: true});
}
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ camelcaseKeys(commandLineArguments);

#### input

Type: `object | object[]`
Type: `Record<string, unknown> | ReadonlyArray<Record<string, unknown>>`

An object or array of objects to camel-case.
A plain object or array of plain objects to camel-case.

#### options

Expand Down