2023-10-19 04:42:17 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-10-19 04:42:17 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-11-04 11:45:37 +01:00
|
|
|
const koRegex1 = /[나-낳]/g;
|
|
|
|
const koRegex2 = /(다$)|(다(?=\.))|(다(?= ))|(다(?=!))|(다(?=\?))/gm;
|
|
|
|
const koRegex3 = /(야(?=\?))|(야$)|(야(?= ))/gm;
|
|
|
|
|
2024-10-09 16:17:22 +02:00
|
|
|
function ifAfter(prefix: string, fn: (x: string) => string) {
|
2024-02-20 21:00:22 +01:00
|
|
|
const preLen = prefix.length;
|
2024-04-19 15:42:58 +02:00
|
|
|
const regex = new RegExp(prefix, 'i');
|
2024-10-09 16:17:22 +02:00
|
|
|
return (x: string, pos: number, string: string) => {
|
2024-04-19 15:42:58 +02:00
|
|
|
return pos > 0 && string.substring(pos - preLen, pos).match(regex) ? fn(x) : x;
|
2024-02-20 21:00:22 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-10-19 04:42:17 +02:00
|
|
|
export function nyaize(text: string): string {
|
|
|
|
return text
|
|
|
|
// ja-JP
|
|
|
|
.replaceAll('な', 'にゃ').replaceAll('ナ', 'ニャ').replaceAll('ナ', 'ニャ')
|
|
|
|
// en-US
|
2024-02-20 21:00:22 +01: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 04:42:17 +02:00
|
|
|
// ko-KR
|
2024-09-09 13:57:36 +02:00
|
|
|
.replace(koRegex1, match => !isNaN(match.charCodeAt(0)) ? String.fromCharCode(
|
2024-04-19 15:42:58 +02:00
|
|
|
match.charCodeAt(0) + '냐'.charCodeAt(0) - '나'.charCodeAt(0),
|
2024-09-09 13:57:36 +02:00
|
|
|
) : match)
|
2023-11-04 11:45:37 +01:00
|
|
|
.replace(koRegex2, '다냥')
|
|
|
|
.replace(koRegex3, '냥');
|
2023-10-19 04:42:17 +02:00
|
|
|
}
|