Sort isUnreachable endpoints to bottom (#5510)

* Sort isUnreachable endpoints to bottom

* sort all levels
This commit is contained in:
Jaco 2021-06-09 01:23:20 +03:00 committed by GitHub
parent f8d25dd233
commit 00f9cf9678
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,6 +4,18 @@
import type { TFunction } from 'i18next';
import type { EndpointOption, LinkOption } from './types';
interface SortOption {
isUnreachable?: boolean;
}
function sortLinks (a: SortOption, b: SortOption): number {
return a.isUnreachable !== b.isUnreachable
? a.isUnreachable
? 1
: -1
: 0;
}
export function expandLinked (input: LinkOption[]): LinkOption[] {
return input.reduce((result: LinkOption[], entry): LinkOption[] => {
result.push(entry);
@ -48,7 +60,10 @@ export function expandEndpoint (t: TFunction, { dnslink, genesisHash, info, isCh
const last = result[result.length - 1];
const options: LinkOption[] = [];
linked.forEach((o) => options.push(...expandEndpoint(t, o, firstOnly)));
linked.sort(sortLinks).forEach((o) =>
options.push(...expandEndpoint(t, o, firstOnly))
);
last.isRelay = true;
last.linked = options;
}
@ -57,5 +72,5 @@ export function expandEndpoint (t: TFunction, { dnslink, genesisHash, info, isCh
}
export function expandEndpoints (t: TFunction, input: EndpointOption[], firstOnly?: boolean): LinkOption[] {
return input.reduce((result: LinkOption[], input) => result.concat(expandEndpoint(t, input, firstOnly)), []);
return input.sort(sortLinks).reduce((result: LinkOption[], input) => result.concat(expandEndpoint(t, input, firstOnly)), []);
}