Repository navigation
Expand file tree
/
Copy pathsearch-utils.js
More file actions
71 lines (59 loc) · 2.13 KB
/
Copy pathsearch-utils.js
File metadata and controls
71 lines (59 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const DIACRITICS_RE = /[\u0300-\u036f]/g;
const NON_ALNUM_RE = /[^a-z0-9]+/g;
export function normaliseSearchText(value) {
return String(value ?? "")
.normalize("NFKD")
.replace(DIACRITICS_RE, "")
.toLowerCase()
.replace(NON_ALNUM_RE, " ")
.trim()
.replace(/\s+/g, " ");
}
export function getSearchTokens(query) {
const normalised = normaliseSearchText(query);
return normalised ? normalised.split(" ") : [];
}
export function matchesSearchQuery(fields, query) {
const tokens = getSearchTokens(query);
if (tokens.length === 0) return true;
const haystack = normaliseSearchText(fields.join(" "));
return tokens.every((token) => haystack.includes(token));
}
function scoreField(field, token, weight) {
if (!field || !field.includes(token)) return 0;
let score = weight;
if (field === token) score += weight * 0.75;
if (field.startsWith(token)) score += weight * 0.5;
if ((" " + field + " ").includes(" " + token + " ")) score += weight * 0.25;
return score;
}
export function scoreSearchItem(item, query) {
const tokens = getSearchTokens(query);
if (tokens.length === 0) return 0;
const fields = {
name: normaliseSearchText(item.name),
description: normaliseSearchText(item.description ?? item.desc),
category: normaliseSearchText(item.cat || item.catLabel || ""),
tags: normaliseSearchText((item.tags || []).join(" ")),
};
return tokens.reduce((total, token) => {
const nameScore = scoreField(fields.name, token, 100);
const categoryScore = scoreField(fields.category, token, 65);
const tagScore = scoreField(fields.tags, token, 60);
const descriptionScore = scoreField(fields.description, token, 25);
return total + nameScore + categoryScore + tagScore + descriptionScore;
}, 0);
}
export function rankSearchItems(items, query) {
const tokens = getSearchTokens(query);
if (tokens.length === 0) return items.slice();
return items
.map((item, index) => ({
item,
index,
score: scoreSearchItem(item, query),
}))
.filter(({ score }) => score > 0)
.sort((a, b) => b.score - a.score || a.index - b.index)
.map(({ item }) => item);
}