Published
Edited
Aug 6, 2020
2 forks
Importers
34 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
OH.asHtml()
Insert cell
OH = new Formula().add('O', ElementsBySymbol.H, '-')
Insert cell
Insert cell
Insert cell
ETOH.asTex()
Insert cell
ETOH = {
// Tests addition, subtraction, charge
const f = new Formula().add('C', 'H', 'H', 'H', 'C', 'H', 'H');
return f.add(OH.sub('-'));
}
Insert cell
Insert cell
viewof methane = Formula.block('C', 'H4')
Insert cell
methane
Insert cell
methane.asString()
Insert cell
viewof ethane = Formula.block('C2H6')
Insert cell
Insert cell
class Reaction {
constructor(left, right) {
const parse = f =>
f instanceof Formula
? [f]
: typeof f === 'string'
? f.split(/\s*,\s*/).map(Formula.parse)
: f.flatmap
? f.flatmap(parse)
: f instanceof Array
? f
: [f];
this.left = parse(left);
this.right = parse(right);
}

asTex() {
const l = this.left.map(s => s.asTex()).join(', ');
const r = this.right.map(s => s.asTex()).join(', ');
return `${l} \\rightarrow ${r}`;
}

balanceCheck() {
return balanceCheck(this.left)(this.right);
}

// Return an HTML version of the reaction, either block/display mode or inline
asHtml(block) {
const view = display => value => ((display.value = value), display);
return view((block ? tex.block : tex)(templateCallSite(this.asTex())))(
this
);
}
}
Insert cell
new Reaction('H2O', 'H2,O2').asHtml(true)
Insert cell
new Reaction('H2O', [Formula.parse('H2', 'O')]).balanceCheck()
Insert cell
Insert cell
Insert cell
balanceCheck(['H2O2', 2])('H2O', 'O2')
Insert cell
balanceCheck(['H2O2', 2])('H2O', 'O2', 'O2')
Insert cell
balanceCheck(['H2O2', 2])('H2O', 'H2O', 'O2')
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
new Formula()
.add(['H2O2', 2])
.sub(['O2', 1])
.isEmpty()
Insert cell
ElementsByNumber[3].describe()
Insert cell
ElementsBySymbol.Cl.describe()
Insert cell
Formula.parse('H2O2').toString()
Insert cell
Insert cell
viewof X = periodicTable(mode)
Insert cell
X
Insert cell
X instanceof Formula && X.toString()
Insert cell
X instanceof Formula && X.asTex()
Insert cell
X instanceof Formula && X.asHtml()
Insert cell
X instanceof Formula && X.asObject()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more