summaryrefslogtreecommitdiff
path: root/packages
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2023-11-04 19:45:37 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2023-11-04 19:45:37 +0900
commitfc0ea0ddacda082528f6aafe78297bf8e592bcdc (patch)
treee2c184b2f567fc036c51e303cfe9a36aa0dfad50 /packages
parent2023.11.0-beta.10 (diff)
downloadsharkey-fc0ea0ddacda082528f6aafe78297bf8e592bcdc.tar.gz
sharkey-fc0ea0ddacda082528f6aafe78297bf8e592bcdc.tar.bz2
sharkey-fc0ea0ddacda082528f6aafe78297bf8e592bcdc.zip
perf(frontend): improve nyaize performance
Diffstat (limited to 'packages')
-rw-r--r--packages/frontend/src/scripts/nyaize.ts19
1 files changed, 13 insertions, 6 deletions
diff --git a/packages/frontend/src/scripts/nyaize.ts b/packages/frontend/src/scripts/nyaize.ts
index 0ac77e1006..62833b4de3 100644
--- a/packages/frontend/src/scripts/nyaize.ts
+++ b/packages/frontend/src/scripts/nyaize.ts
@@ -3,18 +3,25 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
+const enRegex1 = /(?<=n)a/gi;
+const enRegex2 = /(?<=morn)ing/gi;
+const enRegex3 = /(?<=every)one/gi;
+const koRegex1 = /[나-낳]/g;
+const koRegex2 = /(다$)|(다(?=\.))|(다(?= ))|(다(?=!))|(다(?=\?))/gm;
+const koRegex3 = /(야(?=\?))|(야$)|(야(?= ))/gm;
+
export function nyaize(text: string): string {
return text
// ja-JP
.replaceAll('な', 'にゃ').replaceAll('ナ', 'ニャ').replaceAll('ナ', 'ニャ')
// en-US
- .replace(/(?<=n)a/gi, x => x === 'A' ? 'YA' : 'ya')
- .replace(/(?<=morn)ing/gi, x => x === 'ING' ? 'YAN' : 'yan')
- .replace(/(?<=every)one/gi, x => x === 'ONE' ? 'NYAN' : 'nyan')
+ .replace(enRegex1, x => x === 'A' ? 'YA' : 'ya')
+ .replace(enRegex2, x => x === 'ING' ? 'YAN' : 'yan')
+ .replace(enRegex3, x => x === 'ONE' ? 'NYAN' : 'nyan')
// ko-KR
- .replace(/[나-낳]/g, match => String.fromCharCode(
+ .replace(koRegex1, match => String.fromCharCode(
match.charCodeAt(0)! + '냐'.charCodeAt(0) - '나'.charCodeAt(0),
))
- .replace(/(다$)|(다(?=\.))|(다(?= ))|(다(?=!))|(다(?=\?))/gm, '다냥')
- .replace(/(야(?=\?))|(야$)|(야(?= ))/gm, '냥');
+ .replace(koRegex2, '다냥')
+ .replace(koRegex3, '냥');
}