Slightly smarter search generated docs

This commit is contained in:
Lonami Exo 2018-12-15 11:03:31 +01:00
parent 52b179dba8
commit 027d08cec7

View File

@ -78,32 +78,41 @@ if (typeof prependPath !== 'undefined') {
} }
// Assumes haystack has no whitespace and both are lowercase. // 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) { function find(haystack, needle) {
if (needle.length == 0) { if (needle.length == 0) {
return true; return true;
} }
var hi = 0; var hi = 0;
var ni = 0; var ni = 0;
var penalty = 0;
var started = false;
while (true) { while (true) {
while (needle[ni] < 'a' || needle[ni] > 'z') { while (needle[ni] < 'a' || needle[ni] > 'z') {
++ni; ++ni;
if (ni == needle.length) { if (ni == needle.length) {
return true; return penalty;
} }
} }
while (haystack[hi] != needle[ni]) { while (haystack[hi] != needle[ni]) {
++hi; ++hi;
if (started) {
++penalty;
}
if (hi == haystack.length) { if (hi == haystack.length) {
return false; return -1;
} }
} }
++hi; ++hi;
++ni; ++ni;
started = true;
if (ni == needle.length) { if (ni == needle.length) {
return true; return penalty;
} }
if (hi == haystack.length) { if (hi == haystack.length) {
return false; return -1;
} }
} }
} }
@ -117,7 +126,8 @@ function getSearchArray(original, originalu, query) {
var destinationu = []; var destinationu = [];
for (var i = 0; i < original.length; ++i) { 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]); destination.push(original[i]);
destinationu.push(originalu[i]); destinationu.push(originalu[i]);
} }