Skip to content

Instantly share code, notes, and snippets.

@daanta-real
Created June 13, 2024 05:25
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.
변환기: kebobcase/camlcase 형식 ↔ dbms column name 형식
// str를 입력받아, 이것이 kebobcase 혹은 camelcase이면, 대문자 단어들을 밑줄로 연결하는 DB용 컬럼명 형식으로 바꿔주고,
// 반대로 DB용 컬럼명 형식을 받는다면 camelcase로 변환해 주는 함수이다. 파견지에서 인터넷 끊어놓고 작업하라고 할 때 자주 쓰여서 메모.
function convertCase(str) {
return str.includes('-') || str.includes('_')
? str.toLowerCase().replace(/[-_](.)/g, (m, c) => c.toUpperCase())
: str.replace(/([a-z])([A-Z])/g, '$1_$2').toUpperCase()
}
const result = convertCase('superStatCd')
console.log(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment