refactor: updateURL function for improved maintainability (@anthonypz) (#5817)

* refactor: updateURL function

* refactor: rename val to getVal and replace forEach loop with for of
This commit is contained in:
Anthony Perez 2024-08-23 10:48:46 -07:00 committed by GitHub
parent e2d574444a
commit 8863fb70d1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -27,58 +27,37 @@ type SharedTestSettings = [
function updateURL(): void {
const baseUrl = location.origin + "?testSettings=";
const settings: SharedTestSettings = [
null,
null,
null,
null,
null,
null,
null,
null,
const settings: SharedTestSettings = new Array(8).fill(
null
) as SharedTestSettings;
const settingsMap = [
{ key: "mode", getValue: () => Config.mode },
{ key: "mode2", getValue: () => getMode2(Config, currentQuote) },
{ key: "customText", getValue: () => CustomText.getData() },
{ key: "punctuation", getValue: () => Config.punctuation },
{ key: "numbers", getValue: () => Config.numbers },
{ key: "language", getValue: () => Config.language },
{ key: "difficulty", getValue: () => Config.difficulty },
{ key: "funbox", getValue: () => Config.funbox },
];
if (getCheckboxValue("mode")) {
settings[0] = Config.mode;
}
if (getCheckboxValue("mode2")) {
settings[1] = getMode2(Config, currentQuote);
}
if (getCheckboxValue("customText")) {
settings[2] = CustomText.getData();
}
if (getCheckboxValue("punctuation")) {
settings[3] = Config.punctuation;
}
if (getCheckboxValue("numbers")) {
settings[4] = Config.numbers;
}
if (getCheckboxValue("language")) {
settings[5] = Config.language;
}
if (getCheckboxValue("difficulty")) {
settings[6] = Config.difficulty;
}
if (getCheckboxValue("funbox")) {
settings[7] = Config.funbox;
for (const [index, { key, getValue }] of settingsMap.entries()) {
if (getCheckboxValue(key)) {
settings[index] = getValue();
}
}
const compressed = compressToURI(JSON.stringify(settings));
const url = baseUrl + compressed;
$(`#shareTestSettingsModal textarea.url`).val(url);
if (url.length > 2000) {
$(`#shareTestSettingsModal .tooLongWarning`).removeClass("hidden");
} else {
$(`#shareTestSettingsModal .tooLongWarning`).addClass("hidden");
}
updateShareModal(url);
}
function updateShareModal(url: string): void {
const $modal = $(`#shareTestSettingsModal`);
$modal.find("textarea.url").val(url);
$modal.find(".tooLongWarning").toggleClass("hidden", url.length <= 2000);
}
function updateSubgroups(): void {