forked from Yara724/api
fix(reports): localize Persian reports and resolve person display names
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
type PersonRecord = object | null | undefined;
|
||||
|
||||
const placeholders = new Set([
|
||||
"user",
|
||||
"actor",
|
||||
"expert",
|
||||
"unknown",
|
||||
"unknown user",
|
||||
"unknown expert",
|
||||
"system",
|
||||
"null",
|
||||
"undefined",
|
||||
"نامشخص",
|
||||
"کاربر",
|
||||
"موجود نیست",
|
||||
]);
|
||||
const englishDigits = (value: string) =>
|
||||
value.replace(/[۰-۹٠-٩]/g, (digit) =>
|
||||
String("۰۱۲۳۴۵۶۷۸۹٠١٢٣٤٥٦٧٨٩".indexOf(digit) % 10),
|
||||
);
|
||||
export function meaningfulPersonName(value: unknown): string {
|
||||
if (typeof value !== "string") return "";
|
||||
const name = value.trim().replace(/\s+/g, " ");
|
||||
const normalized = englishDigits(name);
|
||||
if (
|
||||
!name ||
|
||||
placeholders.has(name.toLowerCase()) ||
|
||||
/^\+?[\d\s()-]+$/.test(normalized) ||
|
||||
/^[a-f\d]{24}$/i.test(normalized) ||
|
||||
/^[a-f\d]{8}(?:-[a-f\d]{4}){3}-[a-f\d]{12}$/i.test(normalized) ||
|
||||
/@/.test(name)
|
||||
)
|
||||
return "";
|
||||
return name;
|
||||
}
|
||||
function phone(value: unknown): string {
|
||||
if (typeof value !== "string" && typeof value !== "number") return "";
|
||||
const text = String(value).trim();
|
||||
return /^\+?[\d۰-۹٠-٩][\d۰-۹٠-٩ ()-]{6,17}$/.test(text) ? text : "";
|
||||
}
|
||||
/** Names first, then explicitly recorded phone numbers. IDs/national codes are never names. */
|
||||
export function personDisplayName(...records: PersonRecord[]): string {
|
||||
for (const source of records) {
|
||||
if (!source) continue;
|
||||
const record = source as Record<string, unknown>;
|
||||
const joined = [
|
||||
meaningfulPersonName(record.firstName),
|
||||
meaningfulPersonName(record.lastName),
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(" ");
|
||||
const name =
|
||||
meaningfulPersonName(record.fullName) ||
|
||||
joined ||
|
||||
meaningfulPersonName(record.name);
|
||||
if (name) return name;
|
||||
}
|
||||
for (const source of records) {
|
||||
if (!source) continue;
|
||||
const record = source as Record<string, unknown>;
|
||||
for (const key of ["phoneNumber", "mobile", "mobileNumber", "phone"]) {
|
||||
const value = phone(record[key]);
|
||||
if (value) return value;
|
||||
}
|
||||
const legacy = englishDigits(
|
||||
String(record.fullName ?? record.username ?? ""),
|
||||
).replace(/[\s()-]/g, "");
|
||||
if (/^(?:\+98|0098|98|0)9\d{9}$/.test(legacy)) return legacy;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
Reference in New Issue
Block a user