← back to Dw Photo Capture

public/js/field-clean.js

14 lines

/*
 * field-clean.js — shared string-field coercion for OCR/form workflows.
 *
 * Gemini Vision JSON parsing can return what should be string fields as numbers or nulls:
 * a manufacturer SKU like "51526" arrives as the number 51526, a missing field as null instead of "".
 * Before calling .trim() or .replace() on these fields, coerce to string and handle nulls.
 *
 * This helper is used across all simple-*.html capture flows to prevent "Cannot read property 'trim'
 * of undefined" crashes when Gemini returns unexpected types.
 */
function cleanField(v) {
  return String(v == null ? '' : v).trim();
}