-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: getChoseng을 utils에서 별도 함수로 분리합니다. (#192)
* getChoseong분리 * write test code * getChoseong import * remove useless import statemenet * remove unused file
- Loading branch information
Showing
9 changed files
with
95 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# getChoseong | ||
|
||
|
||
Extracts the Choseong from a Korean word. (Example: 사과 -> 'ㅅㄱ') | ||
|
||
```typescript | ||
function getChoseong( | ||
// Korean string from which to extract the choseong | ||
word: string | ||
): string; | ||
``` | ||
|
||
## Examples | ||
|
||
```tsx | ||
getChoseong('사과') // 'ㅅㄱ' | ||
getChoseong('리액트') // 'ㄹㅇㅌ' | ||
getChoseong('띄어 쓰기') // 'ㄸㅇ ㅆㄱ' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# getChoseong | ||
|
||
단어에서 초성을 추출합니다. (예: `사과` -> `'ㅅㄱ'`) | ||
|
||
자세한 예시는 아래 Example을 참고하세요. | ||
|
||
```typescript | ||
function getChoseong( | ||
// 초성을 추출 할 한글 문자열 | ||
word: string | ||
): string; | ||
``` | ||
|
||
## Examples | ||
|
||
```tsx | ||
getChoseong('사과') // 'ㅅㄱ' | ||
getChoseong('리액트') // 'ㄹㅇㅌ' | ||
getChoseong('띄어 쓰기') // 'ㄸㅇ ㅆㄱ' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { getChoseong } from './getChoseong'; | ||
|
||
describe('getChoseong', () => { | ||
it('"사과" 단어에서 초성 "ㅅㄱ"을 추출한다.', () => { | ||
expect(getChoseong('사과')).toBe('ㅅㄱ'); | ||
}); | ||
it('"프론트엔드" 단어에서 초성 "ㅍㄹㅌㅇㄷ"을 추출한다.', () => { | ||
expect(getChoseong('프론트엔드')).toBe('ㅍㄹㅌㅇㄷ'); | ||
}); | ||
it('"ㄴㅈ" 문자에서 초성 "ㄴㅈ"을 추출한다.', () => { | ||
expect(getChoseong('ㄴㅈ')).toBe('ㄴㅈ'); | ||
}); | ||
it('"리액트" 단어에서 초성 "ㄹㅇㅌ"을 추출한다.', () => { | ||
expect(getChoseong('리액트')).toBe('ㄹㅇㅌ'); | ||
}); | ||
|
||
it('"띄어 쓰기" 문장에서 초성 "ㄸㅇ ㅆㄱ"을 추출한다.', () => { | ||
expect(getChoseong('띄어 쓰기')).toBe('ㄸㅇ ㅆㄱ'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { HANGUL_CHARACTERS_BY_FIRST_INDEX, JASO_HANGUL_NFD } from './constants'; | ||
|
||
/** | ||
* @name getChoseong | ||
* @description | ||
* 단어에서 초성을 추출합니다. (예: `사과` -> `'ㅅㄱ'`) | ||
* ```typescript | ||
* getChoseong( | ||
* // 초성을 추출할 단어 | ||
* word: string | ||
* ): string | ||
* ``` | ||
* @example | ||
* getChoseong('사과') // 'ㅅㄱ' | ||
* getChoseong('리액트') // 'ㄹㅇㅌ' | ||
* getChoseong('띄어 쓰기') // 'ㄸㅇ ㅆㄱ' | ||
*/ | ||
export function getChoseong(word: string) { | ||
return word | ||
.normalize('NFD') | ||
.replace(EXTRACT_CHOSEONG_REGEX, '') // NFD ㄱ-ㅎ, NFC ㄱ-ㅎ 외 문자 삭제 | ||
.replace(CHOOSE_NFD_CHOSEONG_REGEX, $0 => HANGUL_CHARACTERS_BY_FIRST_INDEX[$0.charCodeAt(0) - 0x1100]); // NFD to NFC | ||
} | ||
|
||
const EXTRACT_CHOSEONG_REGEX = new RegExp( | ||
`[^\\u${JASO_HANGUL_NFD.START_CHOSEONG.toString(16)}-\\u${JASO_HANGUL_NFD.END_CHOSEONG.toString(16)}ㄱ-ㅎ\\s]+`, | ||
'ug' | ||
); | ||
const CHOOSE_NFD_CHOSEONG_REGEX = new RegExp( | ||
`[\\u${JASO_HANGUL_NFD.START_CHOSEONG.toString(16)}-\\u${JASO_HANGUL_NFD.END_CHOSEONG.toString(16)}]`, | ||
'g' | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters