Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: getChoseng을 utils에서 별도 함수로 분리합니다. #192

Merged
merged 5 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
);
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export { disassemble, disassembleToGroups } from './disassemble';
export { disassembleCompleteCharacter } from './disassembleCompleteCharacter';
export { josa } from './josa';
export { removeLastHangulCharacter } from './removeLastHangulCharacter';
export { getChoseong } from './getChoseong';
export {
canBeChosung,
canBeJongsung,
canBeJungsung,
getChosung,
hasBatchim,
hasProperty,
hasSingleBatchim,
Expand Down
40 changes: 0 additions & 40 deletions src/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {
canBeChoseong,
canBeJongseong,
canBeJungseong,
getChoseong,
getFirstConsonants,
hasBatchim,
hasProperty,
hasSingleBatchim,
Expand Down Expand Up @@ -84,44 +82,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 @@ -4,19 +4,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 @@ -80,73 +69,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