refactor: move function

This commit is contained in:
Miodec 2024-05-15 15:40:01 +02:00
parent 4fbdaa0e6f
commit 527cd5b690
3 changed files with 30 additions and 25 deletions

View file

@ -4,7 +4,7 @@ import * as ManualRestart from "../test/manual-restart-tracker";
import * as TestLogic from "../test/test-logic";
import * as ChallengeController from "../controllers/challenge-controller";
import Config, * as UpdateConfig from "../config";
import * as Misc from "../utils/misc";
import * as Strings from "../utils/strings";
import * as WordFilterPopup from "./word-filter";
import * as Notifications from "../elements/notifications";
import * as SavedTextsPopup from "./saved-texts";
@ -277,7 +277,7 @@ function cleanUpText(): string[] {
text = text.replace(/ +/gm, " ");
text = text.replace(/( *(\r\n|\r|\n) *)/g, "\n ");
if (state.removeFancyTypographyEnabled) {
text = Misc.cleanTypographySymbols(text);
text = Strings.cleanTypographySymbols(text);
}
if (state.replaceNewlines !== "off") {

View file

@ -179,29 +179,6 @@ export function escapeHTML(str: string): string {
return str;
}
export function cleanTypographySymbols(textToClean: string): string {
const specials = {
"“": '"', // “ “
"”": '"', // ” ”
"„": '"', // „ „
"": "'", // ‘ ‘
"": "'", // ’ ’
",": ",", // ‚ ‚
"—": "-", // — —
"…": "...", // … …
"«": "<<",
"»": ">>",
"": "-",
"": " ",
"": " ",
" ": " ",
};
return textToClean.replace(
/[“”’‘—,…«»–\u2007\u202F\u00A0]/g,
(char) => specials[char as keyof typeof specials] || ""
);
}
export function isUsernameValid(name: string): boolean {
if (name === null || name === undefined || name === "") return false;
if (name.toLowerCase().includes("miodec")) return false;

View file

@ -121,3 +121,31 @@ export function getLanguageDisplayString(
export function removeLanguageSize(language: string): string {
return language.replace(/_\d*k$/g, "");
}
/**
* Removes fancy typography symbols from a string.
* @param textToClean
* @returns Cleaned text.
*/
export function cleanTypographySymbols(textToClean: string): string {
const specials = {
"“": '"', // &ldquo; &#8220;
"”": '"', // &rdquo; &#8221;
"„": '"', // &bdquo; &#8222;
"": "'", // &lsquo; &#8216;
"": "'", // &rsquo; &#8217;
",": ",", // &sbquo; &#8218;
"—": "-", // &mdash; &#8212;
"…": "...", // &hellip; &#8230;
"«": "<<",
"»": ">>",
"": "-",
"": " ",
"": " ",
" ": " ",
};
return textToClean.replace(
/[“”’‘—,…«»–\u2007\u202F\u00A0]/g,
(char) => specials[char as keyof typeof specials] || ""
);
}