Skip to content

Commit

Permalink
feat: 인자로 받은 문자의 초성, 중성, 종성 위치 가능 여부를 파악하는 함수 추가 (#36)
Browse files Browse the repository at this point in the history
* feat: 인자로 받은 문자의 초성, 중성, 종성 위치 가능 여부를 파악하는 함수 추가

* Create sweet-fireants-cough.md
  • Loading branch information
evan-moon committed Apr 16, 2024
1 parent a1ae281 commit 1664575
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/sweet-fireants-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"es-hangul": patch
---

feat: 인자로 받은 문자의 초성, 중성, 종성 위치 가능 여부를 파악하는 함수 추가 #24
4 changes: 2 additions & 2 deletions src/chosungIncludes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HANGUL_CHARACTERS_BY_FIRST_INDEX } from './constants';
import { disassembleHangulToGroups } from './disassemble';
import { getFirstConsonants, hasValueInReadOnlyStringList } from './utils';
import { canBeChosung, getFirstConsonants, hasValueInReadOnlyStringList } from './utils';

export function chosungIncludes(x: string, y: string) {
if (!isOnlyInitialConsonant(y)) {
Expand All @@ -18,6 +18,6 @@ export function chosungIncludes(x: string, y: string) {
*/
function isOnlyInitialConsonant(str: string) {
return disassembleHangulToGroups(str).every(disassembled => {
return disassembled.length === 1 && hasValueInReadOnlyStringList(HANGUL_CHARACTERS_BY_FIRST_INDEX, disassembled[0]);
return disassembled.length === 1 && canBeChosung(disassembled[0]);
});
}
65 changes: 64 additions & 1 deletion src/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, expectTypeOf, it } from 'vitest';
import { getFirstConsonants, hasBatchim, hasProperty, hasValueInReadOnlyStringList } from './utils';
import { canBeChosung, canBeJongsung, canBeJungsung, getFirstConsonants, hasBatchim, hasProperty, hasValueInReadOnlyStringList } from './utils';

describe('hasBatchim', () => {
it('should return true for the character "값"', () => {
Expand Down Expand Up @@ -89,3 +89,66 @@ describe('hasProperty', () => {
}
});
});

describe('canBeChosung', () => {
it('ㄱ', () => {
expect(canBeChosung('ㄱ')).toBe(true);
});
it('ㅃ', () => {
expect(canBeChosung('ㅃ')).toBe(true);
});
it('ㅏ', () => {
expect(canBeChosung('ㅏ')).toBe(false);
});
it('ㅘ', () => {
expect(canBeChosung('ㅏ')).toBe(false);
});
it('ㄱㅅ', () => {
expect(canBeChosung('ㅏ')).toBe(false);
});
it('가', () => {
expect(canBeChosung('ㅏ')).toBe(false);
});
});

describe('canBeJungsung', () => {
it('ㅗㅏ', () => {
expect(canBeJungsung('ㅗㅏ')).toBe(true);
});
it('ㅏ', () => {
expect(canBeJungsung('ㅏ')).toBe(true);
});
it('ㄱ', () => {
expect(canBeJungsung('ㄱ')).toBe(false);
});
it('ㄱㅅ', () => {
expect(canBeJungsung('ㄱㅅ')).toBe(false);
});
it('가', () => {
expect(canBeJungsung('가')).toBe(false);
});
});

describe('canBeJongsung', () => {
it('ㄱ', () => {
expect(canBeJongsung('ㄱ')).toBe(true);
});
it('ㄱㅅ', () => {
expect(canBeJongsung('ㄱㅅ')).toBe(true);
});
it('ㅂㅅ', () => {
expect(canBeJongsung('ㅂㅅ')).toBe(true);
});
it('ㅎㄹ', () => {
expect(canBeJongsung('ㅎㄹ')).toBe(false);
});
it('ㅗㅏ', () => {
expect(canBeJongsung('ㅗㅏ')).toBe(false);
});
it('ㅏ', () => {
expect(canBeJongsung('ㅏ')).toBe(false);
});
it('가', () => {
expect(canBeJongsung('ㅏ')).toBe(false);
});
});
70 changes: 70 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import {
HANGUL_CHARACTERS_BY_FIRST_INDEX,
HANGUL_CHARACTERS_BY_LAST_INDEX,
HANGUL_CHARACTERS_BY_MIDDLE_INDEX,
} from './constants';
import { disassembleHangulToGroups } from './disassemble';
import { disassembleCompleteHangulCharacter } from './disassembleCompleteHangulCharacter';

Expand Down Expand Up @@ -42,6 +47,71 @@ export function getFirstConsonants(word: string) {
}, '');
}

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

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

/**
* @name canBeJongsung
* @description
* 인자로 받은 문자가 종성으로 위치할 수 있는 문자인지 검사합니다.
* ```typescript
* canBeJongsung(
* // 대상 문자
* character: string
* ): boolean
* ```
* @example
* canBeChosung('ㄱ') // true
* canBeChosung('ㄱㅅ') // true
* canBeChosung('ㅎㄹ') // false
* canBeChosung('가') // false
* canBeChosung('ㅏ') // false
* canBeChosung('ㅗㅏ') // false
*/
export function canBeJongsung(character: string) {
return hasValueInReadOnlyStringList(HANGUL_CHARACTERS_BY_LAST_INDEX, character);
}

export function hasValueInReadOnlyStringList<T extends string>(list: readonly T[], value: string): value is T {
return list.some(item => item === value);
}
Expand Down

0 comments on commit 1664575

Please sign in to comment.