한글 조사를 구분하기 위해서는 마지막 문자의 받침 유무를 체크하여 처리하면 된다.
마지막 문자에 받침이 있으면 '-이/-을/-은/-과', 받침이 없으면 '-가/-를/-는/-와'가 된다.
아래는 받침 유무를 체크하는 함수이다.
hasFinalConsonant(str) {
if (!str) return false;
const lastCharCode = str.charCodeAt(str.length - 1);
if (!util.isHangul(lastCharCode)) return false;
return (lastCharCode - 44032) % 28;
},
requestStr: (str) => {
return hasFinalConsonant(str) ? '을' : '를';
},
'JavaScript&TypeScript' 카테고리의 다른 글
JavaScript - 모바일에서 canvas 화질 저하되는 현상 (0) | 2024.04.26 |
---|---|
JavaScript - 연락처에 하이픈 추가 (0) | 2024.03.28 |
TypeScript - any 타입을 지양하는 이유 (0) | 2023.09.10 |
NestJS - class-validator 데코레이터 (0) | 2023.09.07 |
NestJS - 네이밍 규칙 (0) | 2023.09.03 |