Move Search functions to search.js
This commit is contained in:
parent
dbff3a122e
commit
f3fd01f143
|
@ -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() {
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue