client/misc: add matchingNames function and fall back to partial matches

This commit is contained in:
Eva
2025-04-04 10:23:49 +02:00
parent ffd62a936f
commit 0d2df6db95
3 changed files with 17 additions and 8 deletions

View File

@ -10,8 +10,7 @@ function _poolListToMatches(text, pools, options) {
return pool2.postCount - pool1.postCount;
})
.map((pool) => {
pool.matchingNames = pool.names.filter((name) => misc.wildcardMatch(text + "*", name, false));
pool.matchingNames = pool.matchingNames.length ? pool.matchingNames : pool.names;
pool.matchingNames = misc.matchingNames(text, pool.names);
let cssName = misc.makeCssName(pool.category, "pool");
const caption =
'<span class="' +
@ -64,8 +63,7 @@ class PoolAutoCompleteControl extends AutoCompleteControl {
}
const result = this._results[this._activeResult].value;
const textToFind = this._options.getTextToFind();
result.matchingNames = result.names.filter((name) => misc.wildcardMatch(textToFind + "*", name, false));
result.matchingNames = result.matchingNames.length ? result.matchingNames : result.names;
result.matchingNames = misc.matchingNames(textToFind, result.names);
return result;
}
}

View File

@ -11,8 +11,7 @@ function _tagListToMatches(text, tags, options, negated) {
return tag2.usages - tag1.usages;
})
.map((tag) => {
tag.matchingNames = tag.names.filter((name) => misc.wildcardMatch(text + "*", name, false));
tag.matchingNames = tag.matchingNames.length ? tag.matchingNames : tag.names;
tag.matchingNames = misc.matchingNames(text, tag.names);
let cssName = misc.makeCssName(tag.category, "tag");
if (options.isTaggedWith(tag.names[0])) {
cssName += " disabled";
@ -87,8 +86,7 @@ class TagAutoCompleteControl extends AutoCompleteControl {
}
const result = this._results[this._activeResult].value;
const textToFind = this._options.getTextToFind();
result.matchingNames = result.names.filter((name) => misc.wildcardMatch(textToFind + "*", name, false));
result.matchingNames = result.matchingNames.length ? result.matchingNames : result.names;
result.matchingNames = misc.matchingNames(textToFind, result.names);
return result;
}
}

View File

@ -217,6 +217,18 @@ function wildcardMatch(pattern, str, sensitive = false) {
return re.test(str);
}
function matchingNames(text, names) {
const minLengthForPartialSearch = 3;
let matches = names.filter((name) => wildcardMatch(text + "*", name, false));
if (!matches.length && text.length >= minLengthForPartialSearch) {
matches = names.filter((name) => wildcardMatch("*" + text + "*", name, false));
}
matches = matches.length ? matches : names;
return matches;
}
module.exports = {
range: range,
formatRelativeTime: formatRelativeTime,
@ -236,4 +248,5 @@ module.exports = {
dataURItoBlob: dataURItoBlob,
getPrettyName: getPrettyName,
wildcardMatch: wildcardMatch,
matchingNames: matchingNames,
};