Use unique contact identifiers for KOC claims
This commit is contained in:
35
lib/claimant-identifier.ts
Normal file
35
lib/claimant-identifier.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user