A raw formula match.
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;
}
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
NaOSOCH3outranks a two-letter coincidence such asIN(from "EINECS") orCSpulled out of surrounding codes/prose.