Rollup merge of #110030 - notriddle:notriddle/clean-up-js, r=GuillaumeGomez

rustdoc: clean up JS

* Stop checking `func` in `onEach`. It's always hard-coded right at the call site, so there's no point.
* Use the ternary operator in a few spots where it makes sense.
* No point in making `onEach` store `arr.length` in a variable if it's only used once anyway.
This commit is contained in:
Nilstrieb 2023-04-08 10:26:13 +02:00 committed by GitHub
commit 77639c0947
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 28 deletions

View File

@ -332,13 +332,7 @@ function preLoadCss(cssUrl) {
}; };
function getPageId() { function getPageId() {
if (window.location.hash) { return window.location.hash.replace(/^#/, "");
const tmp = window.location.hash.replace(/^#/, "");
if (tmp.length > 0) {
return tmp;
}
}
return null;
} }
const toggleAllDocsId = "toggle-all-docs"; const toggleAllDocsId = "toggle-all-docs";
@ -707,7 +701,7 @@ function preLoadCss(cssUrl) {
}); });
const pageId = getPageId(); const pageId = getPageId();
if (pageId !== null) { if (pageId !== "") {
expandSection(pageId); expandSection(pageId);
} }
}()); }());

View File

@ -86,12 +86,8 @@
if (settingId === "theme") { if (settingId === "theme") {
const useSystem = getSettingValue("use-system-theme"); const useSystem = getSettingValue("use-system-theme");
if (useSystem === "true" || settingValue === null) { if (useSystem === "true" || settingValue === null) {
if (useSystem !== "false") { // "light" is the default theme
settingValue = "system preference"; settingValue = useSystem === "false" ? "light" : "system preference";
} else {
// This is the default theme.
settingValue = "light";
}
} }
} }
if (settingValue !== null && settingValue !== "null") { if (settingValue !== null && settingValue !== "null") {

View File

@ -53,10 +53,9 @@ function removeClass(elem, className) {
* @param {boolean} [reversed] - Whether to iterate in reverse * @param {boolean} [reversed] - Whether to iterate in reverse
*/ */
function onEach(arr, func, reversed) { function onEach(arr, func, reversed) {
if (arr && arr.length > 0 && func) { if (arr && arr.length > 0) {
if (reversed) { if (reversed) {
const length = arr.length; for (let i = arr.length - 1; i >= 0; --i) {
for (let i = length - 1; i >= 0; --i) {
if (func(arr[i])) { if (func(arr[i])) {
return true; return true;
} }
@ -150,26 +149,18 @@ const updateTheme = (function() {
* dictates that it should be. * dictates that it should be.
*/ */
function updateTheme() { function updateTheme() {
const use = (theme, saveTheme) => {
switchTheme(theme, saveTheme);
};
// maybe the user has disabled the setting in the meantime! // maybe the user has disabled the setting in the meantime!
if (getSettingValue("use-system-theme") !== "false") { if (getSettingValue("use-system-theme") !== "false") {
const lightTheme = getSettingValue("preferred-light-theme") || "light"; const lightTheme = getSettingValue("preferred-light-theme") || "light";
const darkTheme = getSettingValue("preferred-dark-theme") || "dark"; const darkTheme = getSettingValue("preferred-dark-theme") || "dark";
if (mql.matches) { // use light theme if user prefers it, or has no preference
use(darkTheme, true); switchTheme(mql.matches ? darkTheme : lightTheme, true);
} else {
// prefers a light theme, or has no preference
use(lightTheme, true);
}
// note: we save the theme so that it doesn't suddenly change when // note: we save the theme so that it doesn't suddenly change when
// the user disables "use-system-theme" and reloads the page or // the user disables "use-system-theme" and reloads the page or
// navigates to another page // navigates to another page
} else { } else {
use(getSettingValue("theme"), false); switchTheme(getSettingValue("theme"), false);
} }
} }