const mfCheck = /^(?:[A-Z][a-z]?\d* *)+$/; export default function getAtomsFromMF(mf: string): Record { if (!mfCheck.test(mf)) { throw new Error(`MF can not be parsed: ${mf}`); } const atoms: Record = {}; const parts = mf.matchAll(/(?[A-Z][a-z]?)(?\d*)/g); for (const part of parts) { const { atom, number } = part.groups as { atom: string; number: string }; if (!atoms[atom]) { atoms[atom] = 0; } atoms[atom] += number === '' ? 1 : Number(number); } return atoms; }