diff options
author | johannst <johannes.stoelp@gmail.com> | 2021-05-11 21:49:13 +0200 |
---|---|---|
committer | johannst <johannes.stoelp@gmail.com> | 2021-05-12 00:03:09 +0200 |
commit | d854b9c47bced5067b4e75f18f6f3068431ccdcf (patch) | |
tree | aa28c0984934f6a9d27d6d8979c79b1b05b0ac3b /static | |
parent | 53c9ba85bf18e53a7d998c048d10e578b5a19983 (diff) | |
download | blog-d854b9c47bced5067b4e75f18f6f3068431ccdcf.tar.gz blog-d854b9c47bced5067b4e75f18f6f3068431ccdcf.zip |
simplify one-line lambdas, pull input out of context
Diffstat (limited to 'static')
-rw-r--r-- | static/search.js | 32 |
1 files changed, 14 insertions, 18 deletions
diff --git a/static/search.js b/static/search.js index 02f1814..be67f2d 100644 --- a/static/search.js +++ b/static/search.js @@ -11,12 +11,12 @@ const formatResultPreview = (body, terms) => { // Update global index into body. body_idx += word.length + 1 /* for the ' ' split char */; - if (terms.some(term => { return word.startsWith(term); })) { + if (terms.some(term => word.startsWith(term))) { return { idx : idx, end : idx + word.length }; } else { return null; } - }).filter(match => { return match !== null; }); + }).filter(match => match !== null); // Format preview, by making each term bold and adding a short slice // following after the term. @@ -38,9 +38,7 @@ const formatResult = (result, terms) => { + "</div>"; } -const evaluteSearch = (ctx) => { - let term = ctx.input.value.trim(); - +const evaluteSearchTerm = (term, ctx) => { // Nothing todo if current search term is the same as last one. if (term === ctx.last_search) { return; @@ -91,21 +89,19 @@ const debounce = (func, wait) => { } window.onload = () => { - let ctx = new class { - constructor() { - // Get HTML DOM elements. - this.input = document.getElementById("search"); - this.results = document.getElementById("search-results"); - this.results_items = document.getElementById("search-results-items"); - this.pages = document.getElementById("pages"); - // Load search index. - this.search_index = elasticlunr.Index.load(window.searchIndex); - // Last search term. - this.last_search = ""; - } + let ctx = { + // Get HTML DOM elements. + results : document.getElementById("search-results"), + results_items : document.getElementById("search-results-items"), + pages : document.getElementById("pages"), + // Load search index. + search_index : elasticlunr.Index.load(window.searchIndex), + // Last search term. + last_search : null, }; if (ctx.search_index) { - ctx.input.onkeyup = debounce(() => { evaluteSearch(ctx); }, 200); + const input = document.getElementById("search"); + input.onkeyup = debounce(() => evaluteSearchTerm(input.value.trim(), ctx), 200); } } |