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

fix: 일관된 한글 이름 규칙 설정 (to v2) #204

Merged
merged 17 commits into from
Aug 5, 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
5 changes: 5 additions & 0 deletions .changeset/kind-birds-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"es-hangul": major
---

fix: 일관된 한글 이름 규칙 설정 함수명에서 꼭 필요하지 않다면 hangul이라는 워딩을 제거합니다
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ import hangul from 'es-hangul' // hangul default export에 묶어서도 제공
hangul.getSimilarity(...)
hangul.disassemble(...)
hangul.josa(...)

```
8 changes: 4 additions & 4 deletions docs/src/pages/docs/api/getChoseong.en.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ Extracts the Choseong from a Korean word. (Example: 사과 -> 'ㅅㄱ')

```typescript
function getChoseong(
// Korean string from which to extract the choseong
// Korean string from which to extract the choseong
word: string
): string;
```

## Examples

```tsx
getChoseong('사과') // 'ㅅㄱ'
getChoseong('리액트') // 'ㄹㅇㅌ'
getChoseong('띄어 쓰기') // 'ㄸㅇ ㅆㄱ'
getChoseong('사과'); // 'ㅅㄱ'
getChoseong('리액트'); // 'ㄹㅇㅌ'
getChoseong('띄어 쓰기'); // 'ㄸㅇ ㅆㄱ'
```
6 changes: 3 additions & 3 deletions docs/src/pages/docs/api/getChoseong.ko.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function getChoseong(
## Examples

```tsx
getChoseong('사과') // 'ㅅㄱ'
getChoseong('리액트') // 'ㄹㅇㅌ'
getChoseong('띄어 쓰기') // 'ㄸㅇ ㅆㄱ'
getChoseong('사과'); // 'ㅅㄱ'
getChoseong('리액트'); // 'ㄹㅇㅌ'
getChoseong('띄어 쓰기'); // 'ㄸㅇ ㅆㄱ'
```
11 changes: 6 additions & 5 deletions docs/src/pages/docs/api/hasBatchim.en.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ title: hasBatchim
---

# hasBatchim

Checks if the last character of a Korean string has a batchim (jongseong).

```typescript
````typescript
hasBatchim(
str: string,
// checking for single batchim
str: string,
// checking for single batchim
options?: { single?: boolean }
): boolean


```typescript
hasBatchim('값') // true
hasBatchim('토') // false
hasBatchim('갑', { single: true }) // true
hasBatchim('값', { single: true }) // false
hasBatchim('토', { single: true }) // false
````
````
9 changes: 4 additions & 5 deletions docs/src/pages/docs/api/hasBatchim.ko.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ title: hasBatchim

한글 문자열의 마지막 글자가 받침이 있는지 확인합니다.


```typescript
````typescript
hasBatchim(
str: string,
str: string,
// 홑받침 여부를 확인할지 여부
options?: { single?: boolean }
): boolean


```typescript
hasBatchim('값') // true
hasBatchim('토') // false
hasBatchim('갑', { single: true }) // true
hasBatchim('값', { single: true }) // false
hasBatchim('토', { single: true }) // false
````
````
60 changes: 30 additions & 30 deletions src/_internal/hangul.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
binaryAssembleHangulCharacters,
binaryAssembleHangul,
binaryAssembleCharacters,
binaryAssemble,
isHangulAlphabet,
isHangulCharacter,
isHangul,
Expand Down Expand Up @@ -72,102 +72,102 @@ describe('parse', () => {
});
});

describe('binaryAssembleHangulCharacters', () => {
describe('binaryAssembleCharacters', () => {
it('초성과 중성만 조합', () => {
expect(binaryAssembleHangulCharacters('ㄱ', 'ㅏ')).toEqual('가');
expect(binaryAssembleCharacters('ㄱ', 'ㅏ')).toEqual('가');
});

it('초성과 중성이 합쳐진 문자와 종성을 조합', () => {
expect(binaryAssembleHangulCharacters('가', 'ㅇ')).toEqual('강');
expect(binaryAssembleCharacters('가', 'ㅇ')).toEqual('강');
});

it('초성과 중성과 종성이 합쳐진 문자와 자음을 조합하여 겹받침 만들기', () => {
expect(binaryAssembleHangulCharacters('갑', 'ㅅ')).toEqual('값');
expect(binaryAssembleCharacters('갑', 'ㅅ')).toEqual('값');
});

it('초성과 중성이 합쳐진 문자와 모음을 조립하여 겹모음 만들기', () => {
expect(binaryAssembleHangulCharacters('고', 'ㅏ')).toEqual('과');
expect(binaryAssembleCharacters('고', 'ㅏ')).toEqual('과');
});

it('초성과 중성(겹모음)이 합쳐진 문자와 자음을 조합', () => {
expect(binaryAssembleHangulCharacters('과', 'ㄱ')).toEqual('곽');
expect(binaryAssembleCharacters('과', 'ㄱ')).toEqual('곽');
});

it('초성과 중성(겹모음)과 종성이 합쳐진 문자와 자음을 조합하여 겹받침 만들기', () => {
expect(binaryAssembleHangulCharacters('완', 'ㅈ')).toEqual('왅');
expect(binaryAssembleCharacters('완', 'ㅈ')).toEqual('왅');
});

it('모음만 있는 문자와 모음을 조합하여 겹모음 만들기', () => {
expect(binaryAssembleHangulCharacters('ㅗ', 'ㅏ')).toEqual('ㅘ');
expect(binaryAssembleCharacters('ㅗ', 'ㅏ')).toEqual('ㅘ');
});

it('초성과 중성과 종성이 합쳐진 문자의 연음 법칙', () => {
expect(binaryAssembleHangulCharacters('톳', 'ㅡ')).toEqual('토스');
expect(binaryAssembleCharacters('톳', 'ㅡ')).toEqual('토스');
});

it('초성과 종성(겹모음)과 종성이 합쳐진 문자의 연음 법칙', () => {
expect(binaryAssembleHangulCharacters('왅', 'ㅓ')).toEqual('완저');
expect(binaryAssembleCharacters('왅', 'ㅓ')).toEqual('완저');
});

it('초성과 중성과 종성(겹받침)이 합쳐진 문자의 연음 법칙', () => {
expect(binaryAssembleHangulCharacters('닭', 'ㅏ')).toEqual('달가');
expect(binaryAssembleHangulCharacters('깎', 'ㅏ')).toEqual('까까');
expect(binaryAssembleCharacters('닭', 'ㅏ')).toEqual('달가');
expect(binaryAssembleCharacters('깎', 'ㅏ')).toEqual('까까');
});

it('문법에 맞지 않는 문자를 조합하면 단순 Join 한다. (문법 순서 틀림)', () => {
expect(binaryAssembleHangulCharacters('ㅏ', 'ㄱ')).toEqual('ㅏㄱ');
expect(binaryAssembleHangulCharacters('까', 'ㅃ')).toEqual('까ㅃ');
expect(binaryAssembleHangulCharacters('ㅘ', 'ㅏ')).toEqual('ㅘㅏ');
expect(binaryAssembleCharacters('ㅏ', 'ㄱ')).toEqual('ㅏㄱ');
expect(binaryAssembleCharacters('까', 'ㅃ')).toEqual('까ㅃ');
expect(binaryAssembleCharacters('ㅘ', 'ㅏ')).toEqual('ㅘㅏ');
});

it('순서대로 입력했을 때 조합이 불가능한 문자라면 단순 Join 한다.', () => {
expect(binaryAssembleHangulCharacters('뼈', 'ㅣ')).toEqual('뼈ㅣ');
expect(binaryAssembleCharacters('뼈', 'ㅣ')).toEqual('뼈ㅣ');
});

it('소스가 두 글자 이상이라면 Invalid source 에러를 발생시킨다.', () => {
expect(() => binaryAssembleHangulCharacters('가나', 'ㄴ')).toThrowError(
expect(() => binaryAssembleCharacters('가나', 'ㄴ')).toThrowError(
'Invalid source character: 가나. Source must be one character.'
);
expect(() => binaryAssembleHangulCharacters('ㄱㄴ', 'ㅏ')).toThrowError(
expect(() => binaryAssembleCharacters('ㄱㄴ', 'ㅏ')).toThrowError(
'Invalid source character: ㄱㄴ. Source must be one character.'
);
});

it('다음 문자가 한글 문자 한 글자가 아니라면 Invalid next character 에러를 발생시킨다.', () => {
assert.throws(
() => binaryAssembleHangulCharacters('ㄱ', 'a'),
() => binaryAssembleCharacters('ㄱ', 'a'),
Error,
'Invalid next character: a. Next character must be one of the choseong, jungseong, or jongseong.'
);
assert.throws(
() => binaryAssembleHangulCharacters('ㄱ', 'ㅡㅏ'),
() => binaryAssembleCharacters('ㄱ', 'ㅡㅏ'),
Error,
'Invalid next character: ㅡㅏ. Next character must be one of the choseong, jungseong, or jongseong.'
);
});
});

describe('binaryAssembleHangul', () => {
describe('binaryAssemble', () => {
it('문장과 모음을 조합하여 다음 글자를 생성한다.', () => {
expect(binaryAssembleHangul('저는 고양이를 좋아합닏', 'ㅏ')).toEqual('저는 고양이를 좋아합니다');
expect(binaryAssemble('저는 고양이를 좋아합닏', 'ㅏ')).toEqual('저는 고양이를 좋아합니다');
});

it('문장과 자음을 조합하여 홑받침을 생성한다.', () => {
expect(binaryAssembleHangul('저는 고양이를 좋아하', 'ㅂ')).toEqual('저는 고양이를 좋아합');
expect(binaryAssemble('저는 고양이를 좋아하', 'ㅂ')).toEqual('저는 고양이를 좋아합');
});

it('문장과 자음을 조합하여 겹받침을 생성한다.', () => {
expect(binaryAssembleHangul('저는 고양이를 좋아합', 'ㅅ')).toEqual('저는 고양이를 좋아핪');
expect(binaryAssemble('저는 고양이를 좋아합', 'ㅅ')).toEqual('저는 고양이를 좋아핪');
});

it('조합이 불가능한 자음이 입력되면 단순 Join 한다.', () => {
expect(binaryAssembleHangul('저는 고양이를 좋아합', 'ㄲ')).toEqual('저는 고양이를 좋아합ㄲ');
expect(binaryAssembleHangul('저는 고양이를 좋아합', 'ㅂ')).toEqual('저는 고양이를 좋아합ㅂ');
expect(binaryAssemble('저는 고양이를 좋아합', 'ㄲ')).toEqual('저는 고양이를 좋아합ㄲ');
expect(binaryAssemble('저는 고양이를 좋아합', 'ㅂ')).toEqual('저는 고양이를 좋아합ㅂ');
});

it('조합이 불가능한 모음이 입력되면 단순 Join 한다.', () => {
expect(binaryAssembleHangul('저는 고양이를 좋아하', 'ㅏ')).toEqual('저는 고양이를 좋아하ㅏ');
expect(binaryAssembleHangul('저는 고양이를 좋아합니다', 'ㅜ')).toEqual('저는 고양이를 좋아합니다ㅜ');
expect(binaryAssemble('저는 고양이를 좋아하', 'ㅏ')).toEqual('저는 고양이를 좋아하ㅏ');
expect(binaryAssemble('저는 고양이를 좋아합니다', 'ㅜ')).toEqual('저는 고양이를 좋아합니다ㅜ');
});

describe('assertHangul', () => {
Expand Down
50 changes: 24 additions & 26 deletions src/_internal/hangul.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import assert, { excludeLastElement, isBlank, joinString } from '.';
import { canBeChoseong, canBeJungseong, canBeJongseong } from '../canBe';
import { combineHangulCharacter, combineVowels, curriedCombineHangulCharacter } from '../combineHangulCharacter';
import { combineCharacter, combineVowels, curriedCombineCharacter } from '../combineCharacter';
import { disassembleToGroups } from '../disassemble';
import { hasBatchim } from '../hasBatchim';
import { removeLastHangulCharacter } from '../removeLastHangulCharacter';
import { removeLastCharacter } from '../removeLastCharacter';

export function isHangulCharacter(character: string) {
return /^[가-힣]$/.test(character);
Expand Down Expand Up @@ -48,23 +48,23 @@ export function safeParseHangul(actual: unknown): SafeParseSuccess | SafeParseEr
}

/**
* @name binaryAssembleHangulAlphabets
* @name binaryAssembleAlphabets
* @description
* 두 개의 한글 자모를 합칩니다. 완성된 한글 문자는 취급하지 않습니다.
* @example
* ```
* binaryAssembleHangulAlphabets('ㄱ', 'ㅏ') // 가
* binaryAssembleHangulAlphabets('ㅗ', 'ㅏ') // ㅘ
* binaryAssembleAlphabets('ㄱ', 'ㅏ') // 가
* binaryAssembleAlphabets('ㅗ', 'ㅏ') // ㅘ
* ```
*/
export function binaryAssembleHangulAlphabets(source: string, nextCharacter: string) {
export function binaryAssembleAlphabets(source: string, nextCharacter: string) {
if (canBeJungseong(`${source}${nextCharacter}`)) {
return combineVowels(source, nextCharacter);
}

const isConsonantSource = canBeJungseong(source) === false;
if (isConsonantSource && canBeJungseong(nextCharacter)) {
return combineHangulCharacter(source, nextCharacter);
return combineCharacter(source, nextCharacter);
}

return joinString(source, nextCharacter);
Expand All @@ -79,28 +79,28 @@ export function linkHangulCharacters(source: string, nextCharacter: string) {
const sourceJamo = disassembleToGroups(source)[0];
const [, lastJamo] = excludeLastElement(sourceJamo);

return joinString(removeLastHangulCharacter(source), combineHangulCharacter(lastJamo, nextCharacter));
return joinString(removeLastCharacter(source), combineCharacter(lastJamo, nextCharacter));
}

/**
* @name binaryAssembleHangulCharacters
* @name binaryAssembleCharacters
* @description
* 인자로 받은 한글 문자 2개를 합성합니다.
* ```typescript
* binaryAssembleHangulCharacters(
* binaryAssembleCharacters(
* // 소스 문자
* source: string
* // 다음 문자
* nextCharacter: string
* ): string
* ```
* @example
* binaryAssembleHangulCharacters('ㄱ', 'ㅏ') // 가
* binaryAssembleHangulCharacters('가', 'ㅇ') // 강
* binaryAssembleHangulCharacters('갑', 'ㅅ') // 값
* binaryAssembleHangulCharacters('깎', 'ㅏ') // 까까
* binaryAssembleCharacters('ㄱ', 'ㅏ') // 가
* binaryAssembleCharacters('가', 'ㅇ') // 강
* binaryAssembleCharacters('갑', 'ㅅ') // 값
* binaryAssembleCharacters('깎', 'ㅏ') // 까까
*/
export function binaryAssembleHangulCharacters(source: string, nextCharacter: string) {
export function binaryAssembleCharacters(source: string, nextCharacter: string) {
assert(
isHangulCharacter(source) || isHangulAlphabet(source),
`Invalid source character: ${source}. Source must be one character.`
Expand All @@ -115,7 +115,7 @@ export function binaryAssembleHangulCharacters(source: string, nextCharacter: st
const isSingleCharacter = sourceJamos.length === 1;
if (isSingleCharacter) {
const sourceCharacter = sourceJamos[0];
return binaryAssembleHangulAlphabets(sourceCharacter, nextCharacter);
return binaryAssembleAlphabets(sourceCharacter, nextCharacter);
}

const [restJamos, lastJamo] = excludeLastElement(sourceJamos);
Expand All @@ -126,7 +126,7 @@ export function binaryAssembleHangulCharacters(source: string, nextCharacter: st
return linkHangulCharacters(source, nextCharacter);
}

const fixConsonant = curriedCombineHangulCharacter;
const fixConsonant = curriedCombineCharacter;
const combineJungseong = fixConsonant(restJamos[0]);

if (canBeJungseong(`${lastJamo}${nextCharacter}`)) {
Expand Down Expand Up @@ -162,30 +162,28 @@ export function binaryAssembleHangulCharacters(source: string, nextCharacter: st
}

/**
* @name binaryAssembleHangul
* @name binaryAssemble
* @description
* 인자로 받은 한글 문장과 한글 문자 하나를 합성합니다.
* ```typescript
* binaryAssembleHangul(
* binaryAssemble(
* // 한글 문장
* source: string
* // 한글 문자
* nextCharacter: string
* ): string
* ```
* @example
* binaryAssembleHangul('저는 고양이를 좋아합닏', 'ㅏ') // 저는 고양이를 좋아합니다
* binaryAssembleHangul('저는 고양이를 좋아합', 'ㅅ') // 저는 고양이를 좋아핪
* binaryAssembleHangul('저는 고양이를 좋아하', 'ㅏ') // 저는 고양이를 좋아하ㅏ
* binaryAssemble('저는 고양이를 좋아합닏', 'ㅏ') // 저는 고양이를 좋아합니다
* binaryAssemble('저는 고양이를 좋아합', 'ㅅ') // 저는 고양이를 좋아핪
* binaryAssemble('저는 고양이를 좋아하', 'ㅏ') // 저는 고양이를 좋아하ㅏ
*/
export function binaryAssembleHangul(source: string, nextCharacter: string) {
export function binaryAssemble(source: string, nextCharacter: string) {
const [rest, lastCharacter] = excludeLastElement(source.split(''));
const needJoinString = isBlank(lastCharacter) || isBlank(nextCharacter);

return joinString(
...rest,
needJoinString
? joinString(lastCharacter, nextCharacter)
: binaryAssembleHangulCharacters(lastCharacter, nextCharacter)
needJoinString ? joinString(lastCharacter, nextCharacter) : binaryAssembleCharacters(lastCharacter, nextCharacter)
);
}
10 changes: 5 additions & 5 deletions src/assemble.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { assembleHangul } from './assemble';
import { assemble } from './assemble';

describe('assembleHangul', () => {
describe('assemble', () => {
it('온전한 한글과 한글 문자 조합', () => {
expect(assembleHangul(['아버지가', ' ', '방ㅇ', 'ㅔ ', '들ㅇ', 'ㅓ갑니다'])).toEqual('아버지가 방에 들어갑니다');
expect(assemble(['아버지가', ' ', '방ㅇ', 'ㅔ ', '들ㅇ', 'ㅓ갑니다'])).toEqual('아버지가 방에 들어갑니다');
});
it('온전한 한글만 조합', () => {
expect(assembleHangul(['아버지가', ' ', '방에 ', '들어갑니다'])).toEqual('아버지가 방에 들어갑니다');
expect(assemble(['아버지가', ' ', '방에 ', '들어갑니다'])).toEqual('아버지가 방에 들어갑니다');
});
it('온전하지 않은 한글만 조합', () => {
expect(assembleHangul(['ㅇ', 'ㅏ', 'ㅂ', 'ㅓ', 'ㅈ', 'ㅣ'])).toEqual('아버지');
expect(assemble(['ㅇ', 'ㅏ', 'ㅂ', 'ㅓ', 'ㅈ', 'ㅣ'])).toEqual('아버지');
});
});
Loading