Skip to content

TypeScript API

Terminal window
npm install asciichem

One runtime dependency: parsanol — its wasm engine parses the checksummed PARG artifact the Rust crate embeds. Runs in the browser, Node, and workers.

import { parse, parseText, parseSmiles, parseMolfile } from "asciichem";
const formula = parse("2H_2 + O_2 -> 2H_2O"); // => Formula
parse === parseText; // alias
// All input grammars land in the same model
parseSmiles("CC(=O)OC1=CC=CC=C1C(=O)O"); // aspirin
parseMolfile(v2000Text); // coordinates preserved

Invalid input throws ParseError (extends AsciiChemError) — nothing is ever parsed “partially”.

Every model node carries output methods:

formula.toText(); // canonical AsciiChem text (round trip)
formula.toModelJSON(); // asciichem-model v1 wire JSON
formula.toSvg(); // typographic SVG
formula.toStructuralSvg(); // 2D structural SVG (bonds you author)
formula.toSmiles(); // deterministic SMILES writer
mol.toMolfile(); // V2000 molfile (molecules only)
import { writeSmiles, writeMolfile, Perception } from "asciichem";
writeSmiles(aspirin); // same writers, function form
Perception.aromaticRings(atoms, edges); // shared perception pass

Formatters are registered, not switch-cased — import the ones you need:

import {
TextFormatter, SvgFormatter, StructuralSvgFormatter,
MathmlFormatter, renderMathml,
} from "asciichem";
renderMathml(formula); // presentation MathML, L2 goldens
new TextFormatter().render(formula);

The MathML output is byte-identical across every runtime — pinned by the shared corpus goldens (format/mathml/*), enforced in CI.

The canonical JSON interchange (asciichem-model v1):

import { fromModelJSON } from "asciichem";
const wire = formula.toModelJSON(); // plain JSON object
const back = fromModelJSON(wire); // => Formula
fromModelJSON(JSON.stringify(wire)); // strings work too
back.toText(); // round-trips
import { ParseError, AsciiChemError } from "asciichem";
try {
parse("H_2O @cas(\"not-a-cas\")");
} catch (e) {
if (e instanceof AsciiChemError) {
// all library errors extend AsciiChemError
}
}
import { setInchiEngine, inchiIdentityFor, InchiEngineMissingError } from "asciichem";
setInchiEngine(myEngine); // register once
inchiIdentityFor(molecule) // { inchi, inchikey? }
// no engine registered => throws InchiEngineMissingError with guidance

The InChI algorithm is never reimplemented — engines wrap an InChI implementation. The native engine behind the Ruby gem ships a WASM build as the chemomer npm package (from_molfile, inchikey), byte-exact against the IUPAC 1.07.5 oracle; a plain function adapter is all the seam needs. See Local identity.

  • Browser — everything above. This site’s playground uses exactly this package, client-side.
  • Node — same API; the PARG artifact and the parsanol wasm engine ship prebuilt.
  • Build tools — parse at build time, emit SVG/MathML into static output; the corpus guarantees your web previews match every other runtime’s build-time renders.

Parse/reject · L0 emission · L1 text · L2 MathML · L2 SVG (typographic + structural) · L3 stereo · SMILES · molfile — 100% of the shared corpus. The CML and linter layers, and the resolver/citation ports, are not yet claimed (asciichem-ts#26). See Implementations for the full matrix.

Play