Skip to content

Commit

Permalink
feat: Strictly manage constants by adding hasValueInStringList, hasPr… (
Browse files Browse the repository at this point in the history
#38)

* feat: Strictly manage constants by adding hasValueInStringList, hasPropert

* feat: Added disassembleCompleteHangulCharacter return type

* Create strong-jars-argue.md

---------

Co-authored-by: 박찬혁 <pgg6713@gmail.com>
  • Loading branch information
ssi02014 and okinawaa authored Apr 15, 2024
1 parent 2cb0d98 commit b7df997
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-jars-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"es-hangul": patch
---

feat: Strictly manage constants by adding hasValueInStringList, hasPr…
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 } from './utils';
import { 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 && HANGUL_CHARACTERS_BY_FIRST_INDEX.includes(disassembled[0] ?? '');
return disassembled.length === 1 && hasValueInReadOnlyStringList(HANGUL_CHARACTERS_BY_FIRST_INDEX, disassembled[0]);
});
}
16 changes: 8 additions & 8 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const NUMBER_OF_JUNGSUNG = 21;
* ㄱ -> 'ㄱ'
* ㄳ -> 'ㄱㅅ' 으로 나눈다.
*/
export const DISASSEMBLED_CONSONANTS_BY_CONSONANT: { [letter: string]: string | undefined } = {
export const DISASSEMBLED_CONSONANTS_BY_CONSONANT = {
// 종성이 없는 경우 '빈' 초성으로 관리하는 것이 편리하여, 빈 문자열도 포함한다.
'': '',
: 'ㄱ',
Expand Down Expand Up @@ -40,9 +40,9 @@ export const DISASSEMBLED_CONSONANTS_BY_CONSONANT: { [letter: string]: string |
: 'ㅌ',
: 'ㅍ',
: 'ㅎ',
};
} as const;

export const DISASSEMBLED_VOWELS_BY_VOWEL: { [letter: string]: string | undefined } = {
export const DISASSEMBLED_VOWELS_BY_VOWEL = {
: 'ㅏ',
: 'ㅐ',
: 'ㅑ',
Expand All @@ -64,7 +64,7 @@ export const DISASSEMBLED_VOWELS_BY_VOWEL: { [letter: string]: string | undefine
: 'ㅡ',
: 'ㅡㅣ',
: 'ㅣ',
};
} as const;

/**
* 초성으로 올 수 있는 한글 글자
Expand All @@ -89,17 +89,17 @@ export const HANGUL_CHARACTERS_BY_FIRST_INDEX = [
'ㅌ',
'ㅍ',
'ㅎ',
];
] as const;

/**
* 중성으로 올 수 있는 한글 글자
*/
export const HANGUL_CHARACTERS_BY_MIDDLE_INDEX = Object.values(DISASSEMBLED_VOWELS_BY_VOWEL) as string[];
export const HANGUL_CHARACTERS_BY_MIDDLE_INDEX = Object.values(DISASSEMBLED_VOWELS_BY_VOWEL);

/**
* 종성으로 올 수 있는 한글 글자
*/
export const HANGUL_CHARACTERS_BY_LAST_INDEX = [
export const HANGUL_CHARACTERS_BY_LAST_INDEX = ([
'',
'ㄱ',
'ㄲ',
Expand Down Expand Up @@ -128,4 +128,4 @@ export const HANGUL_CHARACTERS_BY_LAST_INDEX = [
'ㅌ',
'ㅍ',
'ㅎ',
].map(consonant => DISASSEMBLED_CONSONANTS_BY_CONSONANT[consonant]!);
] as const).map(consonant => DISASSEMBLED_CONSONANTS_BY_CONSONANT[consonant]);
9 changes: 5 additions & 4 deletions src/disassemble.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DISASSEMBLED_CONSONANTS_BY_CONSONANT, DISASSEMBLED_VOWELS_BY_VOWEL } from './constants';
import { disassembleCompleteHangulCharacter } from './disassembleCompleteHangulCharacter';
import { hasProperty } from './utils';

export function disassembleHangulToGroups(str: string) {
/*
Expand All @@ -19,16 +20,16 @@ export function disassembleHangulToGroups(str: string) {
continue;
}

const disassembledConsonant = DISASSEMBLED_CONSONANTS_BY_CONSONANT[letter];
if (hasProperty(DISASSEMBLED_CONSONANTS_BY_CONSONANT, letter)) {
const disassembledConsonant = DISASSEMBLED_CONSONANTS_BY_CONSONANT[letter];

if (disassembledConsonant != null) {
result.push([...disassembledConsonant]);
continue;
}

const disassembledVowel = DISASSEMBLED_VOWELS_BY_VOWEL[letter];
if (hasProperty(DISASSEMBLED_VOWELS_BY_VOWEL, letter)) {
const disassembledVowel = DISASSEMBLED_VOWELS_BY_VOWEL[letter];

if (disassembledVowel != null) {
result.push([...disassembledVowel]);
continue;
}
Expand Down
10 changes: 8 additions & 2 deletions src/disassembleCompleteHangulCharacter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ import {
NUMBER_OF_JUNGSUNG,
} from './constants';

interface ReturnTypeDisassembleCompleteHangulCharacter {
first: (typeof HANGUL_CHARACTERS_BY_FIRST_INDEX)[number];
middle: (typeof HANGUL_CHARACTERS_BY_MIDDLE_INDEX)[number];
last: (typeof HANGUL_CHARACTERS_BY_LAST_INDEX)[number];
}

export function disassembleCompleteHangulCharacter(
letter: string
): { first: string; middle: string; last: string } | undefined {
): ReturnTypeDisassembleCompleteHangulCharacter | undefined {
const charCode = letter.charCodeAt(0);

const isCompleteHangul = COMPLETE_HANGUL_START_CHARCODE <= charCode && charCode <= COMPLETE_HANGUL_END_CHARCODE;
Expand All @@ -29,5 +35,5 @@ export function disassembleCompleteHangulCharacter(
first: HANGUL_CHARACTERS_BY_FIRST_INDEX[firstIndex]!,
middle: HANGUL_CHARACTERS_BY_MIDDLE_INDEX[middleIndex]!,
last: HANGUL_CHARACTERS_BY_LAST_INDEX[lastIndex]!,
};
} as const;
}
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,11 @@ export function getFirstConsonants(word: string) {
return `${firstConsonants}${consonant}`;
}, '');
}

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

export function hasProperty<T extends object, K extends PropertyKey>(obj: T, key: K): key is K & keyof T {
return Object.prototype.hasOwnProperty.call(obj, key);
}

0 comments on commit b7df997

Please sign in to comment.