o2o-infinith-frontend/src/features/channels/lib/normalizeHandles.ts

33 lines
921 B
TypeScript

/**
* 다양한 입력 형식의 Instagram 핸들을 순수 username 으로 정규화.
* 백엔드 normalizeHandles 유틸의 브라우저측 사본 — 두 파일을 항상 동기화 유지.
*/
export function normalizeInstagramHandle(
raw: string | null | undefined,
): string | null {
if (!raw || typeof raw !== 'string') return null;
let handle = raw.trim();
if (!handle) return null;
if (handle.includes('instagram.com')) {
try {
const urlStr = handle.startsWith('http') ? handle : `https://${handle}`;
const url = new URL(urlStr);
const segments = url.pathname.split('/').filter(Boolean);
handle = segments[0] || '';
} catch {
const match = handle.match(/instagram\.com\/([^/?#]+)/);
handle = match?.[1] || '';
}
}
if (handle.startsWith('@')) {
handle = handle.slice(1);
}
handle = handle.replace(/\/+$/, '');
return handle || null;
}