본문 바로가기
Node.js

[Node.js] 문자열 표기법 - Camel Case, Pascal Case, Kebab Case, Snake Case

by yonikim 2022. 7. 21.
728x90

프로그래밍을 할 때 우리는 보통 단어 사이의 공백을 제거하여 문자열을 표현하는데, 그 표기법에는 무엇이 있는지 알아보자.

 

 

Camel Case (카멜 케이스)

낙타의 쌍봉과 같이 문자열의 첫 문자를 제외하고 단어의 첫 글자마다 대문자로 표현하는 방식이다. 많은 프로그래밍 언어에서 컨벤션으로 사용된다.

myCamelCase
const toCamelCase = (text) => {
  return text.toLowerCase().replace(/-\w|\s\w/g, clearAndUpper);
};

const clearAndUpper = (text) => {
  return text.replace(/-|\s/, "").toUpperCase();
};

 

 

 

Pascal Case (파스칼 케이스)

카멜 케이스와 유사하지만 첫 문자도 대문자로 표현한다.

MyPascalCase
const toPascalCase = (text) => {
  return text.replace(/(^\w|-\w|\s\w)/g, clearAndUpper);
};

const clearAndUpper = (text) => {
  return text.replace(/-|\s/, "").toUpperCase();
};

 

 

Kebab Case (케밥 케이스)

모두 소문자로 표현하며 단어와 단어 사이를 대시(-) 를 이용하여 구분한다. yml 파일이나 url 주소에서 사용된다.

my-kebab-case
const toKebabCase = (text) => {
  return text
    .replace(/\s|_|-/g, "")
    .replace(
      /[A-Z]+(?![a-z])|[A-Z]/g,
      ($, ofs) => (ofs ? "-" : "") + $.toLowerCase(),
    );
};

 

 

Snake Case (스네이크 케이스)

케밥의 대시(-) 와 다르게 언더스코어(_) 를 구분자로 한다. 모든 문자를 대문자로 표현할 수도 있는데, 이는 주로 상수 표현 시에 사용된다.

my_snake_case
const toSnakeCase = (text) => {
  return text
    .replace(/\s|_|-/g, "")
    .replace(
      /[A-Z]+(?![a-z])|[A-Z]/g,
      ($, ofs) => (ofs ? "_" : "") + $.toLowerCase(),
    );
};

 

 

 

 

 

 

728x90