Skip to content

Commit

Permalink
feat: canBeChoseong, canBeJungseong, canBeJongseong 을 utils에서 별…
Browse files Browse the repository at this point in the history
…도 함수로 분리합니다. (#193)

* canBe series를 별도 함수로 분리합니다

* add change set
  • Loading branch information
okinawaa committed Aug 4, 2024
1 parent a5ff8d2 commit 7ed39ac
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 478 deletions.
5 changes: 5 additions & 0 deletions .changeset/pretty-apes-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"es-hangul": major
---

canBeChoseong, canBeJungseong, canBeJongseong 을 utils에서 별도 함수로 분리합니다.
49 changes: 49 additions & 0 deletions docs/src/pages/docs/api/canBe.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# canBeChoseong

Check if a given character can be a choseong in Korean.

```typescript
function canBeChoseong(character: string): boolean;
```

```typescript
canBeChoseong(''); // true
canBeChoseong(''); // true
canBeChoseong('ㄱㅅ'); // false
canBeChoseong(''); // false
canBeChoseong(''); // false
```

# canBeJungseong

Check if a given character can be a jungseong in Korean.

```typescript
function canBeJungseong(character: string): boolean;
```

```typescript
canBeJungseong(''); // true
canBeJungseong('ㅗㅏ'); // true
canBeJungseong('ㅏㅗ'); // false
canBeJungseong(''); // false
canBeJungseong('ㄱㅅ'); // false
canBeJungseong(''); // false
```

# canBeJongseong

Check if a given character can be a jongseong in Korean.

```typescript
function canBeJongseong(character: string): boolean;
```

```typescript
canBeJongseong(''); // true
canBeJongseong('ㄱㅅ'); // true
canBeJongseong('ㅎㄹ'); // false
canBeJongseong(''); // false
canBeJongseong(''); // false
canBeJongseong('ㅗㅏ'); // false
```
49 changes: 49 additions & 0 deletions docs/src/pages/docs/api/canBe.ko.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# canBeChoseong

인자로 받은 문자가 초성으로 위치할 수 있는 문자인지 검사합니다.

```typescript
function canBeChoseong(character: string): boolean;
```

```typescript
canBeChoseong(''); // true
canBeChoseong(''); // true
canBeChoseong('ㄱㅅ'); // false
canBeChoseong(''); // false
canBeChoseong(''); // false
```

# canBeJungseong

인자로 받은 문자가 중성으로 위치할 수 있는 문자인지 검사합니다.

```typescript
function canBeJungseong(character: string): boolean;
```

```typescript
canBeJungseong(''); // true
canBeJungseong('ㅗㅏ'); // true
canBeJungseong('ㅏㅗ'); // false
canBeJungseong(''); // false
canBeJungseong('ㄱㅅ'); // false
canBeJungseong(''); // false
```

# canBeJongseong

인자로 받은 문자가 종성으로 위치할 수 있는 문자인지 검사합니다.

```typescript
function canBeJongseong(character: string): boolean;
```

```typescript
canBeJongseong(''); // true
canBeJongseong('ㄱㅅ'); // true
canBeJongseong('ㅎㄹ'); // false
canBeJongseong(''); // false
canBeJongseong(''); // false
canBeJongseong('ㅗㅏ'); // false
```
3 changes: 2 additions & 1 deletion src/_internal/hangul.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import assert, { excludeLastElement, isBlank, joinString } from '.';
import { canBeChoseong, canBeJungseong, canBeJongseong } from '../canBe';
import { combineHangulCharacter, combineVowels, curriedCombineHangulCharacter } from '../combineHangulCharacter';
import { disassembleToGroups } from '../disassemble';
import { removeLastHangulCharacter } from '../removeLastHangulCharacter';
import { canBeChoseong, canBeJongseong, canBeJungseong, hasSingleBatchim } from '../utils';
import { hasSingleBatchim } from '../utils';

export function isHangulCharacter(character: string) {
return /^[가-힣]$/.test(character);
Expand Down
79 changes: 79 additions & 0 deletions src/canBe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { canBeChoseong, canBeJongseong, canBeJungseong } from './canBe';

describe('canBeChoseong', () => {
describe('초성이 될 수 있다고 판단되는 경우', () => {
it('ㄱ', () => {
expect(canBeChoseong('ㄱ')).toBe(true);
});
it('ㅃ', () => {
expect(canBeChoseong('ㅃ')).toBe(true);
});
});

describe('초성이 될 수 없다고 판단되는 경우', () => {
it('ㅏ', () => {
expect(canBeChoseong('ㅏ')).toBe(false);
});
it('ㅘ', () => {
expect(canBeChoseong('ㅘ')).toBe(false);
});
it('ㄱㅅ', () => {
expect(canBeChoseong('ㄱㅅ')).toBe(false);
});
it('가', () => {
expect(canBeChoseong('가')).toBe(false);
});
});
});

describe('canBeJungseong', () => {
describe('중성이 될 수 있다고 판단되는 경우', () => {
it('ㅗㅏ', () => {
expect(canBeJungseong('ㅗㅏ')).toBe(true);
});
it('ㅏ', () => {
expect(canBeJungseong('ㅏ')).toBe(true);
});
});

describe('중성이 될 수 없다고 판단되는 경우', () => {
it('ㄱ', () => {
expect(canBeJungseong('ㄱ')).toBe(false);
});
it('ㄱㅅ', () => {
expect(canBeJungseong('ㄱㅅ')).toBe(false);
});
it('가', () => {
expect(canBeJungseong('가')).toBe(false);
});
});
});

describe('canBeJongseong', () => {
describe('종성이 될 수 있다고 판단되는 경우', () => {
it('ㄱ', () => {
expect(canBeJongseong('ㄱ')).toBe(true);
});
it('ㄱㅅ', () => {
expect(canBeJongseong('ㄱㅅ')).toBe(true);
});
it('ㅂㅅ', () => {
expect(canBeJongseong('ㅂㅅ')).toBe(true);
});
});

describe('종성이 될 수 없다고 판단되는 경우', () => {
it('ㅎㄹ', () => {
expect(canBeJongseong('ㅎㄹ')).toBe(false);
});
it('ㅗㅏ', () => {
expect(canBeJongseong('ㅗㅏ')).toBe(false);
});
it('ㅏ', () => {
expect(canBeJongseong('ㅏ')).toBe(false);
});
it('가', () => {
expect(canBeJongseong('ㅏ')).toBe(false);
});
});
});
71 changes: 71 additions & 0 deletions src/canBe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {
HANGUL_CHARACTERS_BY_FIRST_INDEX,
HANGUL_CHARACTERS_BY_LAST_INDEX,
HANGUL_CHARACTERS_BY_MIDDLE_INDEX,
} from './constants';
import { hasValueInReadOnlyStringList } from './utils';

/**
* @name canBeChoseong
* @description
* 인자로 받은 문자가 초성으로 위치할 수 있는 문자인지 검사합니다.
* ```typescript
* canBeChoseong(
* // 대상 문자
* character: string
* ): boolean
* ```
* @example
* canBeChoseong('ㄱ') // true
* canBeChoseong('ㅃ') // true
* canBeChoseong('ㄱㅅ') // false
* canBeChoseong('ㅏ') // false
* canBeChoseong('가') // false
*/
export function canBeChoseong(character: string): character is (typeof HANGUL_CHARACTERS_BY_FIRST_INDEX)[number] {
return hasValueInReadOnlyStringList(HANGUL_CHARACTERS_BY_FIRST_INDEX, character);
}

/**
* @name canBeJungseong
* @description
* 인자로 받은 문자가 중성으로 위치할 수 있는 문자인지 검사합니다.
* ```typescript
* canBeJungseong(
* // 대상 문자
* character: string
* ): boolean
* ```
* @example
* canBeJungseong('ㅏ') // true
* canBeJungseong('ㅗㅏ') // true
* canBeJungseong('ㅏㅗ') // false
* canBeJungseong('ㄱ') // false
* canBeJungseong('ㄱㅅ') // false
* canBeJungseong('가') // false
*/
export function canBeJungseong(character: string): character is (typeof HANGUL_CHARACTERS_BY_MIDDLE_INDEX)[number] {
return hasValueInReadOnlyStringList(HANGUL_CHARACTERS_BY_MIDDLE_INDEX, character);
}

/**
* @name canBeJongseong
* @description
* 인자로 받은 문자가 종성으로 위치할 수 있는 문자인지 검사합니다.
* ```typescript
* canBeJongseong(
* // 대상 문자
* character: string
* ): boolean
* ```
* @example
* canBeJongseong('ㄱ') // true
* canBeJongseong('ㄱㅅ') // true
* canBeJongseong('ㅎㄹ') // false
* canBeJongseong('가') // false
* canBeJongseong('ㅏ') // false
* canBeJongseong('ㅗㅏ') // false
*/
export function canBeJongseong(character: string): character is (typeof HANGUL_CHARACTERS_BY_LAST_INDEX)[number] {
return hasValueInReadOnlyStringList(HANGUL_CHARACTERS_BY_LAST_INDEX, character);
}
2 changes: 1 addition & 1 deletion src/choseongIncludes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { canBeChoseong } from './canBe';
import { disassembleToGroups } from './disassemble';
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/combineHangulCharacter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { canBeChoseong, canBeJongseong, canBeJungseong } from './canBe';
import {
COMPLETE_HANGUL_START_CHARCODE,
DISASSEMBLED_VOWELS_BY_VOWEL,
HANGUL_CHARACTERS_BY_FIRST_INDEX,
HANGUL_CHARACTERS_BY_LAST_INDEX,
HANGUL_CHARACTERS_BY_MIDDLE_INDEX,
} from './constants';
import { canBeChoseong, canBeJongseong, canBeJungseong } from './utils';

/**
* @name combineHangulCharacter
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
export { assembleHangul } from './assemble';
export { canBeChoseong, canBeJongseong, canBeJungseong } from './canBe';
export { choseongIncludes } from './choseongIncludes';
export { chosungIncludes } from './chosungIncludes';
export { combineHangulCharacter, combineVowels, curriedCombineHangulCharacter } from './combineHangulCharacter';
export { convertQwertyToHangul, convertQwertyToHangulAlphabet } from './convertQwertyToHangulAlphabet';
export { disassemble, disassembleToGroups } from './disassemble';
export { disassembleCompleteCharacter } from './disassembleCompleteCharacter';
export { getChoseong } from './getChoseong';
export { josa } from './josa';
export { removeLastHangulCharacter } from './removeLastHangulCharacter';
export { romanize } from './romanize';
export { standardizePronunciation } from './standardizePronunciation';
export { susa } from './susa';
export { getChoseong } from './getChoseong';
4 changes: 2 additions & 2 deletions src/removeLastHangulCharacter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { combineHangulCharacter } from './combineHangulCharacter';
import { excludeLastElement } from './_internal';
import { canBeJungsung } from './utils';
import { canBeJungseong } from './canBe';
import { disassembleToGroups } from './disassemble';

/**
Expand Down Expand Up @@ -32,7 +32,7 @@ export function removeLastHangulCharacter(words: string) {
if (lastCharacterWithoutLastAlphabet.length <= 3) {
const [first, middle, last] = lastCharacterWithoutLastAlphabet;
if (middle != null) {
return canBeJungsung(last)
return canBeJungseong(last)
? combineHangulCharacter(first, `${middle}${last}`)
: combineHangulCharacter(first, middle, last);
}
Expand Down
Loading

0 comments on commit 7ed39ac

Please sign in to comment.