Giter VIP home page Giter VIP logo

molecular-formula's Introduction

molecular-formula

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');

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.

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!

molecular-formula's People

Contributors

emptyport avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.