Skip to content

Make CamelCasedPropertiesDeep preserve leading underscores #1299

@simon-abbott

Description

@simon-abbott

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions