Skip to content

Commit

Permalink
chore: hasbatchim 함수의 조건문 검사 로직을 개선합니다. (#279)
Browse files Browse the repository at this point in the history
* chore: charAt을 사용하여 명시적인 코드를 작성합니다

* chore: AND 연산을 OR 연산으로 변경하여 연산 횟수를 줄입니다.

* chore: if문을 switch문으로 변경하여 연산 횟수를 줄입니다.

* fix: 한글이 아닌 문자나 빈 문자열의 경우에도 false를 반환하도록 수정합니다

* Create few-lies-give.md

---------

Co-authored-by: 박찬혁 <[email protected]>
  • Loading branch information
iamhungry1030 and okinawaa authored Nov 13, 2024
1 parent 075d950 commit 0a60a65
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-lies-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"es-hangul": patch
---

chore: hasbatchim 함수의 조건문 검사 로직을 개선합니다.
23 changes: 13 additions & 10 deletions src/hasBatchim/hasBatchim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,24 @@ export function hasBatchim(
return false;
}
const charCode = lastChar.charCodeAt(0);
const isCompleteHangul = COMPLETE_HANGUL_START_CHARCODE <= charCode && charCode <= COMPLETE_HANGUL_END_CHARCODE;
const isNotCompleteHangul = charCode < COMPLETE_HANGUL_START_CHARCODE || charCode > COMPLETE_HANGUL_END_CHARCODE;

if (!isCompleteHangul) {
if (isNotCompleteHangul) {
return false;
}

const batchimCode = (charCode - COMPLETE_HANGUL_START_CHARCODE) % NUMBER_OF_JONGSEONG;
const batchimLength = JONGSEONGS[batchimCode].length;

if (options?.only === 'single') {
return JONGSEONGS[batchimCode].length === 1;
switch (options?.only) {
case 'single': {
return batchimLength === 1;
}
case 'double': {
return batchimLength === 2;
}
default: {
return batchimCode > 0;
}
}

if (options?.only === 'double') {
return JONGSEONGS[batchimCode].length === 2;
}

return batchimCode > 0;
}

0 comments on commit 0a60a65

Please sign in to comment.