about summary refs log tree commit diff
path: root/src/lib/strings/display-names.ts
blob: a95bfd6798ca8cbd46d4e1290247706d22af71e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import {ModerationUI} from '@atproto/api'

// \u2705 = ✅
// \u2713 = ✓
// \u2714 = ✔
// \u2611 = ☑
const CHECK_MARKS_RE = /[\u2705\u2713\u2714\u2611]/gu
const CONTROL_CHARS_RE =
  /[\u0000-\u001F\u007F-\u009F\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/g
const MULTIPLE_SPACES_RE = /[\s][\s\u200B]+/g

export function sanitizeDisplayName(
  str: string,
  moderation?: ModerationUI,
): string {
  if (moderation?.blur) {
    return ''
  }
  if (typeof str === 'string') {
    return str
      .replace(CHECK_MARKS_RE, '')
      .replace(CONTROL_CHARS_RE, '')
      .replace(MULTIPLE_SPACES_RE, ' ')
      .trim()
  }
  return ''
}

export function combinedDisplayName({
  handle,
  displayName,
}: {
  handle?: string
  displayName?: string
}): string {
  if (!handle) {
    return ''
  }
  return displayName
    ? `${sanitizeDisplayName(displayName)} (@${handle})`
    : `@${handle}`
}