refactor: centralize WhatsApp target normalization
This commit is contained in:
57
src/whatsapp/normalize.test.ts
Normal file
57
src/whatsapp/normalize.test.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { isWhatsAppGroupJid, normalizeWhatsAppTarget } from "./normalize.js";
|
||||
|
||||
describe("normalizeWhatsAppTarget", () => {
|
||||
it("preserves group JIDs", () => {
|
||||
expect(normalizeWhatsAppTarget("120363401234567890@g.us")).toBe(
|
||||
"120363401234567890@g.us",
|
||||
);
|
||||
expect(normalizeWhatsAppTarget("123456789-987654321@g.us")).toBe(
|
||||
"123456789-987654321@g.us",
|
||||
);
|
||||
expect(normalizeWhatsAppTarget("whatsapp:120363401234567890@g.us")).toBe(
|
||||
"120363401234567890@g.us",
|
||||
);
|
||||
expect(
|
||||
normalizeWhatsAppTarget("whatsapp:group:120363401234567890@g.us"),
|
||||
).toBe("120363401234567890@g.us");
|
||||
expect(normalizeWhatsAppTarget("group:123456789-987654321@g.us")).toBe(
|
||||
"123456789-987654321@g.us",
|
||||
);
|
||||
expect(
|
||||
normalizeWhatsAppTarget(" WhatsApp:Group:123456789-987654321@G.US "),
|
||||
).toBe("123456789-987654321@g.us");
|
||||
});
|
||||
|
||||
it("normalizes direct JIDs to E.164", () => {
|
||||
expect(normalizeWhatsAppTarget("1555123@s.whatsapp.net")).toBe("+1555123");
|
||||
});
|
||||
|
||||
it("rejects invalid targets", () => {
|
||||
expect(normalizeWhatsAppTarget("wat")).toBeNull();
|
||||
expect(normalizeWhatsAppTarget("whatsapp:")).toBeNull();
|
||||
expect(normalizeWhatsAppTarget("@g.us")).toBeNull();
|
||||
expect(normalizeWhatsAppTarget("whatsapp:group:@g.us")).toBeNull();
|
||||
});
|
||||
|
||||
it("handles repeated prefixes", () => {
|
||||
expect(normalizeWhatsAppTarget("whatsapp:whatsapp:+1555")).toBe("+1555");
|
||||
expect(normalizeWhatsAppTarget("group:group:120@g.us")).toBe("120@g.us");
|
||||
});
|
||||
});
|
||||
|
||||
describe("isWhatsAppGroupJid", () => {
|
||||
it("detects group JIDs with or without prefixes", () => {
|
||||
expect(isWhatsAppGroupJid("120363401234567890@g.us")).toBe(true);
|
||||
expect(isWhatsAppGroupJid("123456789-987654321@g.us")).toBe(true);
|
||||
expect(isWhatsAppGroupJid("whatsapp:120363401234567890@g.us")).toBe(true);
|
||||
expect(isWhatsAppGroupJid("whatsapp:group:120363401234567890@g.us")).toBe(
|
||||
true,
|
||||
);
|
||||
expect(isWhatsAppGroupJid("x@g.us")).toBe(false);
|
||||
expect(isWhatsAppGroupJid("@g.us")).toBe(false);
|
||||
expect(isWhatsAppGroupJid("120@g.usx")).toBe(false);
|
||||
expect(isWhatsAppGroupJid("+1555123")).toBe(false);
|
||||
});
|
||||
});
|
||||
33
src/whatsapp/normalize.ts
Normal file
33
src/whatsapp/normalize.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { normalizeE164 } from "../utils.js";
|
||||
|
||||
function stripWhatsAppTargetPrefixes(value: string): string {
|
||||
let candidate = value.trim();
|
||||
for (;;) {
|
||||
const before = candidate;
|
||||
candidate = candidate
|
||||
.replace(/^whatsapp:/i, "")
|
||||
.replace(/^group:/i, "")
|
||||
.trim();
|
||||
if (candidate === before) return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
export function isWhatsAppGroupJid(value: string): boolean {
|
||||
const candidate = stripWhatsAppTargetPrefixes(value);
|
||||
const lower = candidate.toLowerCase();
|
||||
if (!lower.endsWith("@g.us")) return false;
|
||||
const localPart = candidate.slice(0, candidate.length - "@g.us".length);
|
||||
if (!localPart || localPart.includes("@")) return false;
|
||||
return /^[0-9]+(-[0-9]+)*$/.test(localPart);
|
||||
}
|
||||
|
||||
export function normalizeWhatsAppTarget(value: string): string | null {
|
||||
const candidate = stripWhatsAppTargetPrefixes(value);
|
||||
if (!candidate) return null;
|
||||
if (isWhatsAppGroupJid(candidate)) {
|
||||
const localPart = candidate.slice(0, candidate.length - "@g.us".length);
|
||||
return `${localPart}@g.us`;
|
||||
}
|
||||
const normalized = normalizeE164(candidate);
|
||||
return normalized.length > 1 ? normalized : null;
|
||||
}
|
||||
Reference in New Issue
Block a user