@daeinc/hangul
Version:
Hangul utilities
342 lines (332 loc) • 17.4 kB
TypeScript
/**
* A list of Hangul consonants that can be used as choseongs.
*
* @example
* ["ㄱ", "ㄲ", .. , "ㅎ"]
*/
declare const CHOSEONGS: readonly ["ㄱ", "ㄲ", "ㄴ", "ㄷ", "ㄸ", "ㄹ", "ㅁ", "ㅂ", "ㅃ", "ㅅ", "ㅆ", "ㅇ", "ㅈ", "ㅉ", "ㅊ", "ㅋ", "ㅌ", "ㅍ", "ㅎ"];
/**
* A list of Hangul vowels that can be used as jungseongs.
*
* @example
* ["ㅏ", .. , "ㅑ", .. , "ㅜㅓ", .. , "ㅣ"]
*/
declare const JUNGSEONGS: readonly ["ㅏ", "ㅐ", "ㅑ", "ㅒ", "ㅓ", "ㅔ", "ㅕ", "ㅖ", "ㅗ", "ㅗㅏ", "ㅗㅐ", "ㅗㅣ", "ㅛ", "ㅜ", "ㅜㅓ", "ㅜㅔ", "ㅜㅣ", "ㅠ", "ㅡ", "ㅡㅣ", "ㅣ"];
/**
* A list of Hangul consonants that can be used as jongseongs.
* The list is not exhaustive of all the consonants as it only contains what can be used as jongseong which is found in Hangul Syllables
* and the charcodes are from the Unicode Hangul Compatibility Jamo table.
*
* @example
* ["ㄱ", "ㄲ", .. , "ㄱㅅ", .. , "ㅎ"]
*/
declare const JONGSEONGS_DISASSEMBLED: readonly ["", "ㄱ", "ㄲ", "ㄱㅅ", "ㄴ", "ㄴㅈ", "ㄴㅎ", "ㄷ", "ㄹ", "ㄹㄱ", "ㄹㅁ", "ㄹㅂ", "ㄹㅅ", "ㄹㅌ", "ㄹㅍ", "ㄹㅎ", "ㅁ", "ㅂ", "ㅂㅅ", "ㅅ", "ㅆ", "ㅇ", "ㅈ", "ㅊ", "ㅋ", "ㅌ", "ㅍ", "ㅎ"];
declare const VOWELS_VERTICAL: readonly ["ㅏ", "ㅐ", "ㅑ", "ㅒ", "ㅓ", "ㅔ", "ㅕ", "ㅖ", "ㅣ"];
declare const VOWELS_HORIZONTAL: readonly ["ㅗ", "ㅛ", "ㅜ", "ㅠ", "ㅡ"];
declare const VOWELS_COMPOUND: readonly ["ㅘ", "ㅙ", "ㅚ", "ㅝ", "ㅞ", "ㅟ", "ㅢ"];
/** the first code in Unicode Hangul Syllables table */
declare const HANGUL_SYLLABLES_START_CHARCODE = 44032;
/** the last code in Unicode Hangul Syllables table */
declare const HANGUL_SYLLABLES_END_CHARCODE = 55215;
/** the first code in Unicode Hangul Compatibility Jamo table */
declare const HANGUL_COMPAT_JAMO_START_CHARCODE = 12592;
declare const HANGUL_COMPAT_JAMO_END_CHARCODE = 12687;
/** 'ㄱ', the first consonant in Unicode Hangul Compatibility Jamo table */
declare const HANGUL_COMPAT_CONSONANT_START_CHARCODE = 12593;
/** 'ㅎ', the last consonant in Unicode Hangul Compatibility Jamo table */
declare const HANGUL_COMPAT_CONSONANT_END_CHARCODE = 12622;
/** 'ㅏ', the first vowel in Unicode Hangul Compatibility Jamo table */
declare const HANGUL_COMPAT_VOWEL_START_CHARCODE = 12623;
/** 'ㅣ', the first vowel in Unicode Hangul Compatibility Jamo table */
declare const HANGUL_COMPAT_VOWEL_END_CHARCODE = 12643;
/** 'ㅥ', the first old consonant in Unicode Hangul Compatibility Jamo table */
declare const HANGUL_COMPAT_OLD_CONSONANT_START_CHARCODE = 12645;
/** 'ㆆ', the last old consonant in Unicode Hangul Compatibility Jamo table */
declare const HANGUL_COMPAT_OLD_CONSONANT_END_CHARCODE = 12678;
/** 'ㆇ', the first vowel in Unicode Hangul Compatibility Jamo table */
declare const HANGUL_COMPAT_OLD_VOWEL_START_CHARCODE = 12679;
/** 'ㆎ', the first vowel in Unicode Hangul Compatibility Jamo table */
declare const HANGUL_COMPAT_OLD_VOWEL_END_CHARCODE = 12686;
/** the first code in Unicode Hangul Jamo table */
declare const HANGUL_JAMO_START_CHARCODE = 4352;
/** the last code in Unicode Hangul Jamo table */
declare const HANGUL_JAMO_END_CHARCODE = 4607;
/** 'ᄀ', the first initial consonant (choseong) in Unicode Hangul Jamo table */
declare const HANGUL_JAMO_INITIAL_CONSONANT_START_CHARCODE = 4352;
/** 'ᄒ', the last initial consonant (choseong) in Unicode Hangul Jamo table */
declare const HANGUL_JAMO_INITIAL_CONSONANT_END_CHARCODE = 4370;
/** NIEUN-KIYEOK, the first old initial consonant (choseong) in Unicode Hangul Jamo table */
declare const HANGUL_JAMO_OLD_INITIAL_CONSONANT_START_CHARCODE = 4371;
/** TIKEUT-RIEUL, the last old initial consonant (choseong) in Unicode Hangul Jamo table */
declare const HANGUL_JAMO_OLD_INITIAL_CONSONANT_END_CHARCODE = 4446;
/** A, the first medial vowel (jungseong) in Unicode Hangul Jamo table */
declare const HANGUL_JAMO_VOWEL_START_CHARCODE = 4449;
/** I, the last medial vowel (jungseong) in Unicode Hangul Jamo table */
declare const HANGUL_JAMO_VOWEL_END_CHARCODE = 4469;
/** A-O, the first old medial vowel (jungseong) in Unicode Hangul Jamo table */
declare const HANGUL_JAMO_OLD_VOWEL_START_CHARCODE = 4470;
/** O-YAE, the last old medial vowel (jungseong) in Unicode Hangul Jamo table */
declare const HANGUL_JAMO_OLD_VOWEL_END_CHARCODE = 4519;
/** KIYEOK, the first final consonant (jongseong) in Unicode Hangul Jamo table */
declare const HANGUL_JAMO_FINAL_CONSONANT_START_CHARCODE = 4520;
/** HIEUH, the last final consonant (jongseong) in Unicode Hangul Jamo table */
declare const HANGUL_JAMO_FINAL_CONSONANT_END_CHARCODE = 4546;
/** KIYEOK-RIEUL, the first old final consonant (jongseong) in Unicode Hangul Jamo table */
declare const HANGUL_JAMO_OLD_FINAL_CONSONANT_START_CHARCODE = 4547;
/** SSANGNIEUN, the last old final consonant (jongseong) in Unicode Hangul Jamo table */
declare const HANGUL_JAMO_OLD_FINAL_CONSONANT_END_CHARCODE = 4607;
/**
* the first code in Unicode Hangul Jamo Extended A table
* It contains the old initial consonants (choseong)
*/
declare const HANGUL_JAMO_EXTENDED_A_START_CHARCODE = 43360;
/**
* the last code in Unicode Hangul Jamo Extended A table
* It contains the old initial consonants (choseong)
*/
declare const HANGUL_JAMO_EXTENDED_A_END_CHARCODE = 43391;
/**
* the first code in Unicode Hangul Jamo Extended B table
* It contains the old medial vowels (jungseong) and old final consonants (jongseong).
*/
declare const HANGUL_JAMO_EXTENDED_B_START_CHARCODE = 55216;
/**
* the last code in Unicode Hangul Jamo Extended B table
* It contains the old medial vowels (jungseong) and old final consonants (jongseong).
*/
declare const HANGUL_JAMO_EXTENDED_B_END_CHARCODE = 55295;
/** O-YEO, the first old medial vowel (jungseong) in Unicode Hangul Jamo Extended B table */
declare const HANGUL_JAMO_EXTENDED_B_VOWEL_START_CHARCODE = 55216;
/** ARAEA-E, the last old medial vowel (jungseong) in Unicode Hangul Jamo Extended B table */
declare const HANGUL_JAMO_EXTENDED_B_VOWEL_END_CHARCODE = 55238;
/** NIEUN-RIEUL, the first old final consonant (jongseong) in Unicode Hangul Jamo Extended B table */
declare const HANGUL_JAMO_EXTENDED_B_CONSONANT_START_CHARCODE = 55243;
/** PHIEUPH-THIEUTH, the last old final consonant (jongseong) in Unicode Hangul Jamo Extended B table */
declare const HANGUL_JAMO_EXTENDED_B_CONSONANT_END_CHARCODE = 55291;
/**
* Assemble input string array.
* Hangul characters will be assembled into complete syllables.
* For non-existing combinations, they will be returned as individual jaso elements. Non-Hangul characters are returned as is.
*
* @param strArr - string (in array) to assemble
*
* @example
* ```ts
* assemble(["ㅁ", "ㅗ", "ㅏ", "ㄹ", "ㄱ"]) // "뫍"
* assemble(["ㅂ", "ㅂ", "ㅏ"]) // "ㅂ바"
* assemble(["ㅁ", "ㅣ", "ㅋ", "ㅣ", "17"]) // "미키17"
* assemble(["미키", "17"]) // "미키17"
* ```
*/
declare const assemble: (strArr: string[]) => string;
/**
* Assemble input string array.
* Hangul characters will be assembled into complete syllables.
* For non-existing combinations, they will be returned as individual jaso elements. Non-Hangul characters are returned as is.
*
* @param strArr - string (in array) to assemble
*
* @example
* ```ts
* assemble(["ㅁ", "ㅗ", "ㅏ", "ㄹ", "ㄱ"]) // "뫍"
* assemble(["ㅂ", "ㅂ", "ㅏ"]) // "ㅂ바"
* assemble(["ㅁ", "ㅣ", "ㅋ", "ㅣ", "17"]) // "미키17"
* assemble(["미키", "17"]) // "미키17"
* ```
*/
declare const _assemble: (strArr: string[]) => string;
/**
* Check if the input string is a valid Jongseong. It simply uses `es-hangul` method of the same name.
*
* @param str - can be a single or double characters
*/
declare const canBeJongseong: (str: string) => str is "" | "ㄱ" | "ㄲ" | "ㄴ" | "ㄷ" | "ㄹ" | "ㅁ" | "ㅂ" | "ㅅ" | "ㅆ" | "ㅇ" | "ㅈ" | "ㅊ" | "ㅋ" | "ㅌ" | "ㅍ" | "ㅎ" | "ㄱㅅ" | "ㄴㅈ" | "ㄴㅎ" | "ㄹㄱ" | "ㄹㅁ" | "ㄹㅂ" | "ㄹㅅ" | "ㄹㅌ" | "ㄹㅍ" | "ㄹㅎ" | "ㅂㅅ";
interface DisassembledCharacter {
choseong: string;
jungseong: string;
jongseong: string;
}
/**
* Disassemble Hangul syllable(s) into `[ { choseong, jungseong, jongseong }, ... ]`
* If input string is not a full Hangul syllable, it will be returned as either choseong(consonant) or jungseong(vowel).
* Hangul characters from Jamo, Jamo Extended A & B ranges are not disassembled and returned as is.
* This is due to the current reliance on `es-hangul`, which does not disassemble Jamo range.
* Non-Hangul characters are returned as is.
*
* @param str - string to disassemble
*
* @example
* ```ts
* disassemble("ㅁ") // [{ choseong: "ㅁ", jungseong: "", jongseong: "" }]
* disassemble("ㅗ")) // [{ choseong: "", jungseong: "ㅗ", jongseong: "" }]
* disassemble("ㄼ") // [{ choseong: "ㄹㅂ", jungseong: "", jongseong: "" }]
* disassemble("몫") // [{ choseong: "ㅁ", jungseong: "ㅗ", jongseong: "ㄱㅅ" }]
* disassemble("꽈") // [{ choseong: "ㄲ", jungseong: "ㅗㅏ", jongseong: "" }]
* disassemble("와yo") // [{ choseong: "ㅇ", jungseong: "ㅗㅏ", jongseong: "" }, "y", "o"]
* ```
*/
declare const disassemble: (str: string) => (string | DisassembledCharacter)[];
/**
* Returns a plain disassembled string
*
* @example
disassembleToJamoString("가나") // "ㄱㅏㄴㅏ"
disassembleToJamoString("밟아") // "ㅂㅏㄹㅂㅇㅏ"
disassembleToJamoString("외워") // "ㅇㅗㅣㅇㅜㅓ"
disassembleToJamoString("ㅘㅇ") // "ㅗㅏㅇ"
disassembleToJamoString("밟abc아") // "ㅂㅏㄹㅂabcㅇㅏ"
*
* @param phrase - A string to disassemble. May or may not include non-Hangul characters
*/
declare const disassembleToJamoString: (phrase: string) => string;
interface IHangulSyllable {
choseong: string;
jungseong: string;
jongseong: string;
}
/**
* Syllable class holds Hangul syllable data `{ choseong, jungseong, jongseong }`.
* There are a few useful methods for processing the Hangul syllable.
* It is most likely a mistake trying to store non-Hangul characters.
*/
declare class HangulSyllable {
char: string;
disassembled: IHangulSyllable;
id: number;
constructor(char: string);
setId(id: number): void;
clone(): HangulSyllable;
isEmpty(): boolean;
hasComponent(): boolean;
hasChoseong(): boolean;
hasJungseong(): boolean;
hasJongseong(): boolean;
/**
* Removes a single component starting from jongseong, jungseong to choseong.
* If it has a double jongseong or jungseong, it will remove a single one each time.
*
* @example
* const syl = new HangulSyllable("깖");
* syl.removeSingleComponent() // syl.char == "깔"
* syl.removeSingleComponent() // "까"
* syl.removeSingleComponent() // "ㄲ"
* syl.removeSingleComponent() // ""
*/
removeSingleComponent(): void;
/**
* @example
* const syl = new HangulSyllable("깖");
* syl.removeSingleJongseong() // syl.char == "깔"
*
* @example
* const syl = new HangulSyllable("깔");
* syl.removeSingleJongseong() // syl.char == "까"
*/
removeSingleJongseong(): void;
/**
* Updates `this.char` and `this.disassembled` according to the input `char`
*
* @param char -
*/
updateSyllable(char: string): void;
/**
* Returns an object with Hangul components
*
* @example
* const syl = new HangulSyllable("깖");
* syl.getDisassembled() // {choseong: "ㄲ", jungseong: "ㅏ", jongseong: "ㄹㅁ"}
*/
getDisassembled(): IHangulSyllable;
/**
* Returns an array of each Hangul component. Note that a double Jongseong is broken down.
*
* @example
* const syl = new HangulSyllable("깖");
* syl.getDisassembledStringArray() // ["ㄲ", "ㅏ", "ㄹㅁ"]
*/
getDisassembledStringArray(): string[];
/**
* Returns a flattened string each Hangul component. Note that a double Jongseong is broken down.
*
* @example
* const syl = new HangulSyllable("깖");
* syl.getDisassembledString() // "ㄲㅏㄹㅁ"
*/
getDisassembledString(): string;
}
/**
* Check if `first` syllable is ready with no chance of further transformation.
* A syllable may be considered ready even if it is not a complete syllable. The function uses the inputs as they are and does not combine/assemble them to form a new syllable.
* This function only checks for the `first` readiness in this specific sequence of two syllables where the readiness means it won't change upon adding new jamo(s).
* The main use case is in real-time keydown events.
*
* @param first - First syllable to check
* @param second - Second syllable being added (ie. keypress)
*/
declare const isFirstSyllableReady: (first: string, second?: string) => boolean;
/**
* Takes in a single character and determine if it's hangul or not
* It compares with Hangul [Unicode charts](https://www.unicode.org/charts/).
* By default, Hangul Compatibility Jamo and Syllables ranges are checked as these are the most common user inputs.
* To include other ranges such as Jamo, JamoExtendedA or JamoExtendedB, use the `opts` object to set the flags.
* Halfwidth jamo range is not included.
*
* @param ch - a single character to check
* @param opts - Optional object to set which Unicode ranges to check.
* @param opts.jamo - default: false
* @param opts.jamoExtendedA - default: false
* @param opts.jamoExtendedB - default: false
* @param opts.compatJamo - default: true
* @param opts.syllable - default: true
*
* @example
* const compatJamo1 = "ㄱ" // 0x3131
* isHangul(compatJamo1); // true
*
* const jamo1 = "ᄀ"; // 0x1100; KIYEOK looks the same but from a different table range.
* isHangul(jamo1); // false
* isHangul(jamo1, { jamo: true }); // true
*
* const jamoExtA = "ꥤ"; // 0xA964
* isHangul(jamoExtA, { jamoExtendedA: true }); // true
*/
declare const isHangul: (ch: string, opts?: {
jamo?: boolean;
jamoExtendedA?: boolean;
jamoExtendedB?: boolean;
compatJamo?: boolean;
syllable?: boolean;
}) => boolean;
declare const isHangulSyllable: (ch: string) => boolean;
declare const isHangulJamo: (ch: string) => boolean;
declare const isHangulCompatJamo: (ch: string) => boolean;
declare const isHangulJamoExtendedA: (ch: string) => boolean;
declare const isHangulJamoExtendedB: (ch: string) => boolean;
/**
* It looks at Unicode Hangul Compatibility Jamo range (`ㄱ-ㅎ`) to decide whether input `ch` is Hangul consonant or not.
* Old consonants are not included.
*/
declare const isConsonant: (ch: string) => boolean;
/**
* It looks at Unicode Hangul Compatibility Jamo range (`ㅏ-ㅣ`) to decide whether input `ch` is Hangul vowel or not.
* Old vowels are not included.
*
* @param ch - A character to test
* @param opts -
* @param opts.vertical - Whether `ch` is in `[ "ㅏ", "ㅐ", "ㅑ", "ㅒ", "ㅓ", "ㅔ", "ㅕ", "ㅖ", "ㅣ" ]`
* @param opts.horizontal - Whether `ch` is in `["ㅗ", "ㅛ", "ㅜ", "ㅠ", "ㅡ"]`
* @param opts.compound - Whether `ch` is in `[ "ㅘ", "ㅙ", "ㅚ", "ㅝ", "ㅞ", "ㅟ", "ㅢ" ]`
*/
declare const isVowel: (ch: string, opts?: {
vertical?: boolean;
horizontal?: boolean;
compound?: boolean;
}) => boolean;
/**
* Given a Hangul character, check if it is a consonant(`"ja"`) or vowel(`"mo"`).
* A full Hangul syllable returns `"syllable"`. If non-Hanul, returns `"other"`
*
* @param ch - a character to test
*/
declare const isJaOrMo: (ch: string) => "ja" | "mo" | "syllable" | "other";
export { CHOSEONGS, type DisassembledCharacter, HANGUL_COMPAT_CONSONANT_END_CHARCODE, HANGUL_COMPAT_CONSONANT_START_CHARCODE, HANGUL_COMPAT_JAMO_END_CHARCODE, HANGUL_COMPAT_JAMO_START_CHARCODE, HANGUL_COMPAT_OLD_CONSONANT_END_CHARCODE, HANGUL_COMPAT_OLD_CONSONANT_START_CHARCODE, HANGUL_COMPAT_OLD_VOWEL_END_CHARCODE, HANGUL_COMPAT_OLD_VOWEL_START_CHARCODE, HANGUL_COMPAT_VOWEL_END_CHARCODE, HANGUL_COMPAT_VOWEL_START_CHARCODE, HANGUL_JAMO_END_CHARCODE, HANGUL_JAMO_EXTENDED_A_END_CHARCODE, HANGUL_JAMO_EXTENDED_A_START_CHARCODE, HANGUL_JAMO_EXTENDED_B_CONSONANT_END_CHARCODE, HANGUL_JAMO_EXTENDED_B_CONSONANT_START_CHARCODE, HANGUL_JAMO_EXTENDED_B_END_CHARCODE, HANGUL_JAMO_EXTENDED_B_START_CHARCODE, HANGUL_JAMO_EXTENDED_B_VOWEL_END_CHARCODE, HANGUL_JAMO_EXTENDED_B_VOWEL_START_CHARCODE, HANGUL_JAMO_FINAL_CONSONANT_END_CHARCODE, HANGUL_JAMO_FINAL_CONSONANT_START_CHARCODE, HANGUL_JAMO_INITIAL_CONSONANT_END_CHARCODE, HANGUL_JAMO_INITIAL_CONSONANT_START_CHARCODE, HANGUL_JAMO_OLD_FINAL_CONSONANT_END_CHARCODE, HANGUL_JAMO_OLD_FINAL_CONSONANT_START_CHARCODE, HANGUL_JAMO_OLD_INITIAL_CONSONANT_END_CHARCODE, HANGUL_JAMO_OLD_INITIAL_CONSONANT_START_CHARCODE, HANGUL_JAMO_OLD_VOWEL_END_CHARCODE, HANGUL_JAMO_OLD_VOWEL_START_CHARCODE, HANGUL_JAMO_START_CHARCODE, HANGUL_JAMO_VOWEL_END_CHARCODE, HANGUL_JAMO_VOWEL_START_CHARCODE, HANGUL_SYLLABLES_END_CHARCODE, HANGUL_SYLLABLES_START_CHARCODE, HangulSyllable, type IHangulSyllable, JONGSEONGS_DISASSEMBLED, JUNGSEONGS, VOWELS_COMPOUND, VOWELS_HORIZONTAL, VOWELS_VERTICAL, _assemble, assemble, canBeJongseong, disassemble, disassembleToJamoString, isConsonant, isFirstSyllableReady, isHangul, isHangulCompatJamo, isHangulJamo, isHangulJamoExtendedA, isHangulJamoExtendedB, isHangulSyllable, isJaOrMo, isVowel };