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.
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.
Converts regular numbers in a string to subscript Unicode characters.
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.
Converts regular numbers in a string to superscript Unicode characters.
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.
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.
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.
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.
Checks if a string is a valid molecular formula.
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:
H₂O, x²;\u escape text — H₂O (e.g. unparsed JSON);H₂O, H₂O (decimal or hex);<sub>/<sup> tags — H<sub>2</sub>O.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.
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.
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.
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.
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.
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.
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.
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.
Build grade regexes
The compiled classifier regex (built once).
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.
The regex source string — paste into regex101 (ECMAScript flavor) to inspect.
Converts a purity grade to a representative percentage.