Prettier code

This also fixes two undeclared variables and the regex is only created once per word
This commit is contained in:
Maximilian Krög
2020-07-12 11:13:39 +02:00
parent 681fc155a7
commit dc05cacefa
2 changed files with 51 additions and 43 deletions
+50 -42
View File
@@ -1,42 +1,45 @@
(function () { (function () {
var template, target; 'use strict';
/* global info, jugl */
let template, target;
function listExamples(examples) { function listExamples(examples) {
target.innerHTML = ""; target.innerHTML = '';
var node = template.process({ template.process({
context: {examples: examples}, context: {examples: examples},
clone: true, clone: true,
parent: target parent: target,
}); });
document.getElementById("count").innerHTML = "(" + examples.length + ")"; document.getElementById('count').innerHTML = '(' + examples.length + ')';
} }
var timerId; let timerId;
function inputChange() { function inputChange() {
if (timerId) { if (timerId) {
window.clearTimeout(timerId); window.clearTimeout(timerId);
} }
var text = this.value; const text = this.value;
timerId = window.setTimeout(function() { timerId = window.setTimeout(function () {
filterList(text); filterList(text);
}, 500); }, 500);
} }
function filterList(text) { function filterList(text) {
var examples; let examples;
if (text.length < 2) { if (text.length < 2) {
examples = info.examples; examples = info.examples;
} else { } else {
var words = text.split(/\W+/); const words = text.split(/\W+/);
var scores = {}; const scores = {};
for(var i=0; i<words.length; ++i) { for (let i = 0; i < words.length; ++i) {
var word = words[i].toLowerCase(); const word = words[i].toLowerCase();
var dict = info.index[word]; let dict = info.index[word];
var updateScores = function() { const updateScores = function () {
for(exIndex in dict) { // eslint-disable-next-line prefer-const
var count = dict[exIndex]; for (let exIndex in dict) {
if(scores[exIndex]) { const count = dict[exIndex];
if(scores[exIndex][word]) { if (scores[exIndex]) {
if (scores[exIndex][word]) {
scores[exIndex][word] += count; scores[exIndex][word] += count;
} else { } else {
scores[exIndex][word] = count; scores[exIndex][word] = count;
@@ -50,9 +53,9 @@
if (dict) { if (dict) {
updateScores(); updateScores();
} else { } else {
var r; const r = new RegExp(word);
for (idx in info.index) { // eslint-disable-next-line prefer-const
r = new RegExp(word); for (let idx in info.index) {
if (r.test(idx)) { if (r.test(idx)) {
dict = info.index[idx]; dict = info.index[idx];
updateScores(); updateScores();
@@ -61,22 +64,27 @@
} }
} }
examples = []; examples = [];
for (var j in scores) { // eslint-disable-next-line prefer-const
var ex = info.examples[j]; for (let j in scores) {
const ex = info.examples[j];
ex.score = scores[j]; ex.score = scores[j];
examples.push(ex); examples.push(ex);
} }
// sort examples by first by number of words matched, then // sort examples by first by number of words matched, then
// by word frequency // by word frequency
examples.sort(function(a, b) { examples.sort(function (a, b) {
var cmp; let cmp;
var aWords = 0, bWords = 0; let aWords = 0,
var aScore = 0, bScore = 0; bWords = 0;
for (var i in a.score) { let aScore = 0,
bScore = 0;
// eslint-disable-next-line prefer-const
for (let i in a.score) {
aScore += a.score[i]; aScore += a.score[i];
aWords += 1; aWords += 1;
} }
for (var j in b.score) { // eslint-disable-next-line prefer-const
for (let j in b.score) {
bScore += b.score[j]; bScore += b.score[j];
bWords += 1; bWords += 1;
} }
@@ -92,29 +100,29 @@
} }
function parseQuery() { function parseQuery() {
var params = {}; const params = {};
var list = window.location.search.substring(1).split("&"); const list = window.location.search.substring(1).split('&');
for (var i = 0; i < list.length; ++i) { for (let i = 0; i < list.length; ++i) {
var pair = list[i].split("="); const pair = list[i].split('=');
if (pair.length == 2) { if (pair.length == 2) {
params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]); params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
} }
} }
if (params["q"]) { if (params['q']) {
var input = document.getElementById("keywords"); const input = document.getElementById('keywords');
input.value = params["q"]; input.value = params['q'];
inputChange.call(input); inputChange.call(input);
} }
} }
window.onload = function() { window.onload = function () {
for (var i = 0; i < info.examples.length; ++i) { for (let i = 0; i < info.examples.length; ++i) {
info.examples[i].link += window.location.search; info.examples[i].link += window.location.search;
} }
template = new jugl.Template("template"); template = new jugl.Template('template');
target = document.getElementById("examples"); target = document.getElementById('examples');
listExamples(info.examples); listExamples(info.examples);
document.getElementById("keywords").onkeyup = inputChange; document.getElementById('keywords').onkeyup = inputChange;
parseQuery(); parseQuery();
}; };
})(); })();
+1 -1
View File
@@ -175,7 +175,7 @@ class ExampleBuilder {
tags: Array.from(uniqueTags), tags: Array.from(uniqueTags),
}; };
const indexSource = `var info = ${JSON.stringify(info)}`; const indexSource = `const info = ${JSON.stringify(info)};`;
compilation.assets['examples-info.js'] = new RawSource(indexSource); compilation.assets['examples-info.js'] = new RawSource(indexSource);
}); });
} }