kbrabrand / camelize-ts

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

Date should be ignored by Camelize #45

Closed mikebridge closed 1 year ago

mikebridge commented 1 year ago

Camelize currently tries to convert the case in my Date objects here. I wonder if this might work:

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

So:


interface TestDateType {
  my_date: Date
  my_string: string
}

type CamelTestDateType = Camelize<TestDateType>
const date = new Date()

const obj: CamelTestDateType = {
  myDate: date,
  myString: "test"
}

expect(obj.myDate.toISOString()).toBe(date.toISOString())
kbrabrand commented 1 year ago

Thanks for the input, @mikebridge 🙏 I've pushed a fix for this issue. Also, as mentioned in my comment on #46, the two exceptions handled in the codeflow (Date and RegExp) are now handled in the type conversion too.