From 027d08cec766b02565a551f60f27f16cefbaaf5f Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sat, 15 Dec 2018 11:03:31 +0100 Subject: [PATCH] Slightly smarter search generated docs --- telethon_generator/data/html/js/search.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/telethon_generator/data/html/js/search.js b/telethon_generator/data/html/js/search.js index 3e24bf0c..6c7f2207 100644 --- a/telethon_generator/data/html/js/search.js +++ b/telethon_generator/data/html/js/search.js @@ -78,32 +78,41 @@ if (typeof prependPath !== 'undefined') { } // Assumes haystack has no whitespace and both are lowercase. +// +// Returns the penalty for finding the needle in the haystack +// or -1 if the needle wasn't found at all. function find(haystack, needle) { if (needle.length == 0) { return true; } var hi = 0; var ni = 0; + var penalty = 0; + var started = false; while (true) { while (needle[ni] < 'a' || needle[ni] > 'z') { ++ni; if (ni == needle.length) { - return true; + return penalty; } } while (haystack[hi] != needle[ni]) { ++hi; + if (started) { + ++penalty; + } if (hi == haystack.length) { - return false; + return -1; } } ++hi; ++ni; + started = true; if (ni == needle.length) { - return true; + return penalty; } if (hi == haystack.length) { - return false; + return -1; } } } @@ -117,7 +126,8 @@ function getSearchArray(original, originalu, query) { var destinationu = []; for (var i = 0; i < original.length; ++i) { - if (find(original[i].toLowerCase(), query)) { + var penalty = find(original[i].toLowerCase(), query); + if (penalty > -1 && penalty < original[i].length / 3) { destination.push(original[i]); destinationu.push(originalu[i]); }