Python 예시
Python에서 snake_case를 camelCase로 변환
snake_case를 밑줄로 나누고 첫 token은 소문자, 나머지는 대문자로 시작해 연결합니다.
Python 코드
def snake_to_camel(value: str) -> str:
parts = [part for part in value.strip("_").split("_") if part]
if not parts:
return ""
head, *tail = parts
return head.lower() + "".join(part[:1].upper() + part[1:].lower() for part in tail)
print(snake_to_camel("user_profile_image"))
# userProfileImage앞뒤 밑줄과 반복 밑줄의 빈 token을 제거해 일반적인 snake_case를 안정적으로 변환합니다.
Python 자체는 snake_case가 일반적이므로 JavaScript frontend, API, JSON과 만나는 경계에서 특히 유용합니다.
고려할 예외 사례
- 약어 출력은 소비 시스템의 규칙에 따라 달라집니다.
- 중첩 dict는 문자열 helper가 아니라 재귀 key 변환이 필요합니다.
- 대문자, 하이픈, 공백이 섞인 값은 일반화된 tokenization이 필요합니다.
- 고정 API contract라면 serialization/deserialization 경계에서 변환하는 편이 명확합니다.
예시
| 변환 전 | 변환 후 |
|---|---|
| user_profile_image | userProfileImage |
| __user__profile__ | userProfile |
| api_response_url | apiResponseUrl |
| created_at | createdAt |
Python 코드
cases = {
"user_profile_image": "userProfileImage",
"__user__profile__": "userProfile",
"api_response_url": "apiResponseUrl",
"created_at": "createdAt",
}
for value, expected in cases.items():
assert snake_to_camel(value) == expected온라인에서 변환 테스트
빠른 확인, batch, 약어 처리, 혼합 네이밍 입력을 브라우저 도구로 테스트하세요.
감지: snake_case
camelCase 출력
userProfileImageUrl
모든 네이밍 형식
camelCase
userProfileImageUrlPascalCase
UserProfileImageUrlsnake_case
user_profile_image_urlkebab-case
user-profile-image-urlCONSTANT_CASE
USER_PROFILE_IMAGE_URLdot.case
user.profile.image.urlpath/case
user/profile/image/url변환은 브라우저에서 로컬로 처리되며 입력은 변환 서버로 전송되지 않습니다.