/** * ChemParse * A TypeScript library for parsing chemical formulas. * * Handles nested parentheses, decimal and scientific notation, * dot-separated parts (hydrates), leading coefficients, element lists * with commas, and ionic charges (caret and superscript notation). * * Methods: * - parse ( formula: string ) : ElementCounts * - validate ( formula: string ) : boolean * - compare ( a: string, b: string ) : boolean * - diff ( a: string, b: string ) : ElementCounts * * @author Paul Köhler (komed3) * @license MIT * @version 1.0.2 */ /** * @typedef ElementSymbol * * A union type of all valid chemical element symbols. */ export type ElementSymbol = 'H' | 'He' | 'Li' | 'Be' | 'B' | 'C' | 'N' | 'O' | 'F' | 'Ne' | 'Na' | 'Mg' | 'Al' | 'Si' | 'P' | 'S' | 'Cl' | 'Ar' | 'K' | 'Ca' | 'Sc' | 'Ti' | 'V' | 'Cr' | 'Mn' | 'Fe' | 'Co' | 'Ni' | 'Cu' | 'Zn' | 'Ga' | 'Ge' | 'As' | 'Se' | 'Br' | 'Kr' | 'Rb' | 'Sr' | 'Y' | 'Zr' | 'Nb' | 'Mo' | 'Tc' | 'Ru' | 'Rh' | 'Pd' | 'Ag' | 'Cd' | 'In' | 'Sn' | 'Sb' | 'Te' | 'I' | 'Xe' | 'Cs' | 'Ba' | 'La' | 'Ce' | 'Pr' | 'Nd' | 'Pm' | 'Sm' | 'Eu' | 'Gd' | 'Tb' | 'Dy' | 'Ho' | 'Er' | 'Tm' | 'Yb' | 'Lu' | 'Hf' | 'Ta' | 'W' | 'Re' | 'Os' | 'Ir' | 'Pt' | 'Au' | 'Hg' | 'Tl' | 'Pb' | 'Bi' | 'Po' | 'At' | 'Rn' | 'Fr' | 'Ra' | 'Ac' | 'Th' | 'Pa' | 'U' | 'Np' | 'Pu' | 'Am' | 'Cm' | 'Bk' | 'Cf' | 'Es' | 'Fm' | 'Md' | 'No' | 'Lr' | 'Rf' | 'Db' | 'Sg' | 'Bh' | 'Hs' | 'Mt' | 'Ds' | 'Rg' | 'Cn' | 'Fl' | 'Lv' | 'Ts' | 'Og'; /** * @typedef ElementCounts * * An object mapping element symbols to their counts in a chemical formula. */ export type ElementCounts = Partial>; /** * @interface ChemParseResult * * The result of parsing a chemical formula, including element counts * and optional ionic charge. */ export interface ChemParseResult { elementCounts: ElementCounts; charge?: number; } /** * ChemParse * * A class providing static methods to parse and analyze chemical formulas. */ export default class ChemParse { /** * Converts a regex match of a charge into a numeric value. * * @param match - The regex match array. * @return - The numeric charge value. */ private static _chargeNumber; /** * Parses the charge from a regex match. * * @param match - The regex match array. * @return - The numeric charge value, or undefined if no charge is found. */ private static _parseCharge; /** * Core parser for a formula segment without leading coefficients or charges. * * @param str - The formula segment to parse. * @return - An object mapping element symbols to their counts. */ private static _parseCore; /** * Parses a chemical formula into its constituent elements and counts. * * @param formula - The chemical formula to parse. * @return - An object containing element counts and optional charge. * @throws - If the formula is invalid. */ static parse(formula: string): ChemParseResult; /** * Validates a chemical formula. * * @param formula - The chemical formula to validate. * @return - True if the formula is valid, false otherwise. */ static validate(formula: string): boolean; /** * Compares two chemical formulas for equivalence. * * @param a - The first chemical formula. * @param b - The second chemical formula. * @return - True if the formulas are equivalent, false otherwise. */ static compare(a: string, b: string): boolean; /** * Computes the difference in element counts and charge between two formulas. * * @param a - The first chemical formula. * @param b - The second chemical formula. * @return - An object representing the difference in element counts and charge. */ static diff(a: string, b: string): ChemParseResult; }