yaejunyang/packages/utils/saferKorean.ts
2026-05-19 15:59:17 +00:00

83 lines
2.7 KiB
TypeScript

import CallingNumberKorean from "./callingNumberKorean";
import FloatKorean from "./floatKorean";
import IntegerKorean from "./integerKorean";
export const IsolatedSymbolMap = {
"?": "물음표",
"!": "느낌표",
"'": "쿼트",
"\"": "더블쿼트",
}
export const SymbolMap = {
"%": "퍼센트",
"$": "달러싸인",
"^": "캐럿",
"&": "엠퍼센드",
"*": "스타",
"#": "해시",
"@": "엣",
".": "쩜",
"-": "마이너스",
"+": "플러스",
"_": "언더바",
"=": "이퀄",
"/": "슬래쉬",
"\\": "역슬래쉬",
"♡": "하투 ",
};
export const VersionPostfix = {
"a": "알파",
"b": "베타",
};
export function saferKorean(input: string): string {
return input.replace(/\.+$/, "")
.replace(/\.\.+/g, "")
.replace(/\.[ \t]/g, " ")
.replace(/^[\?\!\'\"]+$/, (total)=>(
[...total].map(element => IsolatedSymbolMap[
element as keyof typeof IsolatedSymbolMap
]).join("")
))
.replace(/\`\`\`.+?\`\`\`/g, "코드블럭")
.replace(/https\S+/g, "링크")
.replace(/(\d+)[ \t\n]*([개살])/g, (_, num: string, postfix: string)=>{
const intNum = parseInt(num)
if (CallingNumberKorean.canConvert(intNum)) {
return CallingNumberKorean.convert(intNum) + postfix;
} else {
return IntegerKorean.convertFromString(num) + postfix;
}
})
.replace(/(v?)([\d\.]+)([ab]?)/g, (_, suffix: string, num: string, postfix: string) => {
const dotCount = [...num.matchAll(/\./g)].length;
const hasNoSuffix = suffix == "";
if (hasNoSuffix && dotCount == 0) {
return IntegerKorean.convertFromString(num) + postfix;
} else if (hasNoSuffix && dotCount == 1) {
const [intPart, floatPart] = num.split(/\./);
return (
IntegerKorean.convertFromString(intPart)
+ "쩜"
+ FloatKorean.convert(floatPart)
+ postfix
)
} else if (suffix == "v") {
return (
"버전"
+ FloatKorean.convert(num)
+ (VersionPostfix[
postfix as keyof typeof VersionPostfix
] ?? "")
);
} else {
return FloatKorean.convert(num) + postfix;
}
})
.replace(/[\%\^\&\*\#\@\.\-\+\_\=\/\\♡\$]/g, (t) => (
SymbolMap[t as keyof typeof SymbolMap]
))
.replace(/\?+/g, "?")
.replace(/\!+/g, "!")
}