mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
at samuel/exp-cli 61 lines 1.8 kB view raw
1// Regex from the go implementation 2// https://github.com/bluesky-social/indigo/blob/main/atproto/syntax/handle.go#L10 3import {forceLTR} from '#/lib/strings/bidi' 4 5const VALIDATE_REGEX = 6 /^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/ 7 8export const MAX_SERVICE_HANDLE_LENGTH = 18 9 10export function makeValidHandle(str: string): string { 11 if (str.length > 20) { 12 str = str.slice(0, 20) 13 } 14 str = str.toLowerCase() 15 return str.replace(/^[^a-z0-9]+/g, '').replace(/[^a-z0-9-]/g, '') 16} 17 18export function createFullHandle(name: string, domain: string): string { 19 name = (name || '').replace(/[.]+$/, '') 20 domain = (domain || '').replace(/^[.]+/, '') 21 return `${name}.${domain}` 22} 23 24export function isInvalidHandle(handle: string): boolean { 25 return handle === 'handle.invalid' 26} 27 28export function sanitizeHandle(handle: string, prefix = ''): string { 29 return isInvalidHandle(handle) 30 ? '⚠Invalid Handle' 31 : forceLTR(`${prefix}${handle.toLocaleLowerCase()}`) 32} 33 34export interface IsValidHandle { 35 handleChars: boolean 36 hyphenStartOrEnd: boolean 37 frontLength: boolean 38 totalLength: boolean 39 overall: boolean 40} 41 42// More checks from https://github.com/bluesky-social/atproto/blob/main/packages/pds/src/handle/index.ts#L72 43export function validateServiceHandle( 44 str: string, 45 userDomain: string, 46): IsValidHandle { 47 const fullHandle = createFullHandle(str, userDomain) 48 49 const results = { 50 handleChars: 51 !str || (VALIDATE_REGEX.test(fullHandle) && !str.includes('.')), 52 hyphenStartOrEnd: !str.startsWith('-') && !str.endsWith('-'), 53 frontLength: str.length >= 3 && str.length <= MAX_SERVICE_HANDLE_LENGTH, 54 totalLength: fullHandle.length <= 253, 55 } 56 57 return { 58 ...results, 59 overall: !Object.values(results).includes(false), 60 } 61}