Make renderWords filter by search term
This commit is contained in:
parent
060a78b3e4
commit
7011054487
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue