-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
40 lines (34 loc) · 1.72 KB
/
Copy pathscript.js
File metadata and controls
40 lines (34 loc) · 1.72 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
(function () {
'use strict';
/** Normalizes human-readable text so searches are case- and accent-insensitive. */
function normalizeText(value) {
return value.normalize('NFD').replace(/[\u0300-\u036f]/g, '').toLocaleLowerCase('pt-BR');
}
function filterPublications(publications, query) {
const normalizedQuery = normalizeText(query.trim());
return publications.map((publication) => {
const matches = !normalizedQuery || normalizeText(publication.textContent).includes(normalizedQuery);
publication.hidden = !matches;
return matches;
}).filter(Boolean).length;
}
function publicationCountLabel(count, language = 'en') {
const isPortuguese = language.toLocaleLowerCase().startsWith('pt');
if (isPortuguese) return `${count} ${count === 1 ? 'publicação' : 'publicações'}`;
return `${count} ${count === 1 ? 'publication' : 'publications'}`;
}
function init() {
const search = document.querySelector('#publication-search');
const publications = Array.from(document.querySelectorAll('[data-publication]'));
const status = document.querySelector('#results-status');
const emptyState = document.querySelector('.no-results');
document.querySelector('#current-year').textContent = new Date().getFullYear();
search.addEventListener('input', () => {
const count = filterPublications(publications, search.value);
status.textContent = publicationCountLabel(count, document.documentElement.lang);
emptyState.hidden = count !== 0;
});
}
if (typeof module !== 'undefined') module.exports = { normalizeText, filterPublications, publicationCountLabel };
if (typeof document !== 'undefined') document.addEventListener('DOMContentLoaded', init);
}());