Put more weight on the base name

This commit is contained in:
Maximilian Krög
2020-02-15 15:52:04 +01:00
parent b613e8e77f
commit 15beb1f2c6

View File

@@ -18,37 +18,36 @@ $(function () {
function getWeightFunction(searchTerm, allowRegex) { function getWeightFunction(searchTerm, allowRegex) {
function makeRe(searchTerm) { function makeRe(searchTerm) {
return { return {
begin: new RegExp('(?:[.~]|\\b)' + searchTerm, 'g'), // Also match . and ~ to demote double matches, in weight function begin: new RegExp('\\b' + searchTerm), // Begin matches word boundary
name: new RegExp('\\b' + searchTerm + '(?:[~.]|$)'), // Match until module end or name end baseName: new RegExp('\\b' + searchTerm + '[^/]*$'), // Begin matches word boundary of class / module name
fullName: new RegExp('^' + searchTerm + '$') fullName: new RegExp('\\b' + searchTerm + '(?:[~.]|$)'), // Complete word(s) of class / module matches
completeName: new RegExp('^' + searchTerm + '$') // Match from start to finish
} }
} }
const re = constructRegex(searchTerm, makeRe, allowRegex); const re = constructRegex(searchTerm, makeRe, allowRegex);
return function (matchedItem, beginOnly) { return function (matchedItem, beginOnly) {
// We could get smarter on the weight here // We could get smarter on the weight here
let weight = 0;
const name = matchedItem.data('name'); const name = matchedItem.data('name');
const match = name.match(re.begin); if (beginOnly) {
if (match) { return re.baseName.test(name) ? 10000 : 0;
// `ol/geom/Geometry` matches twice when searching 'geom', is weighted higher. }
// but don't boost something like `ol/source/Vector~VectorSource` when searching for 'Vect'. let weight = 0;
let matches = match.length; if (name.match(re.begin)) {
if (matches > 1 && /[.~]/.test(match[matches - 1])) { weight += 10000;
matches -= 1; if (re.baseName.test(name)) {
} weight += 1000000;
weight += matches * 10000;
if (!beginOnly) {
// Full match of the last part of the path is weighted even higher
// Complete match is weighted highest.
if (re.fullName.test(name)) { if (re.fullName.test(name)) {
// Full match of the last part of the path is weighted even higher
weight += 10000000; weight += 10000000;
} else if (re.name.test(name)) { if (re.completeName.test(name)) {
weight += 1000000; // Complete match is weighted highest.
weight += 100000000;
}
} }
} }
// If everything else is equal, prefer shorter names.
weight -= name.length;
} }
// If everything else is equal, prefer shorter names.
weight -= name.length;
return weight; return weight;
} }
} }