/** * The X username alone, as the server keeps it: from "@shop", "shop" or the * address of the profile. Empty for an empty field, null for anything that * is no username (up to 15 letters, digits or underscores). */ export const xHandle = (value: string): string | null => { const typed = value.trim(); const profile = typed.match( /^(?:https?:\/\/)?(?:www\.|mobile\.)?(?:x|twitter)\.com\/@?([^/?#\s]+)/i ); const handle = (profile ? profile[1] : typed).replace(/^@+/, ''); if (handle === '') { return ''; } return /^[A-Za-z0-9_]{1,15}$/.test(handle) ? handle : null; }; export const X_HANDLE_ERROR = 'The username, up to 15 letters, digits or underscores; the address of the profile works too.';