From f668e15318744e2c0195a826ef0d963de26072cb Mon Sep 17 00:00:00 2001 From: BOSUNG BAEK <78058734+BO-LIKE-CHICKEN@users.noreply.github.com> Date: Fri, 19 Apr 2024 12:25:46 +0900 Subject: [PATCH] fix: Handle spaces in initialConsonants search inputs properly (#42) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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: 박찬혁 * fix: 누락된 trimmedY의 변수명 변경 반영 * Create kind-pianos-thank.md --------- Co-authored-by: 박찬혁 --- .changeset/kind-pianos-thank.md | 5 +++++ src/chosungIncludes.spec.ts | 12 ++++++++++++ src/chosungIncludes.ts | 18 ++++++++++++------ 3 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 .changeset/kind-pianos-thank.md diff --git a/.changeset/kind-pianos-thank.md b/.changeset/kind-pianos-thank.md new file mode 100644 index 00000000..5e7800fa --- /dev/null +++ b/.changeset/kind-pianos-thank.md @@ -0,0 +1,5 @@ +--- +"es-hangul": patch +--- + +fix: Handle spaces in initialConsonants search inputs properly diff --git a/src/chosungIncludes.spec.ts b/src/chosungIncludes.spec.ts index 4a6ca9b1..0b8950ce 100644 --- a/src/chosungIncludes.spec.ts +++ b/src/chosungIncludes.spec.ts @@ -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); }); diff --git a/src/chosungIncludes.ts b/src/chosungIncludes.ts index cd9dccea..d085b87c 100644 --- a/src/chosungIncludes.ts +++ b/src/chosungIncludes.ts @@ -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]); }); }