Files
koc-loop/lib/claimant-identifier.ts
2026-08-03 13:48:01 +08:00

36 lines
820 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export type ClaimantIdentifier = {
canonical: string;
display: string;
kind: "phone" | "wechat";
};
function stripLabel(value: string) {
return value
.replace(/^(?:微信号|微信|wechat|手机号|手机)\s*[:]?\s*/i, "")
.trim();
}
export function parseClaimantIdentifier(value: unknown): ClaimantIdentifier | null {
const input = stripLabel(String(value ?? "").trim());
if (!input) return null;
const phone = input.replace(/[\s-]/g, "").replace(/^\+?86/, "");
if (/^1[3-9]\d{9}$/.test(phone)) {
return {
canonical: `phone:${phone}`,
display: phone,
kind: "phone",
};
}
if (/^[A-Za-z][A-Za-z0-9_-]{5,31}$/.test(input)) {
return {
canonical: `wechat:${input.toLowerCase()}`,
display: input,
kind: "wechat",
};
}
return null;
}