From 4dc25e6fc639d25aafef02f66efffc0af4688e80 Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Sun, 22 Aug 2021 13:35:57 +0100 Subject: [PATCH 01/10] :sparkles: Wrote a better search algo --- src/utils/Search.js | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/utils/Search.js 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 */ + +/* 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; From 11f38c9177640a81f7e437f41b5a7f110d870589 Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Sun, 22 Aug 2021 13:36:54 +0100 Subject: [PATCH 02/10] :zap: Implements improved search --- src/views/Home.vue | 69 +++++++++++++++++++------------------------ src/views/Minimal.vue | 12 ++------ 2 files changed, 32 insertions(+), 49 deletions(-) diff --git a/src/views/Home.vue b/src/views/Home.vue index b13c818e..a157ddc9 100644 --- a/src/views/Home.vue +++ b/src/views/Home.vue @@ -1,3 +1,4 @@ +