Refactor domain comparator: split fqdn handling from raw domain

This commit is contained in:
nemunaire 2023-12-11 15:25:25 +01:00
parent ea8a5cdd46
commit 5ff353079d
3 changed files with 27 additions and 4 deletions

View File

@ -19,7 +19,7 @@
import NewDomainInput from '$lib/components/NewDomainInput.svelte';
import ZoneList from '$lib/components/ZoneList.svelte';
import ProviderList from '$lib/components/providers/List.svelte';
import { domainCompare } from '$lib/dns';
import { fqdnCompare } from '$lib/dns';
import type { DomainInList } from '$lib/model/domain';
import type { Provider } from '$lib/model/provider';
import { domains, refreshDomains } from '$lib/stores/domains';
@ -43,7 +43,7 @@
(d) => (!filteredProvider || d.id_provider === filteredProvider._id) &&
(filteredGroup === null || d.group === filteredGroup || ((filteredGroup === '' || filteredGroup === 'undefined') && (d.group === '' || d.group === undefined)))
);
filteredDomains.sort(domainCompare);
filteredDomains.sort(fqdnCompare);
}
}
</script>

View File

@ -14,7 +14,7 @@
import { addDomain } from '$lib/api/domains';
import { listImportableDomains } from '$lib/api/provider';
import ZoneList from '$lib/components/ZoneList.svelte';
import { domainCompare } from '$lib/dns';
import { fqdnCompare } from '$lib/dns';
import type { DomainInList } from '$lib/model/domain';
import type { Provider } from '$lib/model/provider';
import { providersSpecs } from '$lib/stores/providers';
@ -37,7 +37,7 @@
if (l === null) {
importableDomainsList = [];
} else {
l.sort(domainCompare);
l.sort(fqdnCompare);
importableDomainsList = l;
}
},

View File

@ -24,6 +24,29 @@ export function domainCompare (a: string | Domain, b: string | Domain) {
if (!as[0].length) as.shift();
if (!bs[0].length) bs.shift();
const maxDepth = Math.min(as.length, bs.length)
for (let i = 0; i < maxDepth; i++) {
const cmp = as[i].localeCompare(bs[i])
if (cmp !== 0) {
return cmp;
}
}
return as.length - bs.length
}
export function fqdnCompare (a: string | Domain, b: string | Domain) {
// Convert to string if Domain
if (typeof a === "object" && a.domain) a = a.domain;
if (typeof b === "object" && b.domain) b = b.domain;
const as = a.split('.').reverse();
const bs = b.split('.').reverse();
// Remove first item if empty
if (!as[0].length) as.shift();
if (!bs[0].length) bs.shift();
const maxDepth = Math.min(as.length, bs.length)
for (let i = Math.min(maxDepth, 1); i < maxDepth; i++) {
const cmp = as[i].localeCompare(bs[i])