1
Fork 0

Make unknown links sorted at the bottom.

This commit is contained in:
Bauke 2022-01-07 22:51:31 +01:00
parent e3faf966df
commit aeb3baaca2
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 10 additions and 1 deletions

View File

@ -18,6 +18,7 @@ const known: KnownLink[] = knownLinks.map((data: Record<string, unknown>) => ({
})); }));
export default class RelationLink { export default class RelationLink {
public readonly isKnown: boolean;
public readonly link: URL; public readonly link: URL;
public readonly original: string; public readonly original: string;
public readonly text: string; public readonly text: string;
@ -27,6 +28,7 @@ export default class RelationLink {
this.link = new URL(relationUrl); this.link = new URL(relationUrl);
const knownLink = known.find(({regex}) => regex.test(this.link.host)); const knownLink = known.find(({regex}) => regex.test(this.link.host));
this.isKnown = knownLink !== undefined;
this.text = knownLink?.text ?? this.link.host; this.text = knownLink?.text ?? this.link.host;
} }
} }

View File

@ -58,7 +58,14 @@ export default class Release {
); );
const links = Array.from(relations) const links = Array.from(relations)
.map((url) => new RelationLink(url)) .map((url) => new RelationLink(url))
.sort((a, b) => a.text.localeCompare(b.text)); .sort((a, b) =>
// Sort links that aren't known to us at the bottom.
a.isKnown === b.isKnown
? a.text.localeCompare(b.text)
: b.isKnown
? 1 // This return 1 or -1 is because .sort() expects a number.
: -1,
);
return new Release({ return new Release({
artist, artist,