| 1 | type SnakeToCamel<S extends string> = S extends `${infer H}_${infer T}` ? `${Lowercase<H>}${Capitalize<SnakeToCamel<T>>}` : Lowercase<S>;
|
|---|
| 2 | type PascalToCamel<S extends string> = S extends `${infer F}${infer R}` ? `${Lowercase<F>}${R}` : S;
|
|---|
| 3 | /** If it's snake_case, apply the snake_case rule; for uppercase keys, lowercase the entire string; otherwise, just lowercase the first letter (including PascalCase → camelCase). */
|
|---|
| 4 | type AnyToCamel<S extends string> = S extends `${string}_${string}` ? SnakeToCamel<S> : S extends Uppercase<S> ? Lowercase<S> : PascalToCamel<S>;
|
|---|
| 5 | type NonPlainObject = Date | RegExp | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any> | Promise<any> | Error | ArrayBuffer | DataView | Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array | ((...args: any[]) => any) | typeof globalThis;
|
|---|
| 6 | type ToCamelCaseKeys<T> = T extends NonPlainObject ? T : T extends any[] ? Array<ToCamelCaseKeys<T[number]>> : T extends Record<string, any> ? {
|
|---|
| 7 | [K in keyof T as AnyToCamel<Extract<K, string>>]: ToCamelCaseKeys<T[K]>;
|
|---|
| 8 | } : T;
|
|---|
| 9 | /**
|
|---|
| 10 | * Creates a new object composed of the properties with keys converted to camelCase.
|
|---|
| 11 | *
|
|---|
| 12 | * This function takes an object and returns a new object that includes the same properties,
|
|---|
| 13 | * but with all keys converted to camelCase format.
|
|---|
| 14 | *
|
|---|
| 15 | * @template T - The type of object.
|
|---|
| 16 | * @param {T} obj - The object to convert keys from.
|
|---|
| 17 | * @returns {ToCamelCaseKeys<T>} A new object with all keys converted to camelCase.
|
|---|
| 18 | *
|
|---|
| 19 | * @example
|
|---|
| 20 | * // Example with objects
|
|---|
| 21 | * const obj = { user_id: 1, first_name: 'John' };
|
|---|
| 22 | * const result = toCamelCaseKeys(obj);
|
|---|
| 23 | * // result will be { userId: 1, firstName: 'John' }
|
|---|
| 24 | *
|
|---|
| 25 | * // Example with arrays of objects
|
|---|
| 26 | * const arr = [
|
|---|
| 27 | * { user_id: 1, first_name: 'John' },
|
|---|
| 28 | * { user_id: 2, first_name: 'Jane' }
|
|---|
| 29 | * ];
|
|---|
| 30 | * const arrResult = toCamelCaseKeys(arr);
|
|---|
| 31 | * // arrResult will be [{ userId: 1, firstName: 'John' }, { userId: 2, firstName: 'Jane' }]
|
|---|
| 32 | *
|
|---|
| 33 | * // Example with nested objects
|
|---|
| 34 | * const nested = {
|
|---|
| 35 | * user_data: {
|
|---|
| 36 | * user_id: 1,
|
|---|
| 37 | * user_address: {
|
|---|
| 38 | * street_name: 'Main St',
|
|---|
| 39 | * zip_code: '12345'
|
|---|
| 40 | * }
|
|---|
| 41 | * }
|
|---|
| 42 | * };
|
|---|
| 43 | * const nestedResult = toCamelCaseKeys(nested);
|
|---|
| 44 | * // nestedResult will be:
|
|---|
| 45 | * // {
|
|---|
| 46 | * // userData: {
|
|---|
| 47 | * // userId: 1,
|
|---|
| 48 | * // userAddress: {
|
|---|
| 49 | * // streetName: 'Main St',
|
|---|
| 50 | * // zipCode: '12345'
|
|---|
| 51 | * // }
|
|---|
| 52 | * // }
|
|---|
| 53 | * // }
|
|---|
| 54 | */
|
|---|
| 55 | declare function toCamelCaseKeys<T>(obj: T): ToCamelCaseKeys<T>;
|
|---|
| 56 |
|
|---|
| 57 | export { toCamelCaseKeys };
|
|---|