A simple library to work with molecular formula.
View on npm here.
npm install molecular-formula --save
Creating a new molecular formula and accessing the formula
To use this module, import it and then create a new molecular formula.
var molFormula = ;var water = 'H2O';
Subscripts are also now supported thanks to AprilArcus!
var water = 'H₂O';
You can retrieve the original formula like so.
var original_formula = water;
You also have access to the elemental composition.
var composition = water;
Creating formula with parantheses is also supported.
var ammonium_phosphate = '(NH4)3PO4';
There is a method for providing the simplified, condensed molecular formula in cases like this.
var simplified_formula = ammonium_phosphate;
This can be useful in cases where perhaps you have a carbon chain where all the components are kept separate (e.g. CH3(CH2)4CH2OH).
Adding and subtracting elements
You also have the ability to add and subtract elements from the formula with the
subtract functions. You can pass in either a string representation of what you want to add or a JSON object.
ammonium_phosphate;ammonium_phosphate;// This will give you N3H12PO4NaClammonium_phosphate;ammonium_phosphate;// This will give you N3H12PO4 again
Please note that once you add or subtract elements, the getFormula() function will return the same thing as the getSimplifiedFormula() function.
You can retrieve the molecular mass of a formula by using the
water;// This gives 18.01528
You can run
npm test to run the tests after installing the development dependencies.
There are currently no planned improvements to this module. I am open to suggestions so let me know if you think something is missing.
This software is released under the MIT license