Repository: th-ch/youtube-music
File: src/plugins/discord/utils.ts (TypeScript)

Code before:

/**
 * Truncates a string to a specified length, adding ellipsis if truncated.
 * @param str - The string to truncate.
 * @param length - The maximum allowed length.
 * @returns The truncated string.
 */
export const truncateString = (str: string, length: number): string => {
  if (str.length > length) {
    return `${str.substring(0, length - 3)}...`;
  }
  return str;
};

/**
 * Sanitizes a string for Discord Rich Presence activity, ensuring it meets length requirements.
 * @param input - The string to sanitize.
 * @param fallback - A fallback string to use if the input is empty or whitespace. Defaults to 'undefined'.
 * @returns The sanitized string, compliant with Discord's requirements.
 */
export function sanitizeActivityText(input: string | undefined, fallback: string = 'undefined'): string {
  const text = (input && input.trim()) ? input.trim() : fallback.trim();
  let safeString = truncateString(text, 128);

  if (safeString.length === 0) {
    return 'undefined'; // change if necessary
  }

  if (safeString.length < 2) {
    safeString = safeString.padEnd(2, '⠀'); // change if necessary
  }

  return safeString;
}

/**
 * Builds the array of buttons for the Discord Rich Presence activity.
 * @param config - The plugin configuration.
 * @param songInfo - The current song information.
 * @returns An array of buttons or undefined if no buttons are configured.
 */
export const buildDiscordButtons = (
  config: DiscordPluginConfig,
  songInfo: SongInfo,
): GatewayActivityButton[] | undefined => {
  const buttons: GatewayActivityButton[] = [];
  if (config.playOnPearDesktop && songInfo.url) {
    buttons.push({
      label: 'Play on Pear Desktop',
      url: songInfo.url,
    });

Review comment (suggestion):
Any reason to not use the existing fallback?

```suggestion
    return fallback;
```

Code after:

/**
 * Truncates a string to a specified length, adding ellipsis if truncated.
 * @param str - The string to truncate.
 * @param length - The maximum allowed length.
 * @returns The truncated string.
 */
export const truncateString = (str: string, length: number): string => {
  if (str.length > length) {
    return `${str.substring(0, length - 3)}...`;
  }
  return str;
};

/**
 * Sanitizes a string for Discord Rich Presence activity, ensuring it meets length requirements.
 * @param input - The string to sanitize.
 * @param fallback - A fallback string to use if the input is empty or whitespace. Defaults to 'undefined'.
 * @returns The sanitized string, compliant with Discord's requirements.
 */
export function sanitizeActivityText(input: string | undefined, fallback: string = 'undefined'): string {
  const text = (input && input.trim()) ? input.trim() : fallback.trim();
  let safeString = truncateString(text, 128);

  if (safeString.length === 0) {
    return fallback;
  }

  if (safeString.length < 2) {
    safeString = safeString.padEnd(2, '⠀'); // change if necessary
  }

  return safeString;
}

/**
 * Builds the array of buttons for the Discord Rich Presence activity.
 * @param config - The plugin configuration.
 * @param songInfo - The current song information.
 * @returns An array of buttons or undefined if no buttons are configured.
 */
export const buildDiscordButtons = (
  config: DiscordPluginConfig,
  songInfo: SongInfo,
): GatewayActivityButton[] | undefined => {
  const buttons: GatewayActivityButton[] = [];
  if (config.playOnPearDesktop && songInfo.url) {
    buttons.push({
      label: 'Play on Pear Desktop',
      url: songInfo.url,
    });