classifyCustomFieldsAsync function exported ✓ 100.0%

Last updated: 2026-03-04T23:21:38.391Z

Metrics

LOC: 45 Complexity: 9 Params: 2 Coverage: 100.0% (14/14 lines, 0x executed)

Signature

classifyCustomFieldsAsync( fields: FormField[], ): : Promise<FormField[]>

Summary

Async classification for custom component fields. Custom adapter adapters populate label / name / placeholder / autocomplete but cannot determine domain-level fieldType (e.g. "company", "cpf"). This function runs the full active classifier chain — keyword → tensorflow → chrome-ai — so ambiguous fields reach TF.js/Gemini Nano with their contextSignals. Rules: - html-type and html-fallback are excluded (not meaningful for custom DOM). - Generic results ("text", "unknown") never stop the search — we keep trying the next classifier so TF.js gets a chance to classify via contextSignals. - The adapter's concrete type ("select", "checkbox", …) is preserved when all classifiers fail to add semantic context. - "custom-select" is stamped ONLY as a last resort when nothing matched.

Source Code

export async function classifyCustomFieldsAsync(
  fields: FormField[],
): Promise<FormField[]> {
  const classifiers = getActiveClassifiers().filter(
    // html-type probes native input[type] — meaningless for custom wrappers.
    // html-fallback is a last-resort for native inputs — not for custom.
    (c) => c.name !== "html-type" && c.name !== "html-fallback",
  );

  for (const field of fields) {
    const adapterType = field.fieldType; // type set by the adapter (may be "unknown")
    let classified = false;
    const t0 = performance.now();

    for (const classifier of classifiers) {
      const result = classifier.detectAsync
        ? await classifier.detectAsync(field)
        : classifier.detect(field);

      if (result === null) continue;

      // Generic results ("text", "unknown") are not useful for custom fields —
      // continue to the next classifier (e.g. TF.js) which may be more certain.
      if (GENERIC_TYPES.has(result.type)) continue;

      field.fieldType = result.type;
      field.detectionMethod = classifier.name;
      field.detectionConfidence = result.confidence;
      field.detectionDurationMs = performance.now() - t0;
      classified = true;
      break;
    }

    if (!classified) {
      // Nothing could determine the semantic type — preserve the adapter-set
      // concrete type (e.g. "select") and stamp the fallback method.
      // adapterType is already on field.fieldType; just record the method.
      field.detectionMethod = "custom-select";
      field.detectionConfidence = GENERIC_TYPES.has(adapterType) ? 0.5 : 0.9;
      field.detectionDurationMs = performance.now() - t0;
    }
  }

  return fields;
}

Dependencies (Outgoing)

graph LR classifyCustomFieldsAsync["classifyCustomFieldsAsync"] getActiveClassifiers["getActiveClassifiers"] classifyCustomFieldsAsync -->|calls| getActiveClassifiers style classifyCustomFieldsAsync fill:#dbeafe,stroke:#2563eb,stroke-width:2px click classifyCustomFieldsAsync "d6918a7c3b58c267.html" click getActiveClassifiers "94c3286cfdb569c3.html"
TargetType
getActiveClassifiers calls

Impact (Incoming)

graph LR classifyCustomFieldsAsync["classifyCustomFieldsAsync"] detectAllFieldsAsync["detectAllFieldsAsync"] detectFormFields["detectFormFields"] makeField["makeField"] classifierNames["classifierNames"] detectAllFieldsAsync -->|calls| classifyCustomFieldsAsync detectFormFields -->|calls| classifyCustomFieldsAsync makeField -->|uses| classifyCustomFieldsAsync classifierNames -->|uses| classifyCustomFieldsAsync style classifyCustomFieldsAsync fill:#dbeafe,stroke:#2563eb,stroke-width:2px click classifyCustomFieldsAsync "d6918a7c3b58c267.html" click detectAllFieldsAsync "1b422b3353cdbe22.html" click detectFormFields "f533b30bd49ac06c.html" click makeField "a5977875ae7cda10.html" click classifierNames "a00f1b67db4c0c6d.html"