Files
teacherpick/scripts/hangul-roman.js
T
2026-11-10 10:12:09 +09:00

58 lines
2.0 KiB
JavaScript

// 국립국어원 표준 한글 -> 영문 로마자 변환 유틸
const CHO = ['g', 'kk', 'n', 'd', 'tt', 'r', 'm', 'b', 'pp', 's', 'ss', '', 'j', 'jj', 'ch', 'k', 't', 'p', 'h'];
const JUNG = ['a', 'ae', 'ya', 'yae', 'eo', 'e', 'yeo', 'ye', 'o', 'wa', 'wae', 'oe', 'yo', 'u', 'wo', 'we', 'wi', 'yu', 'eu', 'ui', 'i'];
const JONG = ['', 'k', 'k', 'ks', 'n', 'nj', 'nh', 't', 'l', 'lg', 'lm', 'lb', 'ls', 'lt', 'lp', 'lh', 'm', 'p', 'bs', 's', 'ss', 'ng', 'j', 'ch', 'k', 't', 'p', 'h'];
export function hangulToRoman(str) {
if (!str) return '';
let res = '';
for (let i = 0; i < str.length; i++) {
const code = str.charCodeAt(i);
if (code >= 0xac00 && code <= 0xd7a3) {
const syl = code - 0xac00;
const c = Math.floor(syl / 588);
const j = Math.floor((syl % 588) / 28);
const jg = syl % 28;
res += CHO[c] + JUNG[j] + JONG[jg];
} else if (/[a-zA-Z0-9]/.test(str[i])) {
res += str[i].toLowerCase();
}
}
return res;
}
export function toDongSlug(dongName) {
const clean = dongName.trim();
if (clean.endsWith('동')) {
const base = hangulToRoman(clean.slice(0, -1));
return `${base}-dong`;
}
if (clean.endsWith('읍')) {
const base = hangulToRoman(clean.slice(0, -1));
return `${base}-eup`;
}
if (clean.endsWith('면')) {
const base = hangulToRoman(clean.slice(0, -1));
return `${base}-myeon`;
}
if (clean.endsWith('가')) {
const base = hangulToRoman(clean.slice(0, -1));
return `${base}-ga`;
}
if (clean.endsWith('로')) {
const base = hangulToRoman(clean.slice(0, -1));
return `${base}-ro`;
}
return hangulToRoman(clean);
}
export function toSigunguSlug(sigunguName) {
const parts = sigunguName.trim().split(' ');
return parts.map(p => {
if (p.endsWith('구')) return `${hangulToRoman(p.slice(0, -1))}-gu`;
if (p.endsWith('시')) return `${hangulToRoman(p.slice(0, -1))}-si`;
if (p.endsWith('군')) return `${hangulToRoman(p.slice(0, -1))}-gun`;
return hangulToRoman(p);
}).join('-');
}