Skip to content

Commit

Permalink
fix: Handle spaces in initialConsonants search inputs properly (#42)
Browse files Browse the repository at this point in the history
* fix: Handle spaces in initialConsonants search inputs properly

* test: Add test cases for initialConsonants search with space

* fix: remove unused function

* refactor: 공백을 제거하는 역할을 chosungIncludes에 맡기도록 개선

* test: 초성자리에 공백만 들어갈 경우에 대한 테스트 케이스 추가

* fix: 초성 자리에 공백만 들어간 예외사항 처리

* refactor: trim된 문자열을 trimmedStr로 변경

Co-authored-by: 박찬혁 <[email protected]>

* fix: 누락된 trimmedY의 변수명 변경 반영

* Create kind-pianos-thank.md

---------

Co-authored-by: 박찬혁 <[email protected]>
  • Loading branch information
BO-LIKE-CHICKEN and okinawaa committed Apr 19, 2024
1 parent 8a68b73 commit f668e15
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-pianos-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"es-hangul": patch
---

fix: Handle spaces in initialConsonants search inputs properly
12 changes: 12 additions & 0 deletions src/chosungIncludes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ describe('chosungIncludes', () => {
expect(chosungIncludes('프론트엔드', 'ㅍㅌ')).toBe(false);
});

it('should return true when "ㅍㄹㅌㅇㄷㄱㅂㅈ" is entered for searching "프론트엔드 개발자"', () => {
expect(chosungIncludes('프론트엔드 개발자', 'ㅍㄹㅌㅇㄷㄱㅂㅈ')).toBe(true);
});

it('should return true when "ㅍㄹㅌㅇㄷ ㄱㅂㅈ" is entered for searching "프론트엔드 개발자"', () => {
expect(chosungIncludes('프론트엔드 개발자', 'ㅍㄹㅌㅇㄷ ㄱㅂㅈ')).toBe(true);
});

it('should return true when "ㅍㄹㅌㅇㄷ ㄱㅂㅈ" is entered for searching " "', () => {
expect(chosungIncludes('프론트엔드 개발자', ' ')).toBe(false);
});

it('should return false when "푸롴트" is entered for searching "프론트엔드" as it does not only include the initial consonants.', () => {
expect(chosungIncludes('프론트엔드', '푸롴트')).toBe(false);
});
Expand Down
18 changes: 12 additions & 6 deletions src/chosungIncludes.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import { HANGUL_CHARACTERS_BY_FIRST_INDEX } from './constants';
import { disassembleHangulToGroups } from './disassemble';
import { canBeChosung, getFirstConsonants, hasValueInReadOnlyStringList } from './utils';
import { canBeChosung, getFirstConsonants } from './utils';

export function chosungIncludes(x: string, y: string) {
if (!isOnlyInitialConsonant(y)) {
const trimmedY = y.replace(/\s/g, '');

if (!isOnlyInitialConsonant(trimmedY)) {
return false;
}

const initialConsonantsX = getFirstConsonants(x).replace(/\s/g, '');
const initialConsonantsY = getFirstConsonants(y).replace(/\s/g, '');
const initialConsonantsY = trimmedY;

return initialConsonantsX.includes(initialConsonantsY);
}

/*
* @description 한글초성으로만 주어진 경우
* @description 문자열이 한글초성으로만 주어진 경우
*/
function isOnlyInitialConsonant(str: string) {
return disassembleHangulToGroups(str).every(disassembled => {
const groups = disassembleHangulToGroups(str);
if (groups.length === 0) {
return false;
}

return groups.every(disassembled => {
return disassembled.length === 1 && canBeChosung(disassembled[0]);
});
}

0 comments on commit f668e15

Please sign in to comment.