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: 문자열에서 한글만 반환하는 extractHangul을 구현합니다. #130

Merged
merged 9 commits into from
Jun 29, 2024
23 changes: 23 additions & 0 deletions docs/src/pages/docs/api/extractHangul.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: extractHangul
---

# extractHangul

Extracts and returns only Korean characters from the string.

For detailed examples, see below.

```typescript
function extractHangul(str: string): string;
```

## Examples

```tsx
extractHangul('안녕하세요1234abc'); // '안녕하세요'
extractHangul('abcde'); // ''
extractHangul('안녕하세요ㄱㄴ'); // '안녕하세요ㄱㄴ'
extractHangul('안녕하세요 만나서 반갑습니다'); // '안녕하세요 만나서 반갑습니다'
extractHangul('가나다!-29~라마바.,,사'); // '가나다라마바사'
```
23 changes: 23 additions & 0 deletions docs/src/pages/docs/api/extractHangul.ko.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: extractHangul
---

# extractHangul

문자열에서 한글만 추출하여 반환합니다.

자세한 예시는 아래 Example을 참고하세요.

```typescript
function extractHangul(str: string): string;
```

## Examples

```tsx
extractHangul('안녕하세요1234abc'); // '안녕하세요'
extractHangul('abcde'); // ''
extractHangul('안녕하세요ㄱㄴ'); // '안녕하세요ㄱㄴ'
extractHangul('안녕하세요 만나서 반갑습니다'); // '안녕하세요 만나서 반갑습니다'
extractHangul('가나다!-29~라마바.,,사'); // '가나다라마바사'
```
27 changes: 27 additions & 0 deletions src/extractHangul.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { extractHangul } from './extractHangul';

describe('extractHangul', () => {
it('숫자와 알파벳과 특수문자를 제외한 한글 반환', () => {
expect(extractHangul('안녕하세요1234abc!@#')).toBe('안녕하세요');
});

it('한글이 없는 문자열', () => {
expect(extractHangul('1234abc')).toBe('');
});

it('한글과 공백을 제외한 다른 문자는 제거', () => {
expect(extractHangul('한글과 영어가 섞인 문장입니다. Hello!')).toBe('한글과 영어가 섞인 문장입니다 ');
});

it('escape 문자열 유지', () => {
expect(extractHangul('한글과\n\t줄바꿈')).toBe('한글과\n\t줄바꿈');
});

it('모음은 제거하지 않음', () => {
expect(extractHangul('ㅠㅠ')).toBe('ㅠㅠ');
});

it('자음은 제거하지 않음', () => {
expect(extractHangul('ㄱㄴㄱㄴ')).toBe('ㄱㄴㄱㄴ');
});
});
18 changes: 18 additions & 0 deletions src/extractHangul.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @name extractHangul
* @description
* 문자열을 입력받고 한글만 추출해 반환합니다.
*
* @param {string} chars 모든 문자열
*
* @example
* extractHangul('안녕하세요1234abc') // '안녕하세요'
* extractHangul('abcde') // ''
* extractHangul('안녕하세요ㄱㄴ') // '안녕하세요ㄱㄴ'
* extractHangul('안녕하세요 만나서 반갑습니다') // '안녕하세요 만나서 반갑습니다'
* extractHangul('가나다!-29~라마바.,,사') // '가나다라마바사'
*/

export function extractHangul(str: string): string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

실제 사용할 수 있도록 src/index.ts에서 export 해주시면 좋을 것 같아요

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

완료했습니다 ! 감사합니다 !

return str.replace(/[^ㄱ-ㅎㅏ-ㅣ가-힣\s]+/g, '');
}