Move Search functions to search.js

This commit is contained in:
Robbie Antenesse 2019-05-03 12:36:46 -06:00
parent dbff3a122e
commit f3fd01f143
3 changed files with 29 additions and 28 deletions

View File

@ -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() {

27
src/js/search.js Normal file
View File

@ -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'), `<mark>${searchTerm}</mark>`);
markedUpWord.simpleDefinition = markedUpWord.simpleDefinition.replace(new RegExp(searchTerm, 'g'), `<mark>${searchTerm}</mark>`);
markedUpWord.longDefinition = markedUpWord.longDefinition.replace(new RegExp(searchTerm, 'g'), `<mark>${searchTerm}</mark>`);
}
return markedUpWord;
}

View File

@ -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'), `<mark>${searchTerm}</mark>`);
markedUpWord.simpleDefinition = markedUpWord.simpleDefinition.replace(new RegExp(searchTerm, 'g'), `<mark>${searchTerm}</mark>`);
markedUpWord.longDefinition = markedUpWord.longDefinition.replace(new RegExp(searchTerm, 'g'), `<mark>${searchTerm}</mark>`);
}
console.log('markedUpWord', markedUpWord);
return markedUpWord;
}