Make renderWords filter by search term

This commit is contained in:
Robbie Antenesse 2019-05-03 12:02:46 -06:00
parent 060a78b3e4
commit 7011054487
2 changed files with 13 additions and 2 deletions

View File

@ -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);

View File

@ -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;
}