simplify filterAndSort() js function.

This commit is contained in:
fiatjaf
2024-11-27 00:53:00 -03:00
committed by dtonon
parent b1e8f1fade
commit 4597a54e84

View File

@@ -119,30 +119,30 @@ function getPlatform() {
function filterAndSort(children) { function filterAndSort(children) {
const platform = getPlatform(); const platform = getPlatform();
const filteredElements = Array.from(children).filter((element) => { const parent = children[0].parentNode;
const platformAttr = element.getAttribute('data-platform'); const elements = []
return platform === platformAttr || platformAttr === 'web' || platformAttr === 'native' || platformAttr === 'dummy'; for (let i = 0; i < children.length; i++) {
}); let element = children[i]
switch (element.getAttribute('data-platform')) {
Array.from(children).forEach((element) => { case platform:
if (!filteredElements.includes(element)) { case 'web':
element.remove(); case 'native':
case 'dummy':
elements.push(element)
}
} }
});
const sortedElements = filteredElements.sort( elements.sort(
(a, b) => (a, b) =>
parseInt(b.getAttribute('count')) - parseInt(a.getAttribute('count')) parseInt(b.getAttribute('count')) - parseInt(a.getAttribute('count'))
); );
// Assuming you want to re-insert the sorted elements into their parent container parent.innerHTML = ''
const parent = children[0].parentNode; for (let i = 0; i < elements.length; i++) {
parent.innerHTML = ''; // Clear the parent container parent.appendChild(elements[i]);
sortedElements.forEach((element) => { };
parent.appendChild(element);
});
return sortedElements; return elements;
} }
</script> </script>
} }