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: Wrote a better search algo

Alicia Sykes4dc25e6fc639
diff --git a/src/utils/Search.js b/src/utils/Search.js
new file mode 100644
index 00000000..e50b88bf
--- /dev/null
+++ b/src/utils/Search.js
@@ -0,0 +1,52 @@
+/* Dashy: Licensed under MIT, (C) Alicia Sykes 2021 <https://aliciasykes.com> */
+
+/* Tile filtering utility */
+
+/**
+ * Extracts the site name from domain
+ * @param {string} url The URL to process
+ * @returns {string} The hostname from URL
+ */
+const getDomainFromUrl = (url) => {
+ if (!url) return '';
+ const urlPattern = /^(?:https?:\/\/)?(?:w{3}\.)?([a-z\d.-]+)\.(?:[a-z.]{2,10})(?:[/\w.-]*)*/;
+ const domainPattern = url.match(urlPattern);
+ return domainPattern ? domainPattern[1] : '';
+};
+
+/**
+ * Compares search term to a given data attribute
+ * Ignores case, special characters and order
+ * @param {string or other} compareStr The value to compare to
+ * @param {string} searchStr The users search term
+ * @returns {boolean} true if a match, otherwise false
+ */
+const filterHelper = (compareStr, searchStr) => {
+ if (!compareStr) return false;
+ const process = (input) => input.toString().toLowerCase().replace(/[^\w\s]/gi, '');
+ return process(compareStr).includes(process(searchStr));
+};
+
+/**
+ * Filter tiles based on users search term, and returns a filtered list
+ * Will match based on title, description, provider, hostname from url and tags
+ * Ignores case, special characters and other irrelevant things
+ * @param {array} allTiles An array of tiles
+ * @param {string} searchTerm The users search term
+ * @returns A filtered array of tiles
+ */
+const search = (allTiles, searchTerm) => {
+ if (!allTiles) return []; // If no data, then skip
+ return allTiles.filter((tile) => {
+ const {
+ title, description, provider, url, tags,
+ } = tile;
+ return filterHelper(title, searchTerm)
+ || filterHelper(provider, searchTerm)
+ || filterHelper(description, searchTerm)
+ || filterHelper(tags, searchTerm)
+ || filterHelper(getDomainFromUrl(url), searchTerm);
+ });
+};
+
+export default search;