2023-10-19 02:42:17 +00:00
|
|
|
/*
|
2024-02-13 15:59:27 +00:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-10-19 02:42:17 +00:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-11-04 10:45:37 +00:00
|
|
|
const koRegex1 = /[나-낳]/g;
|
|
|
|
const koRegex2 = /(다$)|(다(?=\.))|(다(?= ))|(다(?=!))|(다(?=\?))/gm;
|
|
|
|
const koRegex3 = /(야(?=\?))|(야$)|(야(?= ))/gm;
|
|
|
|
|
2024-02-20 20:00:22 +00:00
|
|
|
function ifAfter(prefix, fn) {
|
|
|
|
const preLen = prefix.length;
|
2024-04-19 13:42:58 +00:00
|
|
|
const regex = new RegExp(prefix, 'i');
|
|
|
|
return (x, pos, string) => {
|
|
|
|
return pos > 0 && string.substring(pos - preLen, pos).match(regex) ? fn(x) : x;
|
2024-02-20 20:00:22 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-10-19 02:42:17 +00:00
|
|
|
export function nyaize(text: string): string {
|
|
|
|
return text
|
|
|
|
// ja-JP
|
|
|
|
.replaceAll('な', 'にゃ').replaceAll('ナ', 'ニャ').replaceAll('ナ', 'ニャ')
|
|
|
|
// en-US
|
2024-02-20 20:00:22 +00:00
|
|
|
.replace(/a/gi, ifAfter('n', x => x === 'A' ? 'YA' : 'ya'))
|
|
|
|
.replace(/ing/gi, ifAfter('morn', x => x === 'ING' ? 'YAN' : 'yan'))
|
|
|
|
.replace(/one/gi, ifAfter('every', x => x === 'ONE' ? 'NYAN' : 'nyan'))
|
2023-10-19 02:42:17 +00:00
|
|
|
// ko-KR
|
2023-11-04 10:45:37 +00:00
|
|
|
.replace(koRegex1, match => String.fromCharCode(
|
2024-04-19 13:42:58 +00:00
|
|
|
match.charCodeAt(0) + '냐'.charCodeAt(0) - '나'.charCodeAt(0),
|
2023-10-19 02:42:17 +00:00
|
|
|
))
|
2023-11-04 10:45:37 +00:00
|
|
|
.replace(koRegex2, '다냥')
|
|
|
|
.replace(koRegex3, '냥');
|
2023-10-19 02:42:17 +00:00
|
|
|
}
|