Skip to content

Instantly share code, notes, and snippets.

@daanta-real
Last active January 1, 2025 18:46
Show Gist options
  • Save daanta-real/6409b4dcdfdfdf74a16e9b38ff8943a9 to your computer and use it in GitHub Desktop.
Save daanta-real/6409b4dcdfdfdf74a16e9b38ff8943a9 to your computer and use it in GitHub Desktop.
변환기: kebob-case/camelCase/snake_case 형식 간 변환
// 케밥케이스, 스네이크케이스, 카멜케이스 변환 함수 (DB용 컬럼명도 처리)
function convertCase(str) {
// 탭 구분된 문자열 처리
return str.includes('\t')
? str.split('\t').map(convertCase).join('\t') // 탭으로 분리된 문자열에 대해 반복 처리
: (str.includes('-') || str.includes('_') // 케밥케이스 또는 스네이크케이스
? str.toLowerCase().replace(/[-_](.)/g, (m, c) => c.toUpperCase()) // 케밥/스네이크 -> 카멜
: str.replace(/([a-z])([A-Z])/g, '$1_$2').toUpperCase()); // 카멜 -> DB용(스네이크)
}
// 샘플 1: 카멜케이스 -> DB용 형식
const camelCaseString = "superStatCd";
console.log("Camel to DB:", convertCase(camelCaseString));
// 샘플 2: DB용 형식 -> 카멜케이스
const dbStyleString = "ITEM_NO ID NAME DATESTR";
console.log("DB to Camel:", convertCase(dbStyleString));
// 샘플 3: 탭 구분된 여러 단어 변환
const mixedString = "superStatCd\titemNo\tuserName";
console.log("Mixed to DB:", convertCase(mixedString));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment