ChemPal Documentation - v1.7.0
    Preparing search index...

    Module helpers/science

    Converters - Science Helpers

    purityGradeToPercentage

    Converts a purity grade to a representative percentage.

    sortablePurityGrade

    Converts a purity value — either a grade label or a percentage — to a number the Purity column can sort on. Grades route through purityGradeToPercentage; everything else is read as the FIRST number in the string, so a range sorts on its lower bound and a trailing qualifier ("+", "or better") is ignored. A leading comparator (<>≤≥≈) applies a small PURITY_COMPARATOR_OFFSET so e.g. >75% sorts just above a bare 75%. Anything unreadable sorts as 0.

    Core Utilities - Science Helpers

    pickBroadestName

    Picks the best single search term from a list of candidate chemical names for the same compound — the broadest form, which tends to yield the most store results (precision is recovered later by scoring each result against every candidate). Chooses the shortest candidate that still contains the primary name's longest word (the compound's distinctive token), so a general synonym like "Sodium hexametaphosphate" beats the more specific primary "Hexasodium hexametaphosphate", while unrelated short synonyms (brand names like "Calgon") are ignored. A one-name (or empty-after-filter) list returns its primary.

    Formatters - Science Helpers

    subscript

    Converts regular numbers in a string to subscript Unicode characters.

    subscriptToAscii

    Converts subscript Unicode digits back to regular ASCII numbers — the inverse of subscript. Turns a display-formatted formula like "Na₆O₁₈P₆" back into "Na6O18P6" before a lookup that only understands plain digits (e.g. a PubChem query, or formula detection). Non-subscript characters pass through unchanged.

    superscript

    Converts regular numbers in a string to superscript Unicode characters.

    superscriptGlyph

    Normalizes Unicode superscript digits already present in str to their literal glyph spelling, mapping each SUPERSCRIPTS value to its SUPERSCRIPT_GLYPHS counterpart. Because both maps resolve the digit keys to identical code points, a string that already contains superscript characters is returned unchanged — this is a normalization pass, not a converter. It does NOT turn ASCII digits into superscripts; use superscript for that.

    subscriptGlyph

    Normalizes Unicode subscript digits already present in str to their literal glyph spelling, mapping each SUBSCRIPTS value to its SUBSCRIPT_GLYPHS counterpart. Because both maps resolve the digit keys to identical code points, a string that already contains subscript characters is returned unchanged — this is a normalization pass, not a converter. It does NOT turn ASCII digits into subscripts; use subscript for that.

    formatFormula

    Formats a plain-ASCII chemical formula with proper Unicode notation: periods that join formula units become an adduct/hydrate dot (⋅), and atom-count digits become subscripts. Digits that are leading stoichiometric coefficients — those at the start of a unit, after a space, or right after the adduct dot — are left full-size, since their preceding character isn't an atom or bracket. Intended for formulas that arrive as plain text (no <sub> markup); tagged formulas should go through findFormulaInHtml instead.

    Interfaces - Science Helpers

    ChemicalSpecs

    Structured chemical properties pulled out of a supplier's free-form product copy. Every field is optional — only the values actually present (and valid) are returned.

    Modules

    <internal>

    Parsers - Science Helpers

    isMoleForm

    Checks if a string is a valid molecular formula.

    findFormulaInText

    Finds the first chemical-formula-like substring in text and returns it with <sub>/<sup> tags converted to Unicode sub/superscript glyphs, or undefined if none is found. Unlike findFormulaInHtml (which only understands <sub>/<sup> markup), this recognizes a subscript/superscript number written in any of four representations, so it works on raw scraped text regardless of how the source encoded it:

    • literal glyphs — H₂O, ;
    • \u escape text — H₂O (e.g. unparsed JSON);
    • HTML numeric entities — H&#8322;O, H&#x2082;O (decimal or hex);
    • <sub>/<sup> tags — H<sub>2</sub>O.
    findFormulaInHtml

    Match for a chemical formula (with or without subscript tags) in a string of html, converting <sub>/<sup> tags to unicode sub/superscripts. Numbers that aren't tagged (e.g. a salt/hydrate coefficient denoting how many of the whole molecule) are matched but left as regular digits.

    parsePurity

    Extracts a purity percentage from a string. Suppliers commonly bake the purity into a product name or grade label (e.g. "Sodium borohydride, min 95%"), so this finds the first percentage and returns it as a number when it falls within the valid (0, 100] range — matching the values ProductBuilder.setPurity accepts. Returns nothing when no valid percentage is present.

    findPurity

    Extracts a purity/grade descriptor from a string, as a string. Unlike parsePurity (which returns only a numeric percentage), this keeps the qualifier — so a comparator percentage like "≥99.8%" or ">99%" is preserved verbatim — and, when no valid percentage is present, falls back to a recognized chemical grade ("ACS Grade", "HPLC Grade", …) via parseGrade, which means an unrecognized string comes back as "Ungraded" rather than nothing. Only an empty or non-string input returns nothing. Built for ProductBuilder.setPurity, whose Purity column shows either kind.

    parseGrade

    Extracts a chemical grade from a string. Recognizes the grade written inline in a product title ("SODIUM, REAGENT (ACS) - 500 G") as well as written as a labeled field ("Grade: Technical"). Falls back to "Ungraded" rather than undefined, so the Purity column always has something to show.

    parseLocalizedNumber

    Parses a numeric token that may use US or European grouping/decimal conventions into a number. When both . and , are present the last-occurring one is treated as the decimal separator and the other as thousands grouping; a single , or . is treated as a decimal point (so European 149,19 reads as 149.19); repeated separators of one kind are treated as grouping only.

    findMolarMass

    Finds the first molar mass / molecular weight in a free-form string and returns it as a number. Built to be run over large, messy scraped text the way findFormulaInText is: labels, separators, and unit spellings vary widely across suppliers and locales, so each is matched tolerantly. Matching is tiered by confidence — a labelled value carrying a unit (Molar mass (M) 149,19 g/mol), then a bare <number> <unit> (58.44 g/mol), then a labelled value with no unit (M.W. 415.6) — and the value is parsed with parseLocalizedNumber so European decimal commas are handled. Returns undefined when no plausible molar mass is present, so unrelated numbers (melting points, densities, prose) are not mistaken for one.

    findMolarity

    Pulls a molar concentration (molarity) out of free-form product copy — the "1.5M", "0.2M", "1M", or "1.5 mol/L" that suppliers bake into titles and descriptions — and returns it as a normalized string suitable for the product's concentration field. The unit must be a capital "M" or literal "mol/L" (see MOLARITY_REGEX), so a lowercase "m" (milli, e.g. "500ml") is never mistaken for molarity. Returns undefined when no molarity is present.

    parseChemicalSpecs

    Extracts structured chemical properties (purity, molecular formula, molecular weight, SMILES) from a supplier's free-form, HTML-laced product copy. Built for Wix suppliers whose specs live inside descriptions and additional-info accordions as loosely-labelled bullet lists — labels and separators vary wildly (MW -, Molecular mass :, Formula:), so each field is matched tolerantly and validated before being returned. CAS numbers are intentionally left to findCAS (in helpers/cas), which already searches free text robustly.

    Regex Patterns - Science Helpers

    buildGradeRegexes

    Build grade regexes

    GRADE_REGEX

    The compiled classifier regex (built once).

    LABELED_GRADE_REGEX

    Companion to GRADE_REGEX for labeled fields ("Grade: Technical"), where an explicit label licenses a bare word-grade stem that would otherwise be too weak to classify. Tried only when GRADE_REGEX finds nothing.

    GRADE_REGEX_SOURCE

    The regex source string — paste into regex101 (ECMAScript flavor) to inspect.