Skip to content
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
15 changes: 15 additions & 0 deletions types/camelize/camelize-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,20 @@ camelize("foo-bar" as const);
// $ExpectType Date
camelize(new Date());

// $ExpectType Date
camelize({ "my-date": new Date() }).myDate;

// $ExpectType RegExp
camelize(/foo/);

// $ExpectType RegExp
camelize({ "my-regexp": /foo/ }).myRegexp;

// $ExpectType string[]
camelize(["foo-bar", "bar-baz"]);

// $ExpectType string[]
camelize({ "my-array": ["foo-bar", "bar-baz"] }).myArray;

// $ExpectType { fooBar: number; }[]
camelize({ "my-array": [{ "foo-bar": 1 }] }).myArray;
14 changes: 7 additions & 7 deletions types/camelize/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ type CamelCase<S extends string> = S extends `${infer FirstWord}${CamelSeparator
? `${Lowercase<FirstWord>}${Uppercase<SecondChar>}${CamelCase<Remaining>}`
: Lowercase<S>;

type Camelize<T> = {
[K in keyof T as CamelCase<string & K>]: T[K] extends {} ? Camelize<T[K]> : T[K];
};

declare function camelize<T>(obj: T): T extends string ? CamelCase<T>
type Camelize<T> = T extends Date ? T
: T extends RegExp ? T
: T extends Date ? T
Copy link
Contributor Author

Choose a reason for hiding this comment

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

check for RegExp and Date happens in the walk function which is also run on nested fields. Moving this condition to the Camelize type

: T extends {} ? Camelize<T>
: T extends Array<infer I> ? Array<Camelize<I>>
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've also added support for arrays (these are handled separately in the original package as well)

: T extends object ? {
Copy link
Contributor Author

@lukaskral lukaskral Jul 26, 2024

Choose a reason for hiding this comment

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

I'm changing here T extends {} to T extends object which I believe is more accurate
https://github.com/ljharb/camelize/blob/main/index.js#L9

[K in keyof T as CamelCase<string & K>]: Camelize<T[K]>;
}
: T;

declare function camelize<T>(obj: T): T extends string ? CamelCase<T> : Camelize<T>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

top level exported function only checks whether the provided argument is string
https://github.com/ljharb/camelize/blob/main/index.js#L4


export = camelize;