GithubHelp home page GithubHelp logo

unitlib's Introduction

unitlib

A modern library for unit conversion and manipulation with zero dependencies and a small bundle size.

Installation:

pnmp install unitlib fraction.js
# or
yarn add unitlib fraction.js
# or
npm install unitlib fraction.js

Example usage:

import { SI } from 'unitlib/systems';

const x = SI.parseQuantity('3.2 kW s / kg').toBaseUnits();
x.value // 3200
x.unit.toString() // "m^2 / s^2"

x.toString() // "3200 m^2 / s^2"
x.toString({ compact: true, fancyUnicode: true }) // "3200m²/s²"
x.toString({ forceExponential: true }) // "3.2 * 10^3 m^2 / s^2"

x.toParts({ forceExponential: true })
// [
//   { type: 'multiplicator', string: '3.2', number: 3.2 },
//   { type: 'multiplicationSign', string: ' * ' },
//   { type: 'base', string: '10', number: 10 },
//   { type: 'exponent', string: '^3', number: 3 },
//   { type: 'unit', string: 'm^2', prefix: '', baseUnit: 'm', exponent: { type: 'exponent', string: '^2', number: 2 } },
//   { type: 'divisionSign', string: ' / ' },
//   { type: 'unit', string: 's^2', prefix: '', baseUnit: 's', exponent: { type: 'exponent', string: '^2', number: 2 } }
// ]

Main concepts

  • Quantity, for example “2 km” or “8.2 MiB/s”, is a numerical value together with a unit. You can perform arithmetic operations on quantities, or convert them to a different unit.
  • Unit can be either simple, for example “km“ or “W”, or composite, for example “kWh/m²“. Simple units are always simplified to a reduced fraction of base units, while composite units allow for things like “Wh/s” to remain unsimplified.
  • System, for example SI, Imperial or IEC, is a collection of base units and their prefixes.

unitlib's People

Contributors

risai avatar m93a avatar quacken8 avatar

Stargazers

 avatar

Watchers

 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.