File size: 1,714 Bytes
27e706d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

export const normalizePhoneNumber = (phone: string): string => {
  let normalized = phone
    .replace(/O/g, "0")
    .replace(/o/g, "0")
    .replace(/I/g, "1")
    .replace(/l/g, "1")
    .replace(/[^\d+]/g, "")
    .trim();
  return normalized;
};

export const normalizeName = (name: string): string => {
  if (!name) return name;
  return name
    .trim()
    .replace(/\s+/g, " ")
    .split(" ")
    .map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
    .join(" ");
};

export const normalizeDate = (dateStr: string): string | null => {
  if (!dateStr) return null;
  try {
    const cleaned = dateStr.trim();
    let date: Date;

    if (cleaned.includes("/")) {
      const parts = cleaned.split("/").map(Number);
      if (parts.length === 3) {
        let [d, m, y] = parts;
        if (y < 100) y += 2000;
        date = new Date(y, m - 1, d);
      } else {
        return null;
      }
    } else if (cleaned.includes("-")) {
      const parts = cleaned.split("-").map(Number);
      if (parts.length === 3) {
        let [y, m, d] = parts;
        if (y < 100) y += 2000;
        date = new Date(y, m - 1, d);
      } else {
        return null;
      }
    } else {
      date = new Date(cleaned);
    }

    if (isNaN(date.getTime())) return null;
    return date.toISOString().split("T")[0];
  } catch {
    return null;
  }
};

export const normalizeNumeric = (value: string): string => {
  return value.replace(/O/g, "0").replace(/o/g, "0").replace(/I/g, "1").replace(/l/g, "1").trim();
};

export const calculateBMI = (weightKg: number, heightCm: number): number => {
  const heightM = heightCm / 100;
  return Number((weightKg / (heightM * heightM)).toFixed(1));
};