detectDateFormat function exported ✓ 100.0%

Last updated: 2026-03-01T23:25:47.120Z

Metrics

LOC: 35 Complexity: 17 Params: 1 Coverage: 100.0% (14/14 lines, 12x executed)

Signature

detectDateFormat(field: { inputType?: string; placeholder?: string; pattern?: string; }): : DateFormat

Summary

Detects the expected date format for a field from its HTML attributes. Priority order: 1. inputType === "date" → ISO (native browser date picker requires YYYY-MM-DD) 2. placeholder text hinting at the format (e.g. "DD/MM/YYYY" → "br") 3. pattern attribute structure hints 4. Default: "iso"

Architecture violations

View all

  • [warning] max-cyclomatic-complexity: 'detectDateFormat' has cyclomatic complexity 17 (max 10)

Tags

#@param field - Subset of FormField attributes to inspect#@returns The detected date format

Source Code

export function detectDateFormat(field: {
  inputType?: string;
  placeholder?: string;
  pattern?: string;
}): DateFormat {
  // Native <input type="date"> always requires YYYY-MM-DD
  if (field.inputType === "date") return "iso";

  const ph = (field.placeholder ?? "").toUpperCase();
  if (ph.length > 0) {
    const ddPos = ph.indexOf("DD");
    const mmPos = ph.indexOf("MM");
    const yyyyPos = Math.max(ph.indexOf("YYYY"), ph.indexOf("AAAA"));

    // YYYY/AAAA appears first → ISO (e.g. "YYYY-MM-DD")
    if (yyyyPos >= 0 && (mmPos < 0 || yyyyPos < mmPos)) return "iso";

    // DD appears before MM → BR (e.g. "DD/MM/YYYY" or "dd/mm/aaaa")
    if (ddPos >= 0 && mmPos >= 0 && ddPos < mmPos) return "br";

    // MM appears before DD → US (e.g. "MM/DD/YYYY")
    if (ddPos >= 0 && mmPos >= 0 && mmPos < ddPos) return "us";
  }

  // Inspect HTML `pattern` attribute for structural clues
  const pat = field.pattern ?? "";
  if (pat) {
    // Starts with 4-digit year: \d{4} or [0-9]{4} → ISO
    if (/^(\\d\{4\}|\[0-9\]\{4\})/.test(pat)) return "iso";
    // Year at end: \d{4}$ → likely BR or US, default to BR (Brazilian context)
    if (/(\\d\{4\}|\[0-9\]\{4\})\s*$/.test(pat)) return "br";
  }

  return "iso";
}

Dependencies (Outgoing)

graph LR detectDateFormat["detectDateFormat"] style detectDateFormat fill:#dbeafe,stroke:#2563eb,stroke-width:2px click detectDateFormat "103dab3e7e6117b8.html"
TargetType
pat dynamic_call

Impact (Incoming)

graph LR detectDateFormat["detectDateFormat"] generateDateForField["generateDateForField"] generateDateForField -->|uses| detectDateFormat style detectDateFormat fill:#dbeafe,stroke:#2563eb,stroke-width:2px click detectDateFormat "103dab3e7e6117b8.html" click generateDateForField "a214011e5ced5989.html"
SourceType
generateDateForField uses