Latest Threat Research:Malicious dYdX Packages Published to npm and PyPI After Maintainer Compromise.Details
Socket
Book a DemoInstallSign in
Socket

number-unit

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

number-unit

Numbers with units.

Source
npmnpm
Version
0.6.0
Version published
Maintainers
1
Created
Source

Number (with) Unit

build status

A heavily tested JavaScript component to handle arbitrary precision numbers with units.

Why?

A number without context (unit) is meaningless. Even more so in the realm of computers. Computers rely on input, usually from humans, to be precise. Humans are prone to error. This library helps to remove that error.

Consider these two famous incidents...

  • Mars Surveyor '98 Orbiter

From https://en.wikipedia.org/wiki/Mars_Climate_Orbiter:

However, on September 23, 1999, communication with the spacecraft was lost as the spacecraft went into orbital insertion, due to ground-based computer software which produced output in non-SI units of pound-seconds (lbf s) instead of the metric units of newton-seconds (N s) specified in the contract between NASA and Lockheed. The spacecraft encountered Mars on a trajectory that brought it too close to the planet, causing it to pass through the upper atmosphere and disintegrate.

  • Air Canada Flight 143

From https://en.wikipedia.org/wiki/Gimli_Glider:

...ran out of fuel at an altitude of 12,500 metres (41,000 ft) MSL, about halfway through its flight originating in Montreal to Edmonton. The crew were able to glide the aircraft safely to an emergency landing at Gimli Industrial Park Airport, a former Royal Canadian Air Force base in Gimli, Manitoba.[1]

The subsequent investigation revealed a combination of company failures and a chain of human errors that defeated built-in safeguards. Fuel loading was miscalculated because of a misunderstanding of the recently adopted metric system which replaced the imperial system.

If you're writing software that handles people's money, you can't afford to be wrong. That's why this library was built.

Install

npm i --save number-unit

Usage

Quick example:

// import { UnitType } from 'number-unit' // if using ES6 (ES2015)
var UnitType = require('number-unit').UnitType

// create a UnitType first
var bitcoin = UnitType.create('bitcoin', { satoshis: 1, bits: 1e2, BTC: 1e8 }, 'bits')

// now create a NumberUnit
var amount1 = bitcoin.BTC(1.53)
var amount2 = bitcoin.bits('1530000') // notice, can accept strings as well

console.log(amount1.toString()) // => 1.53 BTC
console.log(amount2.toString()) // => 1530000 bits

// compare numerical values
console.log(amount1.equals(amount2)) // => true

Important Concepts to Know

  • You must use UnitType.create() to create a UnitType() to start working with NumberUnits.

  • UnitTypes can be a type of other UnitTypes. For example, you may create a UnitType named distance, and then want to create another named distanceSI representing your desire to model SI / Metric Units. Now you may want to create another named distanceUS, modeling United States customary units. Since both have distance has a parent type, you can convert between the two. This is the value of parent types. As it wouldn't make sense to convert from distance to currency or something lie that. See for some examples: https://github.com/jprichardson/number-unit

UnitType

UnitType.create

UnitType.create(label, [parentUnitType], [definitions], [defaultUnit])

Creates an instance of UnitType and returns it.

  • label: The unit type label.
  • parentUnitType: The parent unit type. Useful for converting between UnitType that have the same parent.
  • definitions: Actual conversions.
  • defaultUnit: Default unit. Used when defaultUnit is called.

[instanceof UnitType].parse(string)

Method that parses the input string and returns an instance of NumberUnit with a number value extracted from the string and a unitName from the string.

var amount = bitcoin.parse('1.53 BTC')
console.log(amount.toNumber()) // => 1.53
console.log(amount.unitName) // => BTC

[instanceof UnitType].ZERO

Property that creates and returns an instance of NumberUnit with a number value of 0 and a the unit being the default unit.

var zero = bitcoin.ZERO
console.log(amount.toNumber()) // => 0
console.log(amount.unitName) // => bits

NumberUnit

Unit

ConversionUnit

Keywords

number

FAQs

Package last updated on 09 Nov 2015

Did you know?

Socket

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.

Install

Related posts