Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion docs/ja/reference/object/toCamelCaseKeys.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ const camelCased = toCamelCaseKeys(obj);

### `toCamelCaseKeys(obj)`

オブジェクトのすべてのキーをキャメルケースに変換したい時に`toCamelCaseKeys`を使用してください。ネストされたオブジェクトと配列内のオブジェクトも再帰的に変換されます。
オブジェクトのすべてのキーをキャメルケースに変換したい時に `toCamelCaseKeys` を使用してください。ネストされたオブジェクトと配列内のオブジェクトも再帰的に変換されます。

- `snake_case` → `camelCase`(例: `user_id` → `userId`)
- `PascalCase` → `camelCase`(例: `UserId` → `userId`)
- `uppercase keys` → `camelCase`(例: `FIRST_NAME` → `firstName`, `LAST` → `last`)

```typescript
import { toCamelCaseKeys } from 'es-toolkit/object';
Expand Down Expand Up @@ -50,6 +54,11 @@ const nestedResult = toCamelCaseKeys(nested);
// }
// }
// }になります

// PascalCase と uppercase keys のキーも変換されます
const raw = { UserId: 1, FIRST_NAME: 'JinHo', LAST: 'Yeom' };
const converted = toCamelCaseKeys(raw);
// converted は { userId: 1, firstName: 'JinHo', last: 'Yeom' } になります
```

#### パラメータ
Expand Down
9 changes: 9 additions & 0 deletions docs/ko/reference/object/toCamelCaseKeys.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const camelCased = toCamelCaseKeys(obj);

객체의 모든 키를 카멜 케이스로 변환하고 싶을 때 `toCamelCaseKeys`를 사용하세요. 중첩된 객체와 배열 내의 객체들도 재귀적으로 변환돼요.

- `snake_case` → `camelCase` (예: `user_id` → `userId`)
- `PascalCase` → `camelCase` (예: `UserId` → `userId`)
- `uppercase keys` → `camelCase` (예: `FIRST_NAME` → `firstName`, `LAST` → `last`)

```typescript
import { toCamelCaseKeys } from 'es-toolkit/object';

Expand Down Expand Up @@ -50,6 +54,11 @@ const nestedResult = toCamelCaseKeys(nested);
// }
// }
// }가 돼요

// PascalCase와 uppercase keys 키도 변환돼요
const raw = { UserId: 1, FIRST_NAME: 'JinHo', LAST: 'Yeom' };
const converted = toCamelCaseKeys(raw);
// converted는 { userId: 1, firstName: 'JinHo', last: 'Yeom' }가 돼요
```

#### 파라미터
Expand Down
9 changes: 9 additions & 0 deletions docs/reference/object/toCamelCaseKeys.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const camelCased = toCamelCaseKeys(obj);

Use `toCamelCaseKeys` when you want to convert all keys of an object to camel case. Nested objects and objects within arrays are also converted recursively.

- `snake_case` → `camelCase` (e.g. `user_id` → `userId`)
- `PascalCase` → `camelCase` (e.g. `UserId` → `userId`)
- `uppercase keys` → `camelCase` (e.g. `FIRST_NAME` → `firstName`, `LAST` → `last`)

```typescript
import { toCamelCaseKeys } from 'es-toolkit/object';

Expand Down Expand Up @@ -50,6 +54,11 @@ const nestedResult = toCamelCaseKeys(nested);
// }
// }
// }

// PascalCase and uppercase keys are also converted
const raw = { UserId: 1, FIRST_NAME: 'JinHo', LAST: 'Yeom' };
const converted = toCamelCaseKeys(raw);
// converted is { userId: 1, firstName: 'JinHo', last: 'Yeom' }
```

#### Parameters
Expand Down
9 changes: 9 additions & 0 deletions docs/zh_hans/reference/object/toCamelCaseKeys.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const camelCased = toCamelCaseKeys(obj);

当您想要将对象的所有键转换为驼峰命名法时,请使用 `toCamelCaseKeys`。嵌套对象和数组中的对象也会递归转换。

- `snake_case` → `camelCase`(例如 `user_id` → `userId`)
- `PascalCase` → `camelCase`(例如 `UserId` → `userId`)
- `uppercase keys` → `camelCase`(例如 `FIRST_NAME` → `firstName`, `LAST` → `last`)

```typescript
import { toCamelCaseKeys } from 'es-toolkit/object';

Expand Down Expand Up @@ -50,6 +54,11 @@ const nestedResult = toCamelCaseKeys(nested);
// }
// }
// }

// PascalCase 和 uppercase keys 的键也会被转换
const raw = { UserId: 1, FIRST_NAME: 'JinHo', LAST: 'Yeom' };
const converted = toCamelCaseKeys(raw);
// converted 是 { userId: 1, firstName: 'JinHo', last: 'Yeom' }
```

#### 参数
Expand Down
42 changes: 42 additions & 0 deletions src/object/toCamelCaseKeys.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,46 @@ describe('camelizeKeys', () => {
expectTypeOf(result.b).toEqualTypeOf<RegExp>();
expectTypeOf(result.c).toEqualTypeOf<Map<any, any>>();
});

it('should convert uppercase keys to camelCase at both runtime and type level', () => {
const input = {
FIRST_NAME: 'JinHo',
LAST: 'Yeom',
} as const;

const result = toCamelCaseKeys(input);

expect(result).toEqual({
firstName: 'JinHo',
last: 'Yeom',
});

expectTypeOf(result).toMatchTypeOf<{
firstName: 'JinHo';
last: 'Yeom';
}>();
});
it('should not recurse into NonPlainObject values', () => {
const date = new Date();
const map = new Map<string, any>([['first_name', 'JinHo']]);
const set = new Set<number>([1, 2, 3]);

const input = {
created_at: date,
meta_map: map,
ids_set: set,
};

const result = toCamelCaseKeys(input);

expect(result.createdAt).toBe(date);
expect(result.metaMap).toBe(map);
expect(result.idsSet).toBe(set);

expectTypeOf(result).toMatchTypeOf<{
createdAt: Date;
metaMap: Map<string, any>;
idsSet: Set<number>;
}>();
});
});
5 changes: 3 additions & 2 deletions src/object/toCamelCaseKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ type SnakeToCamel<S extends string> = S extends `${infer H}_${infer T}`
? `${Lowercase<H>}${Capitalize<SnakeToCamel<T>>}`
: Lowercase<S>;

type PascalToCamel<S extends string> = S extends `${infer F}${infer R}` ? `${Lowercase<F>}${R}` : S;
type PascalToCamel<S extends string> =
S extends Uppercase<S> ? Lowercase<S> : S extends `${infer F}${infer R}` ? `${Lowercase<F>}${R}` : S;

/** If it's snake_case, apply the snake_case rule; otherwise, just lowercase the first letter (including PascalCase → camelCase). */
/** 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). */
type AnyToCamel<S extends string> = S extends `${string}_${string}` ? SnakeToCamel<S> : PascalToCamel<S>;

type NonPlainObject =
Expand Down