Skip to content

Commit

Permalink
improve popup search category heuristics
Browse files Browse the repository at this point in the history
  • Loading branch information
tophf committed May 21, 2024
1 parent 9f52bab commit 495f9b4
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions popup/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* @prop {string} sn - screenshotName
* @prop {boolean} sa - screenshotArchived
*
* @prop {number} _bias - sort bias: higher value is a worse match
* @prop {number} _styleId - installed style id
* @prop {boolean} _styleVars - installed style has vars
* @prop {number} _year
Expand All @@ -48,6 +49,7 @@
let results, resultsAllYears;
/** @type IndexEntry[] */
let index;
let host3 = '';
let category = '';
/** @type RegExp */
let rxCategory;
Expand Down Expand Up @@ -534,7 +536,9 @@
fourth ||
third && third !== 'www' && third !== 'm'
);
category = (keepThird && `${third}.` || '') + main + (keepTld || keepThird ? `.${tld}` : '');
const etld = keepTld || keepThird ? `.${tld}` : '';
category = (keepThird && `${third}.` || '') + main + etld;
if (!host3) host3 = keepThird && category;
}
rxCategory = new RegExp(`\\b${stringAsRegExpStr(category)}\\b`, 'i');
return category !== old;
Expand Down Expand Up @@ -594,14 +598,21 @@

function isResultMatching(res) {
const {c} = res;
return (
let bias;
return (bias =
c === category ||
host3 && (
c.includes('.')
? host3.charCodeAt(host3.length - c.length - 1) === 46/*.*/ && host3.endsWith(c)
: host3.includes(`.${c}.`)
) && 2 + !res.n.includes(host3) ||
(category === STYLUS_CATEGORY
? c === 'stylus' // USW
: c === 'global' && searchGlobals &&
(query.length || rxCategory.test(res.n))
)
) && query.every(isInHaystack, res);
) && query.every(isInHaystack, res)
&& (res._bias = bias);
}

/**
Expand All @@ -617,7 +628,7 @@
* @param {IndexEntry} b
*/
function comparator(a, b) {
return (
return a._bias - b._bias || (
order === 'n'
? a.n < b.n ? -1 : a.n > b.n
: b[order] - a[order]
Expand Down

0 comments on commit 495f9b4

Please sign in to comment.