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