npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

molecular-formula

v1.1.5

Published

A library for working with molecular-formula

Downloads

57

Readme

molecular-formula

Build Status codecov All Contributors License: MIT

A simple library to work with molecular formula.

View on npm here.

Installation

npm install molecular-formula --save

Usage

Creating a new molecular formula and accessing the formula

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).

Adding and subtracting elements

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.

Molecular mass

You can retrieve the molecular mass of a formula by using the getMass() function.

Example usage:

water.getMass();
// This gives 18.01528

Tests

You can run npm test to run the tests after installing the development dependencies.

Contributors

April Arcus 💻 ⚠️

Future functionality

There are currently no planned improvements to this module. I am open to suggestions so let me know if you think something is missing.

License

This software is released under the MIT license

Support this project!

Support this project on Patreon!