-
-
Notifications
You must be signed in to change notification settings - Fork 662
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Version 9.0.0 of camelcase changed the behavior to preserve leading underscores. This means that now trying to deeply camel case an object by mapping the keys through camelcase results in a different runtime output than the result of CamelCasedPropertiesDeep.
For example:
type DeeplyCamelCase<T> = CamelCasedPropertiesDeep<
T,
{ preserveConsecutiveUppercase: true }
>;
/**
* Deeply camelCases an object or array
*/
function deeplyCamelCase<T>(input: T): DeeplyCamelCase<T>;
function deeplyCamelCase(obj: unknown): unknown {
if (Array.isArray(obj)) {
return obj.map(deeplyCamelCase);
}
if (obj !== null && typeof obj === 'object') {
return Object.entries(obj).reduce<Record<string, unknown>>(
(result, [key, value]) => {
const camelCaseKey = camelCase(key, {
preserveConsecutiveUppercase: true,
});
result[camelCaseKey] = deeplyCamelCase(value);
return result;
},
{},
);
}
return obj;
}
const dcc = deeplyCamelCase({ _foo_bar: 'baz' });
// At this point dcc has type `{ fooBar }`, but the runtime output is `{ _fooBar }`I think that these types should be kept in sync (or at least have an option, like preserveConsecutiveUppercase, to keep leading underscores).
sindresorhus
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request