Make renderWords filter by search term
This commit is contained in:
parent
060a78b3e4
commit
7011054487
|
@ -1,6 +1,6 @@
|
||||||
import md from 'snarkdown';
|
import md from 'snarkdown';
|
||||||
import { removeTags } from '../helpers';
|
import { removeTags } from '../helpers';
|
||||||
import { getWordsStats, wordExists } from './utilities';
|
import { getWordsStats, wordExists, getMatchingSearchWords } from './utilities';
|
||||||
import { showSection } from './displayToggles';
|
import { showSection } from './displayToggles';
|
||||||
|
|
||||||
export function renderAll() {
|
export function renderAll() {
|
||||||
|
@ -92,7 +92,7 @@ export function renderPartsOfSpeechSelect() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function renderWords() {
|
export function renderWords() {
|
||||||
const { words } = window.currentDictionary;
|
const words = getMatchingSearchWords();
|
||||||
let wordsHTML = '';
|
let wordsHTML = '';
|
||||||
words.forEach(word => {
|
words.forEach(word => {
|
||||||
let detailsMarkdown = removeTags(word.longDefinition);
|
let detailsMarkdown = removeTags(word.longDefinition);
|
||||||
|
|
|
@ -98,3 +98,14 @@ export function wordExists(word, returnId = false) {
|
||||||
});
|
});
|
||||||
return foundWord ? (returnId ? foundWord.wordId : true) : false;
|
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