/ src / lib / utils / server.ts
server.ts
 1  /**
 2   * Abbreviates a server name (domain) for display in compact UI elements like clouds or lists.
 3   * 
 4   * Rules:
 5   * 1. If the name has no dots, return the first character.
 6   * 2. If the first part of the domain is 3 chars or less (e.g., 'bne' in 'bne.social'),
 7   *    preserve the full first part and append the abbreviated TLD.
 8   * 3. Otherwise, take the first character of each part except the last,
 9   *    and append the abbreviated TLD (e.g., 'mastodon.social' -> 'm.s').
10   * 4. TLDs of 3 chars or less are shown in full; others are reduced to their first char.
11   * 
12   * @param name The server domain to abbreviate (e.g., 'mastodon.social')
13   * @returns An abbreviated string (e.g., 'm.s')
14   */
15  export function abbreviateServerName(name: string): string {
16  	const parts = name.split('.');
17  	if (parts.length <= 1) return name[0] || '';
18  
19  	const firstPart = parts[0];
20  	const tld = parts.at(-1) || '';
21  	const tldAbbr = tld.length <= 3 ? tld : tld[0];
22  
23  	// If the first part is short, keep it full for clarity (e.g. bne.s)
24  	if (firstPart.length <= 3) {
25  		return `${firstPart}.${tldAbbr}`;
26  	}
27  
28  	// Otherwise, first char of each part except last (e.g. m.s)
29  	return (
30  		parts
31  			.slice(0, -1)
32  			.map((p) => p[0])
33  			.join('.') +
34  		'.' +
35  		tldAbbr
36  	);
37  }