From 7011054487bc0ca18a340abdafe6598e0d710992 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Fri, 3 May 2019 12:02:46 -0600 Subject: [PATCH] Make renderWords filter by search term --- src/js/render.js | 4 ++-- src/js/utilities.js | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/js/render.js b/src/js/render.js index ab8ed87..33ba482 100644 --- a/src/js/render.js +++ b/src/js/render.js @@ -1,6 +1,6 @@ import md from 'snarkdown'; import { removeTags } from '../helpers'; -import { getWordsStats, wordExists } from './utilities'; +import { getWordsStats, wordExists, getMatchingSearchWords } from './utilities'; import { showSection } from './displayToggles'; export function renderAll() { @@ -92,7 +92,7 @@ export function renderPartsOfSpeechSelect() { } export function renderWords() { - const { words } = window.currentDictionary; + const words = getMatchingSearchWords(); let wordsHTML = ''; words.forEach(word => { let detailsMarkdown = removeTags(word.longDefinition); diff --git a/src/js/utilities.js b/src/js/utilities.js index f3ecba0..ed6ae61 100644 --- a/src/js/utilities.js +++ b/src/js/utilities.js @@ -97,4 +97,15 @@ export function wordExists(word, returnId = false) { return caseSensitive ? existingWord.name === word : existingWord.name.toLowerCase() === word.toLowerCase(); }); return foundWord ? (returnId ? foundWord.wordId : true) : false; +} + +export function getMatchingSearchWords() { + const searchTerm = document.getElementById('searchButton').value.trim(); + const matchingWords = window.currentDictionary.words.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; } \ No newline at end of file