1.0.5 • Published 2 years ago

angular-units-converter v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Units Converter

A simple library to convert units. Heavily based on convert-units.

An example implementation of the library.

Key Features

  • Tree shakable.
  • No dependencies.
  • Build for es6, cmj and umd.
  • Tested to 4 significant figures.

Installation

npm install units-converter --save

Usage

The library provide a simple chained API to transform units of the same type. The units are broken down to modules, so it is possible and recommended, to load only the unite types that you are using.

import { voltage } from 'units-converter';

voltage(1).from('V').to('mV').value;
//1000

or

import * as converter from 'units-converter';

converter.voltage(1).from('V').to('mV').value;
//1000

converter.mass(1).from('lb').to('oz').value;
//16
  • Convert unit will return an object of the unit specifed in the "to" function
import { length } from 'units-converter';

length(1200).from('mm').to('m');
 /* {
      value: 1.2,
      unit: 'm',
      system: 'metric',
      singular: 'Meter',
      plural: 'Meters'
    } */
  • Convert unit to Best
import { length } from 'units-converter';

// the smallest unit with a value above 1
length(12000).from('mm').toBest();
/* {  
      value: 1.2,
      unit: 'm',
      system: 'metric',
      singular: 'Meter',
      plural: 'Meters'
    } */

// the smallest unit excluding meters
length(12000).from('mm').toBest({ exclude: ['m'] });
 /* {
      value: 1200,
      unit: 'cm',
      system: 'metric',
      singular: 'Centimeter',
      plural: 'Centimeters'
    } */

// the smallest unit with a value equal to or above 10
length(900).from('mm').toBest({ cutOffNumber: 10 });
 /* {
      value: 90,
      unit: 'cm',
      system: 'metric',
      singular: 'Centimeter',
      plural: 'Centimeters'
    } */
    
// the smallest unit with a value equal to or above 10
length(1000).from('mm').toBest({ cutOffNumber: 10 });
 /* {
      value: 100,
      unit: 'cm',
      system: 'metric',
      singular: 'Centimeter',
      plural: 'Centimeters'
    } */
  • Possibilities will return an array with the abbreviations of the available units
import { voltage } from 'units-converter';

voltage().from('V').possibilities();
// [ 'V', 'mV', 'kV' ]
  • Describe will return an object with all the details of the unit
import { voltage } from 'units-converter';

voltage().describe('V');

/* { 
      unit: 'V', 
      system: 'metric', 
      singular: 'Volt', 
      plural: 'Volts' 
    } */
  • List will return and array with the description of all the available units
import { voltage } from 'units-converter';

voltage().list();

/* [
    { 
      unit: 'V', 
      system: 'metric', 
      singular: 'Volt', 
      plural: 'Volts' 
    },
    {
      unit: 'mV',
      system: 'metric',
      singular: 'Millivolt',
      plural: 'Millivolts'
    },
    {
      unit: 'kV',
      system: 'metric',
      singular: 'Kilovolt',
      plural: 'Kilovolts'
    }
  ] */

Units

  • mm
  • cm
  • m
  • in
  • ft-us
  • ft
  • fathom
  • mi
  • nMi

Road map

  • Add types to support better integration with typescript using projects.

  • Add functionality for the addition of custom units.

  • Add more units, the goal is to approach the amount of units provided by matlab.

  • Rewrite the project with typescript.