Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
molecular-formula
Advanced tools
A simple library to work with molecular formula.
View on npm here.
npm install molecular-formula --save
To use this module, import it and then create a new molecular formula.
var molFormula = require('molecular-formula');
var water = new molFormula('H2O');
Subscripts are also now supported thanks to AprilArcus!
var water = new molFormula('H₂O');
You can retrieve the original formula like so.
var original_formula = water.getFormula();
You also have access to the elemental composition.
var composition = water.getComposition();
Output:
{
H: 2,
O: 1
}
Creating formula with parantheses is also supported.
var ammonium_phosphate = new molFormula('(NH4)3PO4');
There is a method for providing the simplified, condensed molecular formula in cases like this.
var simplified_formula = ammonium_phosphate.getSimplifiedFormula();
Output:
N3H12PO4
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).
You also have the ability to add and subtract elements from the formula with the add
and subtract
functions. You can pass in either a string representation of what you want to add or a JSON object.
Example usage:
ammonium_phosphate.add('NaCl');
ammonium_phosphate.getSimplifiedFormula();
// This will give you N3H12PO4NaCl
ammonium_phosphate.subtract({ 'Na': 1, 'Cl': 1 });
ammonium_phosphate.getSimplifiedFormula();
// 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 getMass()
function.
Example usage:
water.getMass();
// 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
FAQs
A library for working with molecular-formula
The npm package molecular-formula receives a total of 20 weekly downloads. As such, molecular-formula popularity was classified as not popular.
We found that molecular-formula demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.