Skip to content

Commit

Permalink
feat: getChoseng을 utils에서 별도 함수로 분리합니다. (#192)
Browse files Browse the repository at this point in the history
* getChoseong분리

* write test code

* getChoseong import

* remove useless import statemenet

* remove unused file
  • Loading branch information
okinawaa committed Aug 4, 2024
1 parent 9f8dd1b commit a5ff8d2
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 130 deletions.
19 changes: 19 additions & 0 deletions docs/src/pages/docs/api/getChoseong.en.mdx
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('띄어 쓰기') // 'ㄸㅇ ㅆㄱ'
```
20 changes: 20 additions & 0 deletions docs/src/pages/docs/api/getChoseong.ko.mdx
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('띄어 쓰기') // 'ㄸㅇ ㅆㄱ'
```
3 changes: 2 additions & 1 deletion src/choseongIncludes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { disassembleToGroups } from './disassemble';
import { canBeChoseong, getChoseong } from './utils';
import { getChoseong } from './getChoseong';
import { canBeChoseong } from './utils';

export function choseongIncludes(x: string, y: string) {
const trimmedY = y.replace(/\s/g, '');
Expand Down
2 changes: 1 addition & 1 deletion src/chosungIncludes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isOnlyChoseong } from './choseongIncludes';
import { getChoseong } from './utils';
import { getChoseong } from './getChoseong';

/**
* @deprecated choseongIncludes를 사용해 주세요.
Expand Down
20 changes: 20 additions & 0 deletions src/getChoseong.spec.ts
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('ㄸㅇ ㅆㄱ');
});
});
32 changes: 32 additions & 0 deletions src/getChoseong.ts
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'
);
11 changes: 1 addition & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,4 @@ export { removeLastHangulCharacter } from './removeLastHangulCharacter';
export { romanize } from './romanize';
export { standardizePronunciation } from './standardizePronunciation';
export { susa } from './susa';
export {
canBeChosung,
canBeJongsung,
canBeJungsung,
getChosung,
hasBatchim,
hasProperty,
hasSingleBatchim,
hasValueInReadOnlyStringList,
} from './utils';
export { getChoseong } from './getChoseong';
40 changes: 0 additions & 40 deletions src/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import {
canBeChoseong,
canBeJongseong,
canBeJungseong,
getChoseong,
getFirstConsonants,
hasBatchim,
hasProperty,
hasSingleBatchim,
Expand Down Expand Up @@ -86,44 +84,6 @@ describe('hasSingleBatchim', () => {
});
});

describe('getChoseong', () => {
it('"사과" 단어에서 초성 "ㅅㄱ"을 추출한다.', () => {
expect(getChoseong('사과')).toBe('ㅅㄱ');
});
it('"프론트엔드" 단어에서 초성 "ㅍㄹㅌㅇㄷ"을 추출한다.', () => {
expect(getChoseong('프론트엔드')).toBe('ㅍㄹㅌㅇㄷ');
});
it('"ㄴㅈ" 문자에서 초성 "ㄴㅈ"을 추출한다.', () => {
expect(getChoseong('ㄴㅈ')).toBe('ㄴㅈ');
});
it('"리액트" 단어에서 초성 "ㄹㅇㅌ"을 추출한다.', () => {
expect(getChoseong('리액트')).toBe('ㄹㅇㅌ');
});

it('"띄어 쓰기" 문장에서 초성 "ㄸㅇ ㅆㄱ"을 추출한다.', () => {
expect(getChoseong('띄어 쓰기')).toBe('ㄸㅇ ㅆㄱ');
});
});

describe('getFirstConsonants', () => {
it('"사과" 단어에서 초성 "ㅅㄱ"을 추출한다.', () => {
expect(getFirstConsonants('사과')).toBe('ㅅㄱ');
});
it('"프론트엔드" 단어에서 초성 "ㅍㄹㅌㅇㄷ"을 추출한다.', () => {
expect(getFirstConsonants('프론트엔드')).toBe('ㅍㄹㅌㅇㄷ');
});
it('"ㄴㅈ" 문자에서 초성 "ㄴㅈ"을 추출한다.', () => {
expect(getFirstConsonants('ㄴㅈ')).toBe('ㄴㅈ');
});
it('"리액트" 단어에서 초성 "ㄹㅇㅌ"을 추출한다.', () => {
expect(getFirstConsonants('리액트')).toBe('ㄹㅇㅌ');
});

it('"띄어 쓰기" 문장에서 초성 "ㄸㅇ ㅆㄱ"을 추출된다.', () => {
expect(getFirstConsonants('띄어 쓰기')).toBe('ㄸㅇ ㅆㄱ');
});
});

describe('hasValueInReadOnlyStringList', () => {
const testReadonlyList = ['ㄱ', 'ㄴ', 'ㄷ'] as const;

Expand Down
78 changes: 0 additions & 78 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,8 @@ import {
HANGUL_CHARACTERS_BY_FIRST_INDEX,
HANGUL_CHARACTERS_BY_LAST_INDEX,
HANGUL_CHARACTERS_BY_MIDDLE_INDEX,
JASO_HANGUL_NFD,
NUMBER_OF_JONGSEONG,
} from './constants';
import { disassembleToGroups } from './disassemble';

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'
);

/**
* @name hasBatchim
Expand Down Expand Up @@ -81,73 +70,6 @@ export function hasSingleBatchim(str: string) {
return HANGUL_CHARACTERS_BY_LAST_INDEX[batchimCode].length === 1;
}

/**
* @name getChosung
* @deprecated getChoseong을 사용해 주세요.
* @description
* 단어에서 초성을 추출합니다. (예: `사과` -> `'ㅅㄱ'`)
* ```typescript
* getChoseong(
* // 초성을 추출할 단어
* word: string
* ): string
* ```
* @example
* getChoseong('사과') // 'ㅅㄱ'
* getChoseong('리액트') // 'ㄹㅇㅌ'
* getChoseong('띄어 쓰기') // 'ㄸㅇ ㅆㄱ'
*/
export function getChosung(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
}

/**
* @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
}

/**
* @name getFirstConsonants
* @deprecated getChoseong을 사용해 주세요.
* @description
* 단어에서 초성을 추출합니다. (예: `사과` -> `'ㅅㄱ'`)
* ```typescript
* getFirstConsonants(
* // 초성을 추출할 단어
* word: string
* ): string
* ```
* @example
* getFirstConsonants('사과') // 'ㅅㄱ'
* getFirstConsonants('리액트') // 'ㄹㅇㅌ'
* getFirstConsonants('띄어 쓰기') // 'ㄸㅇ ㅆㄱ'
*/
export function getFirstConsonants(word: string) {
return disassembleToGroups(word).reduce((firstConsonants, [consonant]) => {
return `${firstConsonants}${consonant}`;
}, '');
}

/**
* @name canBeChosung
* @deprecated canBeChoseong을 사용해 주세요.
Expand Down

0 comments on commit a5ff8d2

Please sign in to comment.