JavaScript&TypeScript
JavaScript - 한글 조사 처리
hjkang
2024. 3. 15. 09:29
한글 조사를 구분하기 위해서는 마지막 문자의 받침 유무를 체크하여 처리하면 된다.
마지막 문자에 받침이 있으면 '-이/-을/-은/-과', 받침이 없으면 '-가/-를/-는/-와'가 된다.
아래는 받침 유무를 체크하는 함수이다.
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) ? '을' : '를';
},