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

@energyreach/conversion

v0.1.10

Published

A simple library to perform units conversion.

Downloads

81

Readme

conversion

A simple library to perform units conversion.

Usage

Installation

Install the package:

npm install @energyreach/conversion

Then use it in code as follows:

import { Convert } from '@energyreach/conversion';

Class creation

Create a conversion object to work with. If no constructor parameters are specified, the default UnitsLibrary will be loaded.

// object creation with default units library
const a = new Convert();

// object creation with energy units conversion only
const a = new Convert('energy');

// object creation with temperature and volume units conversion only
const a = new Convert(['temperature', 'volume']);

// object creation with custom units conversion library
const a = new Convert(customUnitsLibrary);

Parameters to Convert():

  • group name (str): One of the units groups to be loaded as specified in Group type.
  • a list of group names (str[]): A list of the units groups to be loaded as specified in Group type.
  • units library (Units): If specified, only this library will be loaded.

Custom units

Other than using the builtin unit conversions, you can also create and import your own as follows:

// object creation with default units library
const a = new Convert();
a.load({
    // Define distance group conversions
    // NOTE: Unit names can be anything, but must be unique, otherwise an existing unit in cache will be overwritten
    'distance': {
        'm': {},
        'km': {base: 1000},
        // you can also add a display name for this unit during printing
        'mi': {base: 1609.344, display: 'mile'},
        // you can also instead of using base, add a formula
        'cm': {
            to: (value: Value): Value => value.div(1000),
            from: (value: Value): Value => value.mul(1000),
        }
    }
});

// empty the units library cache (no units will be loaded and you can start loading anew)
a.clear();

NOTE: Calling load will not clear previous unit loads, but will instead load and overwrite any overlaps.

Conversion

Perform simple unit conversions.

// object creation with default units library
const a = new Convert();

// returns a float number = 54.5
a.from(12.5, 'C').to('F');

// returns a float number with 2 digits after decimal point precision = 72.98
a.from(22.7643, 'C').to('F', 2);

// returns a float number = 18.29
a.from(18.29, 'C').value();
// returns a float number with custom precision = 18.3
a.from(18.29, 'C').value(1);

Precision

You can set precisions in two different ways. Object based precision, which will carry throughout the life of a conversion object, or on-the-go precision, which will apply only to the function call, while the object will retain its originally set precision.

// The default precision is 3 digits after the decimal point
const a = new Convert();

// set the precision to 5 digits after the decimal point
a.precision(5);

// the object precision is now 5 digits, while both of these will return 3 decimal points
a.from(18.29, 'C').value(3);
a.from(18.29, 'C').to('F', 3);

// the following will return 5 decimal points
a.from(18.29, 'C').value();
a.from(18.29, 'C').to('F');

Validation

You can also manually check if a unit is loaded and can be worked with.

// The default precision is 3 digits after the decimal point
const a = new Convert();

// check if we can work with cubic meters
// An error will be thrown if the unit is not found in cache
a.verifyUnit('m^3')

NOTE: All unit operations auto-check whether a particular unit exists before performing any operations.

Display

You can also call the display name of each unit. The following will return cubic meters as '㎥'

// The default precision is 3 digits after the decimal point
const a = new Convert();

// returns the display name for cubic meters
a.display('m^3')

Global

There is a limited set of static functions which can be used for convenience.

// Clear the static cache
Convert.clear();

// Load energy and volume units in static cache
Convert.load(['energy', 'volume']);

// verify we can use volume
Convert.verifyUnit('m^3')

// returns the display name for volume
Convert.display('m^3')