Files
ncd-fe/packages/nicolium/src/utils/nyaize.ts
nicole mikołajczyk b88a638e25 nicolium rename stuff
Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
2026-02-27 01:04:14 +01:00

55 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Adapted from Sharkey, licensed under AGPL-3.0-only
// https://activitypub.software/TransFem-org/Sharkey/-/blob/develop/packages/misskey-js/src/nyaize.ts
const koRegex1 = /[나-낳]/g;
const koRegex2 = /(다$)|(다(?=\.))|(다(?= ))|(다(?=!))|(다(?=\?))/gm;
const koRegex3 = /(야(?=\?))|(야$)|(야(?= ))/gm;
const ifAfter = (prefix: string, fn: (x: string) => string) => {
const preLen = prefix.length;
const regex = new RegExp(prefix, 'i');
return (x: string, pos: number, string: string) => {
return pos > 0 && string.slice(pos - preLen, pos).match(regex) ? fn(x) : x;
};
};
const nyaize = (text: string) =>
text
// ja-JP
.replaceAll('な', 'にゃ')
.replaceAll('ナ', 'ニャ')
.replaceAll('ナ', 'ニャ')
// en-US
.replaceAll(
'a',
ifAfter('n', (x) => (x === 'A' ? 'YA' : 'ya')),
)
.replaceAll(
'ing',
ifAfter('morn', (x) => (x === 'ING' ? 'YAN' : 'yan')),
)
.replaceAll(
'one',
ifAfter('every', (x) => (x === 'ONE' ? 'NYAN' : 'nyan')),
)
// pl-PL
.replaceAll(
'ł',
ifAfter('mia', (x) => (x === 'Ł' ? 'U' : 'u')),
)
// ru-RU
.replaceAll(
'а',
ifAfter('н', (x) => (x === 'А' ? 'Я' : 'я')),
)
// ko-KR
.replace(koRegex1, (match) =>
!isNaN(match.charCodeAt(0))
? String.fromCharCode(match.charCodeAt(0) + '냐'.charCodeAt(0) - '나'.charCodeAt(0))
: match,
)
.replace(koRegex2, '다냥')
.replace(koRegex3, '냥');
export default nyaize;