kbrabrand / camelize-ts

A typescript typed camelCase function that recursively camel cases a snake cased object structure
22 stars 6 forks source link

Support pascal case to camel case? #49

Closed ghmendonca closed 1 year ago

ghmendonca commented 1 year ago

Let's say that we have an object with this shape:

const object = {
  'snake_field': 'snake',
  PascalField: 'pascal'
}

Right now, this returns the type:

type Object = {
  snakeField: string,
  PascalField: string
}

I believe the type should be:

type Object = {
  snakeField: string,
  pascalField: string
}

This is very easy to do, all we need is to change CamelizeObject to:

type CamelizeObject<T, S = false> = {
  [K in keyof T as Uncapitalize<CamelCase<string & K>>]: T[K] extends Date
    ? T[K]
    : T[K] extends RegExp
    ? T[K]
    : T[K] extends Array<infer U>
    ? U extends {} | undefined
      ? Array<CamelizeObject<U>>
      : T[K]
    : T[K] extends {} | undefined
    ? S extends true
      ? T[K]
      : CamelizeObject<T[K]>
    : T[K];
};

The only thing that I changed, is that I added Uncapitalize before CamelCase. If this is fine, I can open a PR

kbrabrand commented 1 year ago

That sounds sensible. A PR is welcome 👍

jonkoops commented 1 year ago

Yeah, this would definitely be an interesting contribution. Feel free to open up a PR!

ghmendonca commented 1 year ago

Great! PR opened -> https://github.com/kbrabrand/camelize-ts/pull/50