Skip to main content
Module

x/kokr/text/jongseong.ts

한국/한국어와 관련된 라이브러리
Latest
File
const RE_DIGIT_ZEROS = /[1-9](0+)$/;const RE_ENG_LAST_TWO = /[a-z]{2}$/i;
const enum Jongseong {= 1,= 4,= 8,= 16,= 17,= 19,= 21,}
const digitZerosMap = [ Jongseong., // 십 Jongseong., // 백 Jongseong., // 천 Jongseong., // 만 Jongseong., // 십만 Jongseong., // 백만 Jongseong., // 천만 Jongseong., // 억 Jongseong., // 십억 Jongseong., // 백억 Jongseong., // 천억 0, // 조 0, // 십조 0, // 백조 0, // 천조 Jongseong., // 경 Jongseong., // 십경 Jongseong., // 백경 Jongseong., // 천경 0, // 해 0, // 십해 0, // 백해 0, // 천해];const digitMap = [ Jongseong., // 영 Jongseong., // 일 0, // 이 Jongseong., // 삼 0, // 사 0, // 오 Jongseong., // 육 Jongseong., // 칠 Jongseong., // 팔 0, // 구];
const engSuffix2Map: Record<string, number> = { nd: 0, ne: Jongseong., le: Jongseong., ng: Jongseong.,};
const engSuffixMap: Record<string, number> = { b: Jongseong., c: Jongseong., d: Jongseong., k: Jongseong., l: Jongseong., m: Jongseong., n: Jongseong., p: Jongseong., t: Jongseong.,};
const engCharMap: Record<string, number> = { l: Jongseong., m: Jongseong., n: Jongseong., r: Jongseong.,};
/** @internal */export function jongseong(word: string): number { let w = word; while (w.length) { // strip paren ABC(D) => ABC w = w.replace(/\([^)]*\)$/, "");
const last = w[w.length - 1]; const lastCharCode = last.charCodeAt(0);
if (lastCharCode >= 44032 && lastCharCode <= 55203) { // 가-힣 return (lastCharCode - 44032) % 28; }
// digit if (lastCharCode >= 48 && lastCharCode <= 57) { // 0-9 const zerosMatch = RE_DIGIT_ZEROS.exec(w); if (zerosMatch) { return digitZerosMap[zerosMatch[1].length - 1] ?? 0; } return digitMap[lastCharCode - 48]; }
// english if ( lastCharCode >= 65 && lastCharCode <= 90 || lastCharCode >= 97 && lastCharCode <= 122 ) { const match = RE_ENG_LAST_TWO.exec(w); if (match) { const suffix2 = match[0].toLowerCase(); const code = engSuffix2Map[suffix2]; if (typeof code === "number") { return code; } return engSuffixMap[suffix2[1]] || 0; } return engCharMap[last.toLowerCase()] ?? 0; }
w = w.slice(0, w.length - 1); }
return 0;}