Skip to content

Commit

Permalink
fix: 초성을 뜻하는 단어를 통일 (#67)
Browse files Browse the repository at this point in the history
* refactor: initialConsonants로 사용되던 변수명을 chosung으로 변경

* refactor: firstConsonants로 사용되던 변수명을 chosung으로 변경

* feat: getFirstConsonants에 deprecated를추가

* Create quick-ties-shake.md

---------

Co-authored-by: 박찬혁 <[email protected]>
  • Loading branch information
BO-LIKE-CHICKEN and okinawaa committed Apr 25, 2024
1 parent 4cdf58c commit 7c030df
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-ties-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"es-hangul": patch
---

fix: 초성을 뜻하는 단어를 통일
12 changes: 6 additions & 6 deletions src/chosungIncludes.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
14 changes: 7 additions & 7 deletions src/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
canBeChosung,
canBeJongsung,
canBeJungsung,
getFirstConsonants,
getChosung,
hasBatchim,
hasProperty,
hasSingleBatchim,
Expand Down Expand Up @@ -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('ㄸㅇ ㅆㄱ');
});
});

Expand Down
22 changes: 22 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7c030df

Please sign in to comment.