Skip to content

Commit

Permalink
Merge pull request RossWilliams#49 from RossWilliams/feat/nullable-ob…
Browse files Browse the repository at this point in the history
…jects

feat: handle nullable objects
  • Loading branch information
RossWilliams authored Jun 29, 2021
2 parents cfaac6f + 7ff58dd commit f0c2d0f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 41 deletions.
91 changes: 50 additions & 41 deletions src/caseConvert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,39 +103,45 @@ export type ToCamel<S extends string | number | symbol> = S extends string
: Uncapitalize<S>
: never;

export type ObjectToCamel<T extends object | undefined> = T extends undefined
? undefined
: {
[K in keyof T as ToCamel<K>]: T[K] extends Array<unknown>
? T[K] extends Array<infer ArrayType>
? ArrayType extends object
? Array<ObjectToCamel<ArrayType>>
: T[K]
: never
: T[K] extends object | undefined
? ObjectToCamel<T[K]>
: T[K];
};
export type ObjectToCamel<T extends object | undefined | null> =
T extends undefined
? undefined
: T extends null
? null
: {
[K in keyof T as ToCamel<K>]: T[K] extends Array<unknown>
? T[K] extends Array<infer ArrayType>
? ArrayType extends object
? Array<ObjectToCamel<ArrayType>>
: T[K]
: never
: T[K] extends object | undefined | null
? ObjectToCamel<T[K]>
: T[K];
};

export type ToPascal<S extends string | number | symbol> = S extends string
? S extends `${infer Head}_${infer Tail}`
? `${Capitalize<Head>}${Capitalize<ToCamel<Tail>>}`
: Capitalize<S>
: never;

export type ObjectToPascal<T extends object | undefined> = T extends undefined
? undefined
: {
[K in keyof T as ToPascal<K>]: T[K] extends Array<unknown>
? T[K] extends Array<infer ArrayType>
? ArrayType extends object
? Array<ObjectToPascal<ArrayType>>
: T[K]
: never
: T[K] extends object | undefined
? ObjectToPascal<T[K]>
: T[K];
};
export type ObjectToPascal<T extends object | undefined | null> =
T extends undefined
? undefined
: T extends null
? null
: {
[K in keyof T as ToPascal<K>]: T[K] extends Array<unknown>
? T[K] extends Array<infer ArrayType>
? ArrayType extends object
? Array<ObjectToPascal<ArrayType>>
: T[K]
: never
: T[K] extends object | undefined | null
? ObjectToPascal<T[K]>
: T[K];
};

export type ToSnake<S extends string | number | symbol> = S extends string
? S extends `${infer Head}${CapitalChars}${infer Tail}` // string has a capital char somewhere
Expand Down Expand Up @@ -179,21 +185,24 @@ export type ToSnake<S extends string | number | symbol> = S extends string
: S /* 'abc' */
: never;

export type ObjectToSnake<T extends object | undefined> = T extends undefined
? undefined
: {
[K in keyof T as ToSnake<K>]: T[K] extends Array<unknown>
? T[K] extends Array<infer ArrayType>
? ArrayType extends object
? Array<ObjectToSnake<ArrayType>>
: T[K]
: never
: T[K] extends object | undefined
? ObjectToSnake<T[K]>
: // Exclude<T[K], undefined> extends object
// ? ObjectToSnake<Exclude<T[K], undefined>>
T[K];
};
export type ObjectToSnake<T extends object | undefined | null> =
T extends undefined
? undefined
: T extends null
? null
: {
[K in keyof T as ToSnake<K>]: T[K] extends Array<unknown>
? T[K] extends Array<infer ArrayType>
? ArrayType extends object
? Array<ObjectToSnake<ArrayType>>
: T[K]
: never
: T[K] extends object | undefined | null
? ObjectToSnake<T[K]>
: // Exclude<T[K], undefined> extends object
// ? ObjectToSnake<Exclude<T[K], undefined>>
T[K];
};

type CapitalLetters =
| 'A'
Expand Down
17 changes: 17 additions & 0 deletions test/caseConvert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,20 @@ type T27 = ToCamel<'helloWorld'>;
const _t27: AssertEqual<T27, 'helloWorld'> = true;
type T28 = ToCamel<'8HelloWorld'>;
const _t28: AssertEqual<T28, '8HelloWorld'> = true;

interface I243 {
nullable_object: { a_prop: string } | null;
}

const _c243: I243 = {
nullable_object: {
a_prop: 'a',
},
};
interface I2432 {
nullableObject: { aProp: string } | null;
}

const _c2432: I2432 = objectToCamel(_c243);

const _s243: AssertEqual<I243, ObjectToSnake<I2432>> = true;

0 comments on commit f0c2d0f

Please sign in to comment.