From f3fd01f143554e4b82d9651a2de2bbaefcd23bfb Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Fri, 3 May 2019 12:36:46 -0600 Subject: [PATCH] Move Search functions to search.js --- src/js/render.js | 3 ++- src/js/search.js | 27 +++++++++++++++++++++++++++ src/js/utilities.js | 27 --------------------------- 3 files changed, 29 insertions(+), 28 deletions(-) create mode 100644 src/js/search.js diff --git a/src/js/render.js b/src/js/render.js index fb28431..c1aa019 100644 --- a/src/js/render.js +++ b/src/js/render.js @@ -1,6 +1,7 @@ import md from 'snarkdown'; import { removeTags } from '../helpers'; -import { getWordsStats, wordExists, getMatchingSearchWords, highlightSearchTerm } from './utilities'; +import { getWordsStats, wordExists } from './utilities'; +import { getMatchingSearchWords, highlightSearchTerm } from './search'; import { showSection } from './displayToggles'; export function renderAll() { diff --git a/src/js/search.js b/src/js/search.js new file mode 100644 index 0000000..43f8280 --- /dev/null +++ b/src/js/search.js @@ -0,0 +1,27 @@ +import { cloneObject } from "../helpers"; + +export function getSearchTerm() { + return document.getElementById('searchButton').value; +} + +export function getMatchingSearchWords() { + const searchTerm = getSearchTerm(); + const matchingWords = window.currentDictionary.words.slice().filter(word => { + const isInName = new RegExp(searchTerm, 'g').test(word.name); + const isInDefinition = new RegExp(searchTerm, 'g').test(word.simpleDefinition); + const isInDetails = new RegExp(searchTerm, 'g').test(word.longDefinition); + return isInName || isInDefinition || isInDetails; + }); + return matchingWords; +} + +export function highlightSearchTerm(word) { + const searchTerm = getSearchTerm(); + const markedUpWord = cloneObject(word); + if (searchTerm) { + markedUpWord.name = markedUpWord.name.replace(new RegExp(searchTerm, 'g'), `${searchTerm}`); + markedUpWord.simpleDefinition = markedUpWord.simpleDefinition.replace(new RegExp(searchTerm, 'g'), `${searchTerm}`); + markedUpWord.longDefinition = markedUpWord.longDefinition.replace(new RegExp(searchTerm, 'g'), `${searchTerm}`); + } + return markedUpWord; +} \ No newline at end of file diff --git a/src/js/utilities.js b/src/js/utilities.js index e4c8961..7ae88c6 100644 --- a/src/js/utilities.js +++ b/src/js/utilities.js @@ -98,30 +98,3 @@ export function wordExists(word, returnId = false) { }); return foundWord ? (returnId ? foundWord.wordId : true) : false; } - -export function getSearchTerm() { - return document.getElementById('searchButton').value; -} - -export function getMatchingSearchWords() { - const searchTerm = getSearchTerm(); - const matchingWords = window.currentDictionary.words.slice().filter(word => { - const isInName = new RegExp(searchTerm, 'g').test(word.name); - const isInDefinition = new RegExp(searchTerm, 'g').test(word.simpleDefinition); - const isInDetails = new RegExp(searchTerm, 'g').test(word.longDefinition); - return isInName || isInDefinition || isInDetails; - }); - return matchingWords; -} - -export function highlightSearchTerm(word) { - const searchTerm = getSearchTerm(); - const markedUpWord = cloneObject(word); - if (searchTerm) { - markedUpWord.name = markedUpWord.name.replace(new RegExp(searchTerm, 'g'), `${searchTerm}`); - markedUpWord.simpleDefinition = markedUpWord.simpleDefinition.replace(new RegExp(searchTerm, 'g'), `${searchTerm}`); - markedUpWord.longDefinition = markedUpWord.longDefinition.replace(new RegExp(searchTerm, 'g'), `${searchTerm}`); - } - console.log('markedUpWord', markedUpWord); - return markedUpWord; -} \ No newline at end of file