ChemPal Documentation - v1.6.0
    Preparing search index...
    • Scores how "formula-like" a candidate is, so the best of several matches can be chosen. Favors more element symbols first (a real formula packs several), then the presence of a subscript/count, then raw length — so NaOSOCH3 outranks a two-letter coincidence such as IN (from "EINECS") or CS pulled out of surrounding codes/prose.

      Parameters

      • candidate: string

        A raw formula match.

      Returns number

      A numeric score; higher is more likely to be a real formula.

      scoreFormula("NaOSOCH3") > scoreFormula("IN"); // true
      
      export function scoreFormula(candidate: string): number {
      const elementCount = candidate.match(new RegExp(FORMULA_ELEMENT_PATTERN, 'g'))?.length ?? 0;
      const hasCount = /[0-9₀-₉⁰-⁹]/.test(candidate);
      return elementCount * 100 + (hasCount ? 50 : 0) + candidate.length;
      }