diff options
author | Martin Fischer <martin@push-f.com> | 2025-04-11 16:33:59 +0200 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2025-04-13 20:19:48 +0200 |
commit | 50ea018252ce69542eab6a107b99ea8179810d1e (patch) | |
tree | 25091624110c413bd095581f79b50cd7ab030656 /assets/script.js | |
parent | 16b01fd1dfb8797b5529ea444f5b26cd26be9c3c (diff) |
refactor: introduce lex-serve package
Diffstat (limited to 'assets/script.js')
-rw-r--r-- | assets/script.js | 56 |
1 files changed, 0 insertions, 56 deletions
diff --git a/assets/script.js b/assets/script.js deleted file mode 100644 index 95b8393..0000000 --- a/assets/script.js +++ /dev/null @@ -1,56 +0,0 @@ -const searchInput = document.getElementById('search'); -const suggestionsDiv = document.getElementById('suggestions'); - -async function enableAutocomplete() { - const res = await fetch('/laws.json'); - const laws = await res.json(); - // TODO: strip accents before searching - - searchInput.addEventListener('input', (e) => { - if (searchInput.value == '') { - suggestionsDiv.innerHTML = ''; - return; - } - - const titleRegex = new RegExp(searchInput.value, 'i'); - const abbrRegex = new RegExp('^' + searchInput.value, 'i'); - const suggestions = []; - - laws.map(l => { - const titleMatch = titleRegex.exec(l.title); - const abbrMatch = abbrRegex.exec(l.abbr); - - return { - law: l, - titleScore: titleMatch ? titleMatch.index : Number.MAX_VALUE, - abbrScore: abbrMatch ? abbrMatch.index: Number.MAX_VALUE, - } - }) - .filter(l => l.titleScore < Number.MAX_VALUE || l.abbrScore < Number.MAX_VALUE) - .sort((a,b) => { - let abbrDiff = a.abbrScore - b.abbrScore; - if (a.law.abbr && b.law.abbr && abbrDiff == 0 && a.abbrScore != Number.MAX_VALUE) { - abbrDiff = a.law.abbr.length - b.law.abbr.length; - } - return abbrDiff || a.titleScore - b.titleScore; - }) - .slice(0, 30) - .forEach(x => { - const l = x.law; - const a = document.createElement('a'); - if (l.redir) - a.href = '/' + l.redir; - else - a.href = l.url; - a.textContent = l.title; - const li = document.createElement('li'); - li.appendChild(a); - suggestions.push(li); - }); - - suggestionsDiv.replaceChildren(...suggestions); - }); -} - -if ('json' in searchInput.dataset) - enableAutocomplete();
\ No newline at end of file |