Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/feat-jung-jong-keep-non-hangul.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"es-hangul": patch
---

feat: getJungseong, getJongseong에 keepNonHangul 옵션을 추가합니다. true일 때 숫자·기호 등 비한글 문자를 유지합니다.
5 changes: 5 additions & 0 deletions .changeset/fix-assemble-empty-array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"es-hangul": patch
---

fix(assemble): 빈 배열을 인자로 받을 때 TypeError가 발생하던 문제를 수정합니다. 이제 `assemble([])`은 빈 문자열을 반환합니다.
7 changes: 3 additions & 4 deletions docs/src/pages/docs/api/core/getJongseong.en.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Extracts the Jongseong (final consonants) from a Korean word. (Example: 별 -> '

```typescript
function getJongseong(
// Korean string from which to extract the jongseong
word: string
word: string,
options?: { keepNonHangul?: boolean }
): string;
```

Expand All @@ -22,6 +22,7 @@ getJongseong('별'); // 'ㄹ'
getJongseong('하늘'); // 'ㄹ'
getJongseong('띄어 쓰기'); // ' '
getJongseong('파이팅'); // 'ㅇ'
getJongseong('값123', { keepNonHangul: true }); // 'ㅄ123'
```

## Demo
Expand All @@ -37,5 +38,3 @@ console.log(getJongseong('별'));
```

</Sandpack>


7 changes: 3 additions & 4 deletions docs/src/pages/docs/api/core/getJongseong.ko.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { Sandpack } from '@/components/Sandpack';

```typescript
function getJongseong(
// 종성을 추출할 한글 문자열
word: string
word: string,
options?: { keepNonHangul?: boolean }
): string;
```

Expand All @@ -24,6 +24,7 @@ getJongseong('별'); // 'ㄹ'
getJongseong('하늘'); // 'ㄹ'
getJongseong('띄어 쓰기'); // ' '
getJongseong('파이팅'); // 'ㅇ'
getJongseong('값123', { keepNonHangul: true }); // 'ㅄ123'
```

## 사용해보기
Expand All @@ -39,5 +40,3 @@ console.log(getJongseong('별'));
```

</Sandpack>


6 changes: 3 additions & 3 deletions docs/src/pages/docs/api/core/getJungseong.en.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Extracts the Jungseong (vowels) from a Korean word. (Example: 사과 -> 'ㅏㅘ'

```typescript
function getJungseong(
// Korean string from which to extract the jungseong
word: string
word: string,
options?: { keepNonHangul?: boolean }
): string;
```

Expand All @@ -21,6 +21,7 @@ function getJungseong(
getJungseong('사과'); // 'ㅏㅘ'
getJungseong('하늘'); // 'ㅏㅡ'
getJungseong('띄어 쓰기'); // 'ㅢㅓ ㅡㅣ'
getJungseong('네이버123', { keepNonHangul: true }); // 'ㅔㅣㅓ123'
```

## Demo
Expand All @@ -36,4 +37,3 @@ console.log(getJungseong('사과'));
```

</Sandpack>

7 changes: 3 additions & 4 deletions docs/src/pages/docs/api/core/getJungseong.ko.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { Sandpack } from '@/components/Sandpack';

```typescript
function getJungseong(
// 중성을 추출할 한글 문자열
word: string
word: string,
options?: { keepNonHangul?: boolean }
): string;
```

Expand All @@ -23,6 +23,7 @@ function getJungseong(
getJungseong('사과'); // 'ㅏㅘ'
getJungseong('하늘'); // 'ㅏㅡ'
getJungseong('띄어 쓰기'); // 'ㅢㅓ ㅡㅣ'
getJungseong('네이버123', { keepNonHangul: true }); // 'ㅔㅣㅓ123'
```

## 사용해보기
Expand All @@ -38,5 +39,3 @@ console.log(getJungseong('사과'));
```

</Sandpack>


4 changes: 4 additions & 0 deletions src/core/assemble/assemble.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ describe('assemble', () => {
expect(assemble(['ㄱ', 'ㅏ', '!', 'ㄴ', 'ㅏ'])).toEqual('가!나');
expect(assemble(['ㅇ', 'ㅑ', '1', 'ㅎ', 'ㅏ'])).toEqual('야1하');
});

it('빈 배열은 빈 문자열을 반환한다', () => {
expect(assemble([])).toEqual('');
});
});
2 changes: 1 addition & 1 deletion src/core/assemble/assemble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ import { binaryAssemble } from '@/_internal/hangul';
*/
export function assemble(fragments: string[]) {
const disassembled = disassemble(fragments.join('')).split('');
return disassembled.reduce(binaryAssemble);
return disassembled.reduce(binaryAssemble, '');
}
8 changes: 8 additions & 0 deletions src/core/getJongseong/getJongseong.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ describe('getJongseong', () => {
it('"파이팅" 단어에서 종성 "ㅇ"을 추출한다.', () => {
expect(getJongseong('파이팅')).toBe('ㅇ');
});

it('keepNonHangul이 false이면 숫자 등 비한글을 제거한다.', () => {
expect(getJongseong('값123')).toBe('ㅄ');
});

it('keepNonHangul이 true이면 숫자 등 비한글을 유지한다.', () => {
expect(getJongseong('값123', { keepNonHangul: true })).toBe('ㅄ123');
});
});


31 changes: 21 additions & 10 deletions src/core/getJongseong/getJongseong.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,30 @@ import { JASO_HANGUL_NFD } from '../getChoseong/constants';
* ```typescript
* getJongseong(
* // 종성을 추출할 단어
* word: string
* word: string,
* // 옵션 객체로 keepNonHangul 속성을 받을 수 있습니다.
* options?: { keepNonHangul?: boolean }
* ): string
* ```
* @example
* getJongseong('값') // 'ㅄ'
* getJongseong('사과') // ''
* getJongseong('값123', { keepNonHangul: true }) // 'ㅄ123'
*/
export function getJongseong(word: string) {
export function getJongseong(word: string, options: { keepNonHangul?: boolean } = {}) {
const { keepNonHangul = false } = options;

if (keepNonHangul) {
return word
.normalize('NFD')
.replace(REMOVE_NFD_CHO_JUNG_REGEX, '')
.replace(CHOOSE_NFD_JONGSEONG_REGEX, $0 => JONGSEONGS_COMPOSITE[$0.charCodeAt(0) - 0x11a8]);
}

return word
.normalize('NFD')
.replace(EXTRACT_JONGSEONG_REGEX, '')
.replace(
CHOOSE_NFD_JONGSEONG_REGEX,
$0 => JONGSEONGS_COMPOSITE[$0.charCodeAt(0) - 0x11a8]
);
.replace(CHOOSE_NFD_JONGSEONG_REGEX, $0 => JONGSEONGS_COMPOSITE[$0.charCodeAt(0) - 0x11a8]);
}

const EXTRACT_JONGSEONG_REGEX = new RegExp(
Expand All @@ -35,9 +44,11 @@ const CHOOSE_NFD_JONGSEONG_REGEX = new RegExp(
'g'
);

const JONGSEONGS_COMPOSITE = JONGSEONGS.slice(1).map(
d => Object.fromEntries(
Object.entries(DISASSEMBLED_CONSONANTS_BY_CONSONANT).map(([key, val]) => [val, key])
)[d]
const REMOVE_NFD_CHO_JUNG_REGEX = new RegExp(
`[\\u${JASO_HANGUL_NFD.START_CHOSEONG.toString(16)}-\\u${JASO_HANGUL_NFD.END_CHOSEONG.toString(16)}\\u${JASO_HANGUL_NFD.START_JUNGSEONG.toString(16)}-\\u${JASO_HANGUL_NFD.END_JUNGSEONG.toString(16)}]+`,
'ug'
);

const JONGSEONGS_COMPOSITE = JONGSEONGS.slice(1).map(
d => Object.fromEntries(Object.entries(DISASSEMBLED_CONSONANTS_BY_CONSONANT).map(([key, val]) => [val, key]))[d]
);
8 changes: 8 additions & 0 deletions src/core/getJungseong/getJungseong.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ describe('getJungseong', () => {
it('"띄어 쓰기" 문장에서 중성 "ㅢㅓ ㅡㅣ"을 추출한다.', () => {
expect(getJungseong('띄어 쓰기')).toBe('ㅢㅓ ㅡㅣ');
});

it('keepNonHangul이 false이면 숫자 등 비한글을 제거한다.', () => {
expect(getJungseong('네이버123')).toBe('ㅔㅣㅓ');
});

it('keepNonHangul이 true이면 숫자 등 비한글을 유지한다.', () => {
expect(getJungseong('네이버123', { keepNonHangul: true })).toBe('ㅔㅣㅓ123');
});
});


24 changes: 19 additions & 5 deletions src/core/getJungseong/getJungseong.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,29 @@ import { JASO_HANGUL_NFD } from '../getChoseong/constants';
* ```typescript
* getJungseong(
* // 중성을 추출할 단어
* word: string
* word: string,
* // 옵션 객체로 keepNonHangul 속성을 받을 수 있습니다.
* options?: { keepNonHangul?: boolean }
* ): string
* ```
* @example
* getJungseong('사과') // 'ㅏㅘ'
* getJungseong('띄어 쓰기') // 'ㅢㅓ ㅡㅣ'
* getJungseong('네이버123', { keepNonHangul: true }) // 'ㅔㅣㅓ123'
*/
export function getJungseong(word: string) {
export function getJungseong(word: string, options: { keepNonHangul?: boolean } = {}) {
const { keepNonHangul = false } = options;

if (keepNonHangul) {
return word
.normalize('NFD')
.replace(REMOVE_NFD_CHO_JONG_REGEX, '')
.replace(CHOOSE_NFD_JUNGSEONG_REGEX, $0 => JUNGSEONGS_COMPOSITE[$0.charCodeAt(0) - 0x1161]);
}

return word
.normalize('NFD')
.replace(EXTRACT_JUNGSEONG_REGEX, '')
.replace(EXTRACT_JUNGSEONG_REGEX, '')
.replace(CHOOSE_NFD_JUNGSEONG_REGEX, $0 => JUNGSEONGS_COMPOSITE[$0.charCodeAt(0) - 0x1161]); // NFD -> 합성 중성 문자
}

Expand All @@ -30,7 +42,9 @@ const CHOOSE_NFD_JUNGSEONG_REGEX = new RegExp(
`[\\u${JASO_HANGUL_NFD.START_JUNGSEONG.toString(16)}-\\u${JASO_HANGUL_NFD.END_JUNGSEONG.toString(16)}]`,
'g'
);
const REMOVE_NFD_CHO_JONG_REGEX = new RegExp(
`[\\u${JASO_HANGUL_NFD.START_CHOSEONG.toString(16)}-\\u${JASO_HANGUL_NFD.END_CHOSEONG.toString(16)}\\u${JASO_HANGUL_NFD.START_JONGSEONG.toString(16)}-\\u${JASO_HANGUL_NFD.END_JONGSEONG.toString(16)}]+`,
'ug'
);

const JUNGSEONGS_COMPOSITE = Object.keys(DISASSEMBLED_VOWELS_BY_VOWEL);