Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor getQueryParameters() to use URL/URLSearchParams #218

Closed
shaedrich opened this issue May 15, 2024 · 7 comments · Fixed by #222
Closed

Refactor getQueryParameters() to use URL/URLSearchParams #218

shaedrich opened this issue May 15, 2024 · 7 comments · Fixed by #222

Comments

@shaedrich
Copy link
Contributor

shaedrich commented May 15, 2024

Would it make sense to refactor getQueryParameters() to use URL/URLSearchParams, so we don't have to maintain custom code over native one or do you prefer not to fix something that ain't broken?

function getQueryParameters() {
var a = window.location.search.substr(1).split('&');
if (a === "") return {};
var b = {};
for (var i = 0; i < a.length; i++) {
var p = a[i].split('=');
if (p.length != 2) continue;
b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}
return b;
}

By the way, this

function optionalLocalStorageGetItem(key) {
try {
return localStorage.getItem(key);
} catch (e) {
return null;
}

could also be rewritten as

function optionalLocalStorageGetItem(key) {
    return localStorage.getItem(key) ?? null;
}

and so can

theme = optionalLocalStorageGetItem("theme");
if (theme === null) {
set_theme(editor, themelist, "GitHub");
} else {
set_theme(editor, themelist, theme);
}

as

theme = optionalLocalStorageGetItem("theme");
set_theme(editor, themelist, theme ?? "GitHub");

as well as

var themes = themelist.themes;
themes.sort(function (a, b) {
if (a.caption < b.caption) {
return -1;
} else if (a.caption > b.caption) {
return 1;
}
return 0;
});

which can be shortened to

var themes = themelist.themes;
themes.sort((a, b) => a.caption.localeCompare(b.caption));

Then, we have

var classes = mappings[arguments[1]];
if (classes) {
return '<span class="' + classes + '">' + arguments[2] + '</span>';
} else {
return arguments[2];
}

which doesn't need the else, since it has a return:

var classes = mappings[arguments[1]];
if (classes) {
    return '<span class="' + classes + '">' + arguments[2] + '</span>';
}
return arguments[2];

The following is an interesting case:

if (on_success) {
on_success(req.responseText);
}
} else {
if (on_fail) {
on_fail(req.status, req.responseText);
}
}

it could either be rewritten as

if (req.status == expect) {
    if (on_success) {
        on_success(req.responseText);
    }
} else if (on_fail) {
    on_fail(req.status, req.responseText);
}

or

if (req.status == expect && on_success) {
    on_success(req.responseText);
}
if (req.status != expect && on_fail) {
    on_fail(req.status, req.responseText);
}

Next, we have a bunch of these:

asmButton.onclick = function () {
compile("asm", result, session.getValue(), asmButton);
};
irButton.onclick = function () {
compile("llvm-ir", result, session.getValue(), irButton);
};
gistButton.onclick = function () {
shareGist(result, session.getValue(), gistButton);
};

when they only contain a single function call, instead of creating a bunch of overhead with new anonymous wrapper functions, you could use bind:

asmButton.onclick = compile.bind(window, "asm", result, session.getValue(), asmButton);

irButton.onclick = compile.bind(window, "llvm-ir", result, session.getValue(), irButton);

gistButton.onclick = shareGist.bind(window, result, session.getValue(), gistButton);

clearResultButton.onclick = clear_result.bind(window, result);

themes.onkeyup = themes.onchange = set_theme.bind(window, editor, themelist, themes.options[themes.selectedIndex].text);

request.ontimeout = set_result.bind(window, result, "<p class=error>Connection timed out" +
                "<p class=error-explanation>Are you connected to the Internet?");

One could possibly also refactor editGo(), editShowRegion(), editShowLine(), and editShowPoint(), but that'd be slightly more than a simplification.

Miscellaneous

@ponylang-main ponylang-main added the discuss during sync Should be discussed during an upcoming sync label May 15, 2024
@jemc
Copy link
Member

jemc commented May 21, 2024

@mfelsche - are we still pulling in updated code from the Rust playground that we lifted from (in which case it may not make sense to make a bunch of maintainability-focused changes to the javascript code) or are we pretty decoupled from that upstream repo now (in which case it may indeed make sense to make iterative small improvements)?

@mfelsche
Copy link
Contributor

@jemc we didnt update it from the original codebase for ages. I didnt do any work on the frontend bits, but would be happy to get those on stable feet. Happy for every improvement here.

@shaedrich
Copy link
Contributor Author

Am I missing something or is samples obsolete and can be removed?

var samples = 2;

@mfelsche
Copy link
Contributor

It doesn't seem to be referenced. Your js language server of choice might know more through some static analysis.

@shaedrich
Copy link
Contributor Author

Would you like to have JSDoc comments above your functions?

@mfelsche
Copy link
Contributor

Those aren't my functions. They belong to everyone. Treat them like they are yours. Whatever improves code quality, Iwe are happy to accept. I can write Rust and Pony, but I don't really dare to touch javascript.
So I hereby hand ownership of the JS code in here to you. It is old and hungry and copied and only slightly adapted from the rust playground it was forked from. Feel free to improve in any way you deem helpful (mostly to you doing further improvements).

@shaedrich
Copy link
Contributor Author

Those aren't my functions. They belong to everyone.

Yeah, sorry, bad wording on my part 😅 I didn't mean singular "you". More like that the repository owners might have a little more say in this

Treat them like they are yours. Whatever improves code quality, Iwe are happy to accept. […] So I hereby hand ownership of the JS code in here to you. […] Feel free to improve in any way you deem helpful (mostly to you doing further improvements).

Perfect 👍🏻 Will do.

@jemc jemc closed this as completed in #222 May 25, 2024
@ponylang-main ponylang-main removed the discuss during sync Should be discussed during an upcoming sync label May 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants