From 292ff812fec1b6615f103d2c777b3f4e142be5cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Kr=C3=B6g?= Date: Sun, 12 Jul 2020 11:22:59 +0200 Subject: [PATCH] Split searching and listing --- examples/index.js | 129 ++++++++++++++++++++++++---------------------- 1 file changed, 66 insertions(+), 63 deletions(-) diff --git a/examples/index.js b/examples/index.js index eede9227a1..d80ecd926b 100644 --- a/examples/index.js +++ b/examples/index.js @@ -24,78 +24,81 @@ }, 500); } - function filterList(text) { - let examples; + function getMatchingExamples(text) { if (text.length < 2) { - examples = info.examples; - } else { - const words = text.split(/\W+/); - const scores = {}; - for (let i = 0; i < words.length; ++i) { - const word = words[i].toLowerCase(); - let dict = info.index[word]; - const updateScores = function () { - // eslint-disable-next-line prefer-const - for (let exIndex in dict) { - const count = dict[exIndex]; - if (scores[exIndex]) { - if (scores[exIndex][word]) { - scores[exIndex][word] += count; - } else { - scores[exIndex][word] = count; - } + return info.examples; + } + const words = text.split(/\W+/); + const scores = {}; + for (let i = 0; i < words.length; ++i) { + const word = words[i].toLowerCase(); + let dict = info.index[word]; + const updateScores = function () { + // eslint-disable-next-line prefer-const + for (let exIndex in dict) { + const count = dict[exIndex]; + if (scores[exIndex]) { + if (scores[exIndex][word]) { + scores[exIndex][word] += count; } else { - scores[exIndex] = {}; scores[exIndex][word] = count; } + } else { + scores[exIndex] = {}; + scores[exIndex][word] = count; } - }; - if (dict) { - updateScores(); - } else { - const r = new RegExp(word); - // eslint-disable-next-line prefer-const - for (let idx in info.index) { - if (r.test(idx)) { - dict = info.index[idx]; - updateScores(); - } + } + }; + if (dict) { + updateScores(); + } else { + const r = new RegExp(word); + // eslint-disable-next-line prefer-const + for (let idx in info.index) { + if (r.test(idx)) { + dict = info.index[idx]; + updateScores(); } } } - examples = []; - // eslint-disable-next-line prefer-const - for (let j in scores) { - const ex = info.examples[j]; - ex.score = scores[j]; - examples.push(ex); - } - // sort examples by first by number of words matched, then - // by word frequency - examples.sort(function (a, b) { - let cmp; - let aWords = 0, - bWords = 0; - let aScore = 0, - bScore = 0; - // eslint-disable-next-line prefer-const - for (let i in a.score) { - aScore += a.score[i]; - aWords += 1; - } - // eslint-disable-next-line prefer-const - for (let j in b.score) { - bScore += b.score[j]; - bWords += 1; - } - if (aWords == bWords) { - cmp = bScore - aScore; - } else { - cmp = bWords - aWords; - } - return cmp; - }); } + const examples = []; + // eslint-disable-next-line prefer-const + for (let j in scores) { + const ex = info.examples[j]; + ex.score = scores[j]; + examples.push(ex); + } + // sort examples by first by number of words matched, then + // by word frequency + examples.sort(function (a, b) { + let cmp; + let aWords = 0, + bWords = 0; + let aScore = 0, + bScore = 0; + // eslint-disable-next-line prefer-const + for (let i in a.score) { + aScore += a.score[i]; + aWords += 1; + } + // eslint-disable-next-line prefer-const + for (let j in b.score) { + bScore += b.score[j]; + bWords += 1; + } + if (aWords == bWords) { + cmp = bScore - aScore; + } else { + cmp = bWords - aWords; + } + return cmp; + }); + return examples; + } + + function filterList(text) { + const examples = getMatchingExamples(text); listExamples(examples); }