-
Notifications
You must be signed in to change notification settings - Fork 0
/
abbrsearch.js
37 lines (28 loc) · 1.08 KB
/
abbrsearch.js
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
import { abbreviationsMap } from "./abbreviations.js";
export function init(searchElement, resultsElement) {
const update = () => updateAbbreviationLookupResults(resultsElement);
searchElement.addEventListener("input", update);
update();
}
function updateAbbreviationLookupResults(resultsElement) {
// clear all previous lookup results
//
// might be pretty slow, see also
// https://stackoverflow.com/questions/3955229/remove-all-child-elements-of-a-dom-node-in-javascript
resultsElement.innerHTML = "";
for (const [abbrKey, abbrValue] of fuzzyFindAbbreviations(abbrSearch.value)) {
const result = document.createElement("li");
result.appendChild(document.createTextNode(`${abbrKey}\t${abbrValue}`));
abbrLookupResults.appendChild(result);
}
}
function fuzzyFindAbbreviations(str) {
str = str.toLowerCase();
const matches = [];
for (const [abbrKey, abbrValue] of abbreviationsMap.entries()) {
if (abbrKey.toLowerCase().includes(str) || abbrValue.toLowerCase().includes(str)) {
matches.push([abbrKey, abbrValue]);
}
}
return matches;
}