From 8bb24e7afd9a49e30688bdbdea58536816ea4b86 Mon Sep 17 00:00:00 2001 From: zombieFox Date: Sat, 13 Jun 2020 09:25:39 +0100 Subject: [PATCH] [refactor] improve string handling --- src/js/auto-suggest.js | 16 ++++++++-------- src/js/background.js | 2 +- src/js/clock.js | 2 +- src/js/date.js | 2 +- src/js/greeting.js | 2 +- src/js/header.js | 2 +- src/js/helper.js | 4 ++-- src/js/layout.js | 2 +- src/js/link.js | 34 +++++++++++++++++----------------- src/js/search.js | 2 +- src/js/theme.js | 20 ++++++++++---------- 11 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/js/auto-suggest.js b/src/js/auto-suggest.js index ced78d46..4427ef0b 100644 --- a/src/js/auto-suggest.js +++ b/src/js/auto-suggest.js @@ -153,7 +153,7 @@ var autoSuggest = (function() { document.removeEventListener("keydown", bind.navigateResults, false); }, clickOut: function(event) { - if (!(event.target.classList.contains("auto-suggest")) &&!(event.target.classList.contains("auto-suggest-list")) && !(event.target.classList.contains("auto-suggest-input"))) { + if (!(event.target.classList.contains("auto-suggest")) && !(event.target.classList.contains("auto-suggest-list")) && !(event.target.classList.contains("auto-suggest-input"))) { render.close(); }; } @@ -239,29 +239,29 @@ var autoSuggest = (function() { }; render.suggestItems = function() { - var searchTerm = _currentInputOptions.input.value.replace(/^\s+/, "").replace(/\s+$/, "").toLowerCase(); + var searchTerm = helper.trimString(_currentInputOptions.input.value); var action = { fontawesomeIcon: function() { - if (searchTerm == "" || searchTerm == undefined) { - return fontawesome.icons; - } else { + if (helper.checkIfValidString(searchTerm)) { return fontawesome.icons.filter(function(item) { var match = false; - if (item.name.toLowerCase().includes(searchTerm) || item.label.toLowerCase().includes(searchTerm)) { + if (item.name.toLowerCase().includes(searchTerm.toLowerCase()) || item.label.toLowerCase().includes(searchTerm.toLowerCase())) { match = true; }; item.search.forEach(function(item, index) { - if (item.toLowerCase().includes(searchTerm)) { + if (item.toLowerCase().includes(searchTerm.toLowerCase())) { match = true; }; }); item.styles.forEach(function(item, index) { - if (item.toLowerCase().includes(searchTerm)) { + if (item.toLowerCase().includes(searchTerm.toLowerCase())) { match = true; }; }); return match; }); + } else { + return fontawesome.icons; }; } }; diff --git a/src/js/background.js b/src/js/background.js index 61cb2e72..e6c20ca4 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -172,7 +172,7 @@ var background = (function() { render.feedback = { init: function() { - if (helper.checkValueString(state.get.current().background.image.file.name)) { + if (helper.checkIfValidString(state.get.current().background.image.file.name)) { render.feedback.current(); } else { render.feedback.empty(); diff --git a/src/js/clock.js b/src/js/clock.js index aa00832c..a1666bc7 100644 --- a/src/js/clock.js +++ b/src/js/clock.js @@ -112,7 +112,7 @@ var clock = (function() { }; if (state.get.current().header.clock.separator.show) { var separatorCharacter; - if (helper.checkValueString(state.get.current().header.clock.separator.text)) { + if (helper.checkIfValidString(state.get.current().header.clock.separator.text)) { separatorCharacter = helper.trimString(state.get.current().header.clock.separator.text); } else { separatorCharacter = ":"; diff --git a/src/js/date.js b/src/js/date.js index 2469e900..089f3cfe 100644 --- a/src/js/date.js +++ b/src/js/date.js @@ -129,7 +129,7 @@ var date = (function() { }; if (state.get.current().header.date.separator.show) { var separatorCharacter; - if (helper.checkValueString(state.get.current().header.date.separator.text)) { + if (helper.checkIfValidString(state.get.current().header.date.separator.text)) { separatorCharacter = helper.trimString(state.get.current().header.date.separator.text); } else { separatorCharacter = ":"; diff --git a/src/js/greeting.js b/src/js/greeting.js index 150b23e6..9da24504 100644 --- a/src/js/greeting.js +++ b/src/js/greeting.js @@ -37,7 +37,7 @@ var greeting = (function() { } }; var string = message[state.get.current().header.greeting.type](); - if (helper.checkValueString(state.get.current().header.greeting.name)) { + if (helper.checkIfValidString(state.get.current().header.greeting.name)) { string = string + ", " + helper.trimString(state.get.current().header.greeting.name) }; var greetingItem = helper.node("span|class:greeting-item"); diff --git a/src/js/header.js b/src/js/header.js index 8768daff..671b9915 100644 --- a/src/js/header.js +++ b/src/js/header.js @@ -373,7 +373,7 @@ var header = (function() { var headerSearchBody = helper.node("div|class:search-wrapper"); var form = helper.node("form|class:search,action,method:get"); var searchInput; - if (helper.checkValueString(state.get.current().header.search.engine.custom.queryName)) { + if (helper.checkIfValidString(state.get.current().header.search.engine.custom.queryName)) { searchInput = helper.node("input|class:search-input,type:search,placeholder:Find or Search,name:" + helper.trimString(state.get.current().header.search.engine.custom.queryName) + ",autocomplete:off,autocorrect:off,autocapitalize:off,spellcheck:false,tabindex:1"); } else { searchInput = helper.node("input|class:search-input,type:search,placeholder:Find or Search,name:q,autocomplete:off,autocorrect:off,autocapitalize:off,spellcheck:false,tabindex:1"); diff --git a/src/js/helper.js b/src/js/helper.js index e67dfad0..8f06f2ba 100644 --- a/src/js/helper.js +++ b/src/js/helper.js @@ -767,7 +767,7 @@ var helper = (function() { }; }; - var checkValueString = function(value) { + var checkIfValidString = function(value) { var result = false; if (typeof value == "string") { value = value.trim().replace(/\s/g, ""); @@ -812,7 +812,7 @@ var helper = (function() { isJsonString: isJsonString, isHexNumber: isHexNumber, convertColor: convertColor, - checkValueString: checkValueString, + checkIfValidString: checkIfValidString, trimString: trimString }; diff --git a/src/js/layout.js b/src/js/layout.js index 80195eba..5581c739 100644 --- a/src/js/layout.js +++ b/src/js/layout.js @@ -24,7 +24,7 @@ var layout = (function() { render.title = function() { var title = helper.e("title"); - if (helper.checkValueString(state.get.current().layout.title)) { + if (helper.checkIfValidString(state.get.current().layout.title)) { title.textContent = helper.trimString(state.get.current().layout.title); } else { title.textContent = "New tab"; diff --git a/src/js/link.js b/src/js/link.js index 5504c6ea..9529251f 100644 --- a/src/js/link.js +++ b/src/js/link.js @@ -709,7 +709,7 @@ var link = (function() { }); groupHeader.appendChild(groupHeaderItemControl); - if (stagedGroup.group.name.show && helper.checkValueString(stagedGroup.group.name.text)) { + if (stagedGroup.group.name.show && helper.checkIfValidString(stagedGroup.group.name.text)) { helper.addClass(groupHeader, "group-header-name"); groupHeader.appendChild(groupHeaderItemName); }; @@ -916,7 +916,7 @@ var link = (function() { value: "link-item" }] }; - if (stagedLink.link.accent.by == "custom" || stagedLink.link.color.by == "custom" || helper.checkValueString(stagedLink.link.image)) { + if (stagedLink.link.accent.by == "custom" || stagedLink.link.color.by == "custom" || helper.checkIfValidString(stagedLink.link.image)) { linkItemOptions.attr.push({ key: "style", value: "" @@ -940,7 +940,7 @@ var link = (function() { "--link-item-color: " + stagedLink.link.color.rgb.r + ", " + stagedLink.link.color.rgb.g + ", " + stagedLink.link.color.rgb.b + ";" + "--link-item-color-focus-hover: " + stagedLink.link.color.rgb.r + ", " + stagedLink.link.color.rgb.g + ", " + stagedLink.link.color.rgb.b + ";"; }; - if (helper.checkValueString(stagedLink.link.image)) { + if (helper.checkIfValidString(stagedLink.link.image)) { linkItemOptions.attr[1].value = linkItemOptions.attr[1].value + "--link-image-url: url(" + helper.trimString(stagedLink.link.image) + ");" }; }; @@ -999,7 +999,7 @@ var link = (function() { }; var linkDisplayName; - if (helper.checkValueString(stagedLink.link.name)) { + if (helper.checkIfValidString(stagedLink.link.name)) { linkDisplayName = helper.node("p:" + helper.trimString(stagedLink.link.name) + "|class:link-display-name"); } else { linkDisplayName = helper.node("p|class:link-display-name"); @@ -1045,11 +1045,11 @@ var link = (function() { linkDisplay.appendChild(linkDisplayVisual); }; - if (helper.checkValueString(stagedLink.link.name)) { + if (helper.checkIfValidString(stagedLink.link.name)) { linkDisplay.appendChild(linkDisplayName); }; - if (helper.checkValueString(stagedLink.link.image)) { + if (helper.checkIfValidString(stagedLink.link.image)) { linkPanelFront.appendChild(linkImage); }; linkPanelFront.appendChild(linkDisplay); @@ -1556,10 +1556,10 @@ var link = (function() { if (bookmarks.get().length > 0) { bookmarks.get().forEach(function(arrayItem, index) { var name; - if (arrayItem.name.text == null || arrayItem.name.text == "") { - name = "Unnamed group " + (index + 1); - } else { + if (helper.checkIfValidString(arrayItem.name.text)) { name = arrayItem.name.text; + } else { + name = "Unnamed group " + (index + 1); }; var option = helper.makeNode({ tag: "option", @@ -2900,7 +2900,7 @@ var link = (function() { useStagedLink: true }); var heading; - if (helper.checkValueString(stagedLink.link.name)) { + if (helper.checkIfValidString(stagedLink.link.name)) { heading = "Edit " + stagedLink.link.name; } else { heading = "Edit unnamed bookmark"; @@ -2954,10 +2954,10 @@ var link = (function() { useStagedGroup: true }); var heading; - if (stagedGroup.group.name.text == null || stagedGroup.group.name.text == "") { - heading = "Edit unnamed group " + (stagedGroup.position.origin + 1); - } else { + if (helper.checkIfValidString(stagedGroup.group.name.text)) { heading = "Edit " + stagedGroup.group.name.text; + } else { + heading = "Edit unnamed group " + (stagedGroup.position.origin + 1); }; var successAction = function() { var copyStagedGroup = JSON.parse(JSON.stringify(stagedGroup)); @@ -3005,7 +3005,7 @@ var link = (function() { stagedLink.link = JSON.parse(JSON.stringify(copyStagedLink.link)); stagedLink.position = JSON.parse(JSON.stringify(copyStagedLink.position)); var heading; - if (helper.checkValueString(stagedLink.link.name)) { + if (helper.checkIfValidString(stagedLink.link.name)) { heading = "Remove " + stagedLink.link.name; } else { heading = "Remove unnamed bookmark"; @@ -3052,10 +3052,10 @@ var link = (function() { stagedGroup.group = JSON.parse(JSON.stringify(copyStagedGroup.group)); stagedGroup.position = JSON.parse(JSON.stringify(copyStagedGroup.position)); var heading; - if (stagedGroup.group.name.text == null || stagedGroup.group.name.text == "") { - heading = "Remove unnamed group " + (stagedGroup.position.origin + 1); - } else { + if (helper.checkIfValidString(stagedGroup.group.name.text)) { heading = "Remove " + stagedGroup.group.name.text; + } else { + heading = "Remove unnamed group " + (stagedGroup.position.origin + 1); }; var successAction = function() { var copyStagedGroup = JSON.parse(JSON.stringify(stagedGroup)); diff --git a/src/js/search.js b/src/js/search.js index 077c27c4..a8d26408 100644 --- a/src/js/search.js +++ b/src/js/search.js @@ -144,7 +144,7 @@ var search = (function() { render.check = function() { var searchInput = helper.e(".search-input"); - if (helper.checkValueString(searchInput.value)) { + if (helper.checkIfValidString(searchInput.value)) { mod.searching.open(); } else { mod.searching.close(); diff --git a/src/js/theme.js b/src/js/theme.js index 4a5fe4f5..9aab72f2 100644 --- a/src/js/theme.js +++ b/src/js/theme.js @@ -2298,11 +2298,11 @@ var theme = (function() { mod.preset.all.forEach(function(arrayItem, index) { var displayFont = arrayItem.font.display.name + ":" + arrayItem.font.display.weight; var uiFont = arrayItem.font.ui.name + ":" + arrayItem.font.ui.weight; - if (helper.checkValueString(arrayItem.font.display.name) && !allPresetFonts.includes(displayFont)) { + if (helper.checkIfValidString(arrayItem.font.display.name) && !allPresetFonts.includes(displayFont)) { mod.font.loaded.push(arrayItem.font.display.name); allPresetFonts.push(displayFont); }; - if (helper.checkValueString(arrayItem.font.ui.name) && !allPresetFonts.includes(uiFont)) { + if (helper.checkIfValidString(arrayItem.font.ui.name) && !allPresetFonts.includes(uiFont)) { mod.font.loaded.push(arrayItem.font.ui.name); allPresetFonts.push(uiFont); }; @@ -2316,7 +2316,7 @@ var theme = (function() { custom: { display: function() { var displayFont = helper.trimString(state.get.current().theme.font.display.name); - if (!mod.font.loaded.includes(displayFont) && helper.checkValueString(displayFont)) { + if (!mod.font.loaded.includes(displayFont) && helper.checkIfValidString(displayFont)) { mod.font.loaded.push(displayFont); WebFont.load({ google: { @@ -2328,7 +2328,7 @@ var theme = (function() { }, ui: function() { var uiFont = helper.trimString(state.get.current().theme.font.ui.name); - if (!mod.font.loaded.includes(uiFont) && helper.checkValueString(uiFont)) { + if (!mod.font.loaded.includes(uiFont) && helper.checkIfValidString(uiFont)) { mod.font.loaded.push(uiFont); WebFont.load({ google: { @@ -2353,7 +2353,7 @@ var theme = (function() { display: { name: function() { var html = helper.e("html"); - if (helper.checkValueString(state.get.current().theme.font.display.name)) { + if (helper.checkIfValidString(state.get.current().theme.font.display.name)) { html.style.setProperty("--theme-font-display-name", "\"" + helper.trimString(state.get.current().theme.font.display.name) + "\", \"Fjalla One\", sans-serif"); } else { html.style.removeProperty("--theme-font-display-name"); @@ -2372,7 +2372,7 @@ var theme = (function() { ui: { name: function() { var html = helper.e("html"); - if (helper.checkValueString(state.get.current().theme.font.ui.name)) { + if (helper.checkIfValidString(state.get.current().theme.font.ui.name)) { html.style.setProperty("--theme-font-ui-name", "\"" + helper.trimString(state.get.current().theme.font.ui.name) + "\", \"Open Sans\", sans-serif"); } else { html.style.removeProperty("--theme-font-ui-name"); @@ -2456,7 +2456,7 @@ var theme = (function() { themePresetPreview.style.setProperty("--theme-preset-accent", arrayItem.accent.rgb.r + ", " + arrayItem.accent.rgb.g + ", " + arrayItem.accent.rgb.b); themePresetPreview.appendChild(themePresetAccent); - if (helper.checkValueString(arrayItem.name)) { + if (helper.checkIfValidString(arrayItem.name)) { var themePresetName = helper.node("span:" + arrayItem.name + "|class:theme-preset-name"); themePresetPreview.appendChild(themePresetName); }; @@ -2553,7 +2553,7 @@ var theme = (function() { var themeCustomAccent = helper.node("span|class:theme-custom-accent"); themeCustomPreview.style.setProperty("--theme-custom-accent", arrayItem.accent.rgb.r + ", " + arrayItem.accent.rgb.g + ", " + arrayItem.accent.rgb.b); themeCustomPreview.appendChild(themeCustomAccent); - if (helper.checkValueString(arrayItem.name)) { + if (helper.checkIfValidString(arrayItem.name)) { themeCustomPreview.appendChild(helper.node("span:" + helper.trimString(arrayItem.name) + "|class:theme-custom-name")); }; themeCustomButton.appendChild(themeCustomPreview); @@ -2710,7 +2710,7 @@ var theme = (function() { useStagedTheme: true }); var heading; - if (helper.checkValueString(stagedThemeCustom.theme.name)) { + if (helper.checkIfValidString(stagedThemeCustom.theme.name)) { heading = "Edit " + stagedThemeCustom.theme.name; } else { heading = "Edit unnamed theme"; @@ -2750,7 +2750,7 @@ var theme = (function() { stagedThemeCustom.position.index = JSON.parse(JSON.stringify(copyStagedThemeCustom.position.index)); stagedThemeCustom.theme = JSON.parse(JSON.stringify(copyStagedThemeCustom.theme)); var heading; - if (helper.checkValueString(stagedThemeCustom.theme.name)) { + if (helper.checkIfValidString(stagedThemeCustom.theme.name)) { heading = "Remove " + stagedThemeCustom.theme.name; } else { heading = "Remove unnamed theme";