Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

:sparkles: Adds helper functions for search bangs

Alicia Sykes55beeaf010c7
diff --git a/src/utils/Search.js b/src/utils/Search.js
index e50b88bf..f03078bc 100644
--- a/src/utils/Search.js
+++ b/src/utils/Search.js
@@ -1,6 +1,7 @@
/* Dashy: Licensed under MIT, (C) Alicia Sykes 2021 <https://aliciasykes.com> */
/* Tile filtering utility */
+import ErrorHandler from '@/utils/ErrorHandler';
/**
* Extracts the site name from domain
@@ -35,7 +36,7 @@ const filterHelper = (compareStr, searchStr) => {
* @param {string} searchTerm The users search term
* @returns A filtered array of tiles
*/
-const search = (allTiles, searchTerm) => {
+export const searchTiles = (allTiles, searchTerm) => {
if (!allTiles) return []; // If no data, then skip
return allTiles.filter((tile) => {
const {
@@ -49,4 +50,30 @@ const search = (allTiles, searchTerm) => {
});
};
-export default search;
+/* From a list of search bangs, return the URL associated with it */
+export const getSearchEngineFromBang = (searchQuery, bangList) => {
+ const bangNames = Object.keys(bangList);
+ const foundBang = bangNames.find((bang) => searchQuery.includes(bang));
+ return bangList[foundBang];
+};
+
+/* For a given search engine key, return the corresponding URL, or throw error */
+export const findUrlForSearchEngine = (searchEngine, availableSearchEngines) => {
+ // If missing search engine, report error return false
+ if (!searchEngine) { ErrorHandler('No search engine specified'); return undefined; }
+ // If search engine is already a URL, then return it
+ if ((/(http|https):\/\/[^]*/).test(searchEngine)) return searchEngine;
+ // If search engine was found successfully, return the URL
+ if (availableSearchEngines[searchEngine]) return availableSearchEngines[searchEngine];
+ // Otherwise, there's been an error, log it and return false
+ ErrorHandler(`Specified Search Engine was not Found: '${searchEngine}'`);
+ return undefined;
+};
+
+/* Removes all known bangs from a search query */
+export const stripBangs = (searchQuery, bangList) => {
+ const bangNames = Object.keys(bangList || {});
+ let q = searchQuery;
+ bangNames.forEach((bang) => { q = q.replace(bang, ''); });
+ return q.trim();
+};