hangulx
Version:
Providing various utilities for Hangul
318 lines (317 loc) • 13.5 kB
JavaScript
import { Syllable } from "./Syllable";
import { Consonant } from "./Consonant";
import { Vowel } from "./Vowel";
import { COMPLETE } from "./constants/COMPLETE";
var Hangul = /** @class */ (function () {
function Hangul() {
}
/**
* 주어진 charCode가 한글 범위에 속하는지 확인합니다. 자음, 모음 등과 같이 완성된 한글이 아닌 것도 포함됩니다.
*
* Checks if the given charCode falls within the range of Hangul characters, including consonants, vowels, and incomplete Hangul characters.
*
* @param charCode - 검사할 문자 코드 (String.charCodeAt()의 반환값)
* @param charCode - The character code to check (as returned by String.charCodeAt())
* @returns 한글 범위에 속하면 true, 아니면 false
* @returns true if the charCode is within the Hangul range, otherwise false
*/
Hangul.isHangulCharCode = function (charCode) {
if (charCode === null || charCode === undefined) {
return false;
}
// Consonant
if (Consonant.isConsonantCharCode(charCode)) {
return true;
}
// Vowel
if (Vowel.isVowelCharCode(charCode)) {
return true;
}
// Completed Hangul
if (Hangul.isCompleteCharCode(charCode)) {
return true;
}
return false;
};
/**
* 주어진 문자가 한글인지 확인합니다. 자음, 모음 등과 같이 완성된 한글이 아닌 것도 포함됩니다. 문자열이 제공될 경우 첫 문자로 판단합니다.
*
* Checks if the given character is a Hangul character, including consonants, vowels, and incomplete Hangul. If a string is provided, it checks the first character.
*
* @param char - 검사할 단일 문자
* @param char - The single character to check
* @returns 한글이면 true, 아니면 false
* @returns true if the character is a Hangul, otherwise false
*/
Hangul.isHangul = function (char) {
return this.isHangulCharCode(char.charCodeAt(0));
};
/**
* 주어진 문자열의 모든 문자가 한글인지 확인합니다. 자음, 모음 등과 같이 완성된 한글이 아닌 것도 포함됩니다.
*
* Checks if all characters in the given string are Hangul, including consonants, vowels, and incomplete Hangul.
*
* @param str - 검사할 문자열
* @param str - The string to check
* @returns 문자열의 모든 문자가 한글이면 true, 아니면 false
* @returns true if all characters in the string are Hangul, otherwise false
*/
Hangul.isHangulAll = function (str) {
var length = str.length;
if (length === 0) {
return false;
}
for (var i = 0; i < length; ++i) {
if (!this.isHangulCharCode(str.charCodeAt(i))) {
return false;
}
}
return true;
};
/**
* 주어진 문자열에 하나 이상의 한글이 포함되어 있는지 확인합니다. 자음, 모음 등과 같이 완성된 한글이 아닌 것도 포함됩니다.
*
* Checks if the given string contains at least one Hangul character, including consonants, vowels, and incomplete Hangul.
*
* @param str - 검사할 문자열
* @param str - The string to check
* @returns 한글이 하나라도 있으면 true, 없으면 false
* @returns true if there is at least one Hangul character, otherwise false
*/
Hangul.hasHangul = function (str) {
var length = str.length;
if (length === 0) {
return false;
}
for (var i = 0; i < length; ++i) {
if (this.isHangulCharCode(str.charCodeAt(i))) {
return true;
}
}
return false;
};
/**
* 주어진 charCode가 완성된 한글 범위에 속하는지 확인합니다.
*
* Checks if the given charCode falls within the range of complete Korean syllables.
*
* @param charCode - 검사할 문자 코드 (String.charCodeAt()의 반환값)
* @param charCode - The character code to check (as returned by String.charCodeAt())
* @returns 완성된 한글이면 true, 아니면 false
* @returns true if the charCode is a complete Korean syllable, otherwise false
*/
Hangul.isCompleteCharCode = function (charCode) {
if (charCode === null || charCode === undefined) {
return false;
}
if (isNaN(charCode)) {
return false;
}
return charCode >= COMPLETE.START_CHAR_CODE && charCode <= COMPLETE.END_CHAR_CODE;
};
/**
* 주어진 문자가 완성된 한글인지 확인합니다.
*
* Checks if the given character is a complete Korean syllable.
*
* @param char - 검사할 단일 문자
* @param char - The single character to check
* @returns 완성된 한글이면 true, 아니면 false
* @returns true if the character is a complete Korean syllable, otherwise false
*/
Hangul.isComplete = function (char) {
return this.isCompleteCharCode(char.charCodeAt(0));
};
/**
* 주어진 문자열의 모든 문자가 완성된 한글인지 확인합니다.
*
* Checks if all characters in the given string are complete Korean syllables.
*
* @param str - 검사할 문자열
* @param str - The string to check
* @returns 문자열의 모든 문자가 완성된 한글이면 true, 아니면 false
* @returns true if all characters in the string are complete Korean syllables, otherwise false
*/
Hangul.isCompleteAll = function (str) {
var length = str.length;
if (length === 0) {
return false;
}
for (var i = 0; i < length; ++i) {
if (!this.isCompleteCharCode(str.charCodeAt(i))) {
return false;
}
}
return true;
};
/**
* 주어진 문자열에 하나 이상의 완성된 한글이 포함되어 있는지 확인합니다.
*
* Checks if the given string contains at least one complete Korean syllable.
*
* @param str - 검사할 문자열
* @param str - The string to check
* @returns 완성된 한글이 하나라도 있으면 true, 아니면 false
* @returns true if there is at least one complete Korean syllable, otherwise false
*/
Hangul.hasComplete = function (str) {
var length = str.length;
if (length === 0) {
return false;
}
for (var i = 0; i < length; ++i) {
if (this.isCompleteCharCode(str.charCodeAt(i))) {
return true;
}
}
return false;
};
Hangul.disassembleFromChar = function (char, option) {
var charCode = char.charCodeAt(0);
var isHangul = this.isHangulCharCode(charCode);
if (!isHangul) {
// 한글이 아닌 경우
return [char.charAt(0)];
}
// Hangul
var isComplete = this.isCompleteCharCode(charCode);
if (isComplete) {
// 완성된 한글(가-힣)
var results = [];
var syllable = Syllable.disassembleFromCharCode(charCode);
if (syllable) {
if (!option) {
// There is no option
results.push(syllable.cho);
results.push(syllable.jung);
if (syllable.jong) {
results.push(syllable.jong);
}
return results;
}
// There is an option
results.push.apply(results, this.disassembleFromChar(syllable.cho, option));
results.push.apply(results, this.disassembleFromChar(syllable.jung, option));
if (syllable.jong !== undefined) {
results.push.apply(results, this.disassembleFromChar(syllable.jong, option));
}
// Return result
return results;
}
// Failed to disassemble competed hangul to Syllable, Will not processed
}
else {
// 완성된 한글이 아닌 경우(자음, 모음 등)
if (Consonant.isConsonantCharCode(charCode)) {
// 자음인 경우
if (!option) {
return [char.charAt(0)];
}
if (Consonant.isDoubleConsonantCharCode(charCode)) {
if (option.doubleConsonant) {
return Consonant.disassembleDouble(char);
}
}
if (Consonant.isClusterConsonantCharCode(charCode)) {
if (option.clusterConsonant) {
return Consonant.disassembleCompound(char);
}
}
return [char.charAt(0)];
}
if (Vowel.isVowelCharCode(charCode)) {
if (!option) {
return [char.charAt(0)];
}
// ㅣ([j])계 상승이중모음(上昇二重母音, rising diphthong)
if (Vowel.isRisingJDiphthongCode(charCode)) {
if (option.risingJDiphthong) {
return Vowel.disassembleRisingDiphthong(char);
}
}
// ㅗ/ㅜ([w])계 상승이중모음(上昇二重母音, rising diphthong)
if (Vowel.isRisingWDiphthongCode(charCode)) {
if (option.risingWDiphthong) {
return Vowel.disassembleRisingDiphthong(char);
}
}
// 하강이중모음(下降二重母音, falling diphthong)
if (Vowel.isFallingDiphthongCode(charCode)) {
if (option.fallingDiphthong) {
return Vowel.disassembleFallingDiphthong(char);
}
}
return [char.charAt(0)];
}
}
// Not processed
console.warn('not processed character', char);
return [char.charAt(0)];
};
/**
* 주어진 문자열을 자모로 분리합니다. 옵션을 통해 쌍자음과 복합자음의 분리 여부를 설정할 수 있습니다.
*
* Disassembles the given string into its individual Hangul components (jamo).
* The option allows control over whether to split double consonants and cluster consonants.
*
* @param str - 분리할 문자열
* @param str - The string to disassemble
* @param option - 분리 옵션 (쌍자음, 복합자음 분리 여부 설정)
* @param option - The disassembly option (controls splitting of double and cluster consonants)
* @returns 자모로 분리된 문자열 배열
* @returns An array of strings representing the disassembled jamo characters
*/
Hangul.disassemble = function (str, option) {
var length = str.length;
var results = [];
for (var i = 0; i < length; ++i) {
results.push.apply(results, this.disassembleFromChar(str.charAt(i), option));
}
return results;
};
Hangul.disassembleToGroup = function (str, option) {
var length = str.length;
var results = [];
for (var i = 0; i < length; ++i) {
results.push(this.disassembleFromChar(str.charAt(i), option));
}
return results;
};
/**
* 주어진 문자열을 자모로 분리합니다. 옵션을 통해 쌍자음과 복합자음의 분리 여부를 설정할 수 있습니다.
*
* Disassembles the given string into its individual Hangul components (jamo).
* The option allows control over whether to split double consonants and cluster consonants.
*
* @param str - 분리할 문자열
* @param str - The string to disassemble
* @param option - 분리 옵션 (쌍자음, 복합자음 분리 여부 설정)
* @param option - The disassembly option (controls splitting of double and cluster consonants)
* @returns 자모로 분리된 문자열
* @returns strings representing the disassembled jamo characters
*/
Hangul.disassembleToString = function (str, option) {
return this.disassemble(str, option).join((option === null || option === void 0 ? void 0 : option.separator) || '');
};
/**
* 랜덤한 완성형 한글을 지정한 길이만큼 생성합니다.
*
* Generates a random string of complete Hangul characters with the specified length.
*
* @param length 생성할 문자열의 길이를 지정
* @param length Specifies the length of the string to be generated
* @returns 지정된 길이만큼 랜덤하게 생성된 문자열
* @returns A randomly generated string of Hangul characters of the specified length
*/
Hangul.randomComplete = function (length) {
if (length === void 0) { length = 1; }
var results = [];
for (var i = 0; i < length; ++i) {
var randomCharCode = Math.floor(Math.random() * (COMPLETE.END_CHAR_CODE - COMPLETE.START_CHAR_CODE + 1)) + COMPLETE.START_CHAR_CODE;
results.push(String.fromCharCode(randomCharCode));
}
return results.join('');
};
return Hangul;
}());
export { Hangul };