29 lines
652 B
TypeScript
29 lines
652 B
TypeScript
export type Handle = {
|
|
localPart: string
|
|
domain: string | null
|
|
}
|
|
|
|
// Parse a "handle" in the form: `[@] <local-part> '@' <domain>`
|
|
export function parseHandle(query: string): Handle {
|
|
// Remove the leading @, if there's one.
|
|
if (query.startsWith('@')) {
|
|
query = query.substring(1)
|
|
}
|
|
|
|
// In case the handle has been URL encoded
|
|
query = decodeURIComponent(query)
|
|
|
|
const parts = query.split('@')
|
|
const localPart = parts[0]
|
|
|
|
if (!/^[\w-.]+$/.test(localPart)) {
|
|
throw new Error('invalid handle: localPart: ' + localPart)
|
|
}
|
|
|
|
if (parts.length > 1) {
|
|
return { localPart, domain: parts[1] }
|
|
} else {
|
|
return { localPart, domain: null }
|
|
}
|
|
}
|