kbrabrand / camelize-ts

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

fix: ignore dates in Camelize #46

Closed mikebridge closed 1 year ago

mikebridge commented 1 year ago

Closes #45

jonkoops commented 1 year ago

Hmmm, this seems like a good change. But looks like this would still create issues if other types of objects are passed in (such as ArrayBuffer, or anything else really). I wonder if we can solve this generically.

mikebridge commented 1 year ago

That's a good question---I wonder there's a way of using the equivalent of Object.prototype.toString to differentiate objects. I don't know how to use this as a conditional type though:

Object.prototype.toString.call(obj) === '[object Object]'
mikebridge commented 1 year ago

I don't know if it is related but I also see that setting a nested array to optional seems to confuse Camelize too:

it('converts an array that can be undefined', () => {
  interface TestType1 {
    a_b: string
  }
  interface ArrayOrUndefinedTest  {
    obj: {
      test_arr?: TestType1[]  // <---------------- if the question mark is removed, "const result=" line below works.
    }
  }

  const parent: SnakeCaseToCamelCase<ArrayOrUndefinedTest> = {
    obj: {
      testArr: [{ aB: 'test'}],
    },
  }

  const result = parent.obj.testArr?.map((z) => z.aB)
  expect(result[0]).toBe('test')
})

Specifically, the ".map" gives this error:

Type 'Camelize<(<U>(callbackfn: (value: TestType1, index: number, array: TestType1[]) => U, thisArg?: any) => U[]), false>' has no call signatures.
kbrabrand commented 1 year ago

Sorry about the slow response here. I don't really see a great way of handling this in a generic way as it would have to be handled generically in both the typing and the actual conversion..

The conversion handles two exceptions; Date and RegExp, so I've added checks for these two types in the typing. This has is out in version 2.5.0.

Also, the issue mentioned about camelizing arrays has been sorted in #48.