From 7c030dfb7f8c050b082b3ab3b4f760c39fbe4413 Mon Sep 17 00:00:00 2001 From: BOSUNG BAEK <78058734+BO-LIKE-CHICKEN@users.noreply.github.com> Date: Thu, 25 Apr 2024 19:03:57 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=B4=88=EC=84=B1=EC=9D=84=20=EB=9C=BB?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EB=8B=A8=EC=96=B4=EB=A5=BC=20=ED=86=B5?= =?UTF-8?q?=EC=9D=BC=20(#67)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: initialConsonants로 사용되던 변수명을 chosung으로 변경 * refactor: firstConsonants로 사용되던 변수명을 chosung으로 변경 * feat: getFirstConsonants에 deprecated를추가 * Create quick-ties-shake.md --------- Co-authored-by: 박찬혁 --- .changeset/quick-ties-shake.md | 5 +++++ src/chosungIncludes.ts | 12 ++++++------ src/utils.spec.ts | 14 +++++++------- src/utils.ts | 22 ++++++++++++++++++++++ 4 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 .changeset/quick-ties-shake.md diff --git a/.changeset/quick-ties-shake.md b/.changeset/quick-ties-shake.md new file mode 100644 index 00000000..b563baa7 --- /dev/null +++ b/.changeset/quick-ties-shake.md @@ -0,0 +1,5 @@ +--- +"es-hangul": patch +--- + +fix: 초성을 뜻하는 단어를 통일 diff --git a/src/chosungIncludes.ts b/src/chosungIncludes.ts index d085b87c..254d1f30 100644 --- a/src/chosungIncludes.ts +++ b/src/chosungIncludes.ts @@ -1,23 +1,23 @@ import { disassembleHangulToGroups } from './disassemble'; -import { canBeChosung, getFirstConsonants } from './utils'; +import { canBeChosung, getChosung } from './utils'; export function chosungIncludes(x: string, y: string) { const trimmedY = y.replace(/\s/g, ''); - if (!isOnlyInitialConsonant(trimmedY)) { + if (!isOnlyChosung(trimmedY)) { return false; } - const initialConsonantsX = getFirstConsonants(x).replace(/\s/g, ''); - const initialConsonantsY = trimmedY; + const chosungX = getChosung(x).replace(/\s/g, ''); + const chosungY = trimmedY; - return initialConsonantsX.includes(initialConsonantsY); + return chosungX.includes(chosungY); } /* * @description 문자열이 한글초성으로만 주어진 경우 */ -function isOnlyInitialConsonant(str: string) { +function isOnlyChosung(str: string) { const groups = disassembleHangulToGroups(str); if (groups.length === 0) { return false; diff --git a/src/utils.spec.ts b/src/utils.spec.ts index 1e5f7dca..04d281f2 100644 --- a/src/utils.spec.ts +++ b/src/utils.spec.ts @@ -3,7 +3,7 @@ import { canBeChosung, canBeJongsung, canBeJungsung, - getFirstConsonants, + getChosung, hasBatchim, hasProperty, hasSingleBatchim, @@ -46,22 +46,22 @@ describe('hasSingleBatchim', () => { }); }); -describe('getFirstConsonants', () => { +describe('getChosung', () => { it('should extract the initial consonants "ㅅㄱ" from the word "사과"', () => { - expect(getFirstConsonants('사과')).toBe('ㅅㄱ'); + expect(getChosung('사과')).toBe('ㅅㄱ'); }); it('should extract the initial consonants "ㅍㄹㅌㅇㄷ" from the word "프론트엔드"', () => { - expect(getFirstConsonants('프론트엔드')).toBe('ㅍㄹㅌㅇㄷ'); + expect(getChosung('프론트엔드')).toBe('ㅍㄹㅌㅇㄷ'); }); it('should extract the initial consonants "ㄴㅈ" from the consonants "ㄴㅈ"', () => { - expect(getFirstConsonants('ㄴㅈ')).toBe('ㄴㅈ'); + expect(getChosung('ㄴㅈ')).toBe('ㄴㅈ'); }); it('should extract the initial consonants "ㄹㅇㅌ" from the word "리액트"', () => { - expect(getFirstConsonants('리액트')).toBe('ㄹㅇㅌ'); + expect(getChosung('리액트')).toBe('ㄹㅇㅌ'); }); it('should extract the initial consonants "ㄸㅇ ㅆㄱ" from the phrase "띄어 쓰기"', () => { - expect(getFirstConsonants('띄어 쓰기')).toBe('ㄸㅇ ㅆㄱ'); + expect(getChosung('띄어 쓰기')).toBe('ㄸㅇ ㅆㄱ'); }); }); diff --git a/src/utils.ts b/src/utils.ts index ddab75d7..0c1b15a6 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -51,8 +51,30 @@ export function hasSingleBatchim(str: string) { return disassembled.length === 3; } +/** + * @name getChosung + * @description + * 단어에서 초성을 추출합니다. (예: `사과` -> `'ㅅㄱ'`) + * ```typescript + * getChosung( + * // 초성을 추출할 단어 + * word: string + * ): string + * ``` + * @example + * getChosung('사과') // 'ㅅㄱ' + * getChosung('리액트') // 'ㄹㅇㅌ' + * getChosung('띄어 쓰기') // 'ㄸㅇ ㅆㄱ' + */ +export function getChosung(word: string) { + return disassembleHangulToGroups(word).reduce((chosung, [consonant]) => { + return `${chosung}${consonant}`; + }, ''); +} + /** * @name getFirstConsonants + * @deprecated getChosung을 사용해 주세요. * @description * 단어에서 초성을 추출합니다. (예: `사과` -> `'ㅅㄱ'`) * ```typescript