File size: 809 Bytes
b6ecafa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const RTL_CHAR_REGEX =
  /\p{Script=Hebrew}|\p{Script=Arabic}|\p{Script=Syriac}|\p{Script=Thaana}/u

export function detectTextDirection(text: string | null): 'rtl' | 'ltr' {
  if (!text) return 'ltr'
  const skipPattern = /[\s\p{P}\p{S}]/u
  for (const char of text) {
    if (skipPattern.test(char)) continue
    return RTL_CHAR_REGEX.test(char) ? 'rtl' : 'ltr'
  }
  return 'ltr'
}

export function validateAttachment(file: { name: string; size: number; type: string }): string | null {
  if (file.size > 10 * 1024 * 1024) return `File "${file.name}" exceeds 10MB limit`
  return null
}

export function formatFileSize(bytes: number): string {
  if (bytes < 1024) return `${bytes} B`
  if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`
  return `${(bytes / (1024 * 1024)).toFixed(1)} MB`
}