All Projects → jscheiny → Safe Units

jscheiny / Safe Units

Licence: mit
Type-safe TypeScript units of measure 👷📏

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Safe Units

Rink Rs
Unit conversion tool and library written in rust
Stars: ✭ 242 (+76.64%)
Mutual labels:  units, units-of-measure
Indriya
JSR 385 - Reference Implementation
Stars: ✭ 74 (-45.99%)
Mutual labels:  units, units-of-measure
Unit Api
Units of Measurement API
Stars: ✭ 140 (+2.19%)
Mutual labels:  units, units-of-measure
UnitfulAstro.jl
An extension of Unitful.jl for astronomers.
Stars: ✭ 18 (-86.86%)
Mutual labels:  units-of-measure, units
Coulomb
coulomb: unit analysis for Scala
Stars: ✭ 109 (-20.44%)
Mutual labels:  units, units-of-measure
Units
A compile-time enabled Modern C++ library that provides compile-time dimensional analysis and unit/quantity manipulation.
Stars: ✭ 365 (+166.42%)
Mutual labels:  units, units-of-measure
measured
Type-safe, intuitive units of measure
Stars: ✭ 81 (-40.88%)
Mutual labels:  units-of-measure, units
Unitful.jl
Physical quantities with arbitrary units
Stars: ✭ 279 (+103.65%)
Mutual labels:  units, units-of-measure
Units Of Measure
Type-safe dimensional analysis and unit conversion in Kotlin.
Stars: ✭ 69 (-49.64%)
Mutual labels:  typesafe, units-of-measure
Length.js
📏 JavaScript library for length units conversion.
Stars: ✭ 292 (+113.14%)
Mutual labels:  units, units-of-measure
Unitsnet
Makes life working with units of measurement just a little bit better.
Stars: ✭ 641 (+367.88%)
Mutual labels:  units, units-of-measure
Yaiouom
Prototype extension of the Rust type system towards checking units-of-measure
Stars: ✭ 86 (-37.23%)
Mutual labels:  units-of-measure
Mizur
Mizur is a tool to simplify the handling of units, and units coercion/mapping. (it is an evolution of Abacus)
Stars: ✭ 34 (-75.18%)
Mutual labels:  typesafe
Quantiphy
Physical quantities
Stars: ✭ 33 (-75.91%)
Mutual labels:  units
Units Converter
A simple utility library to measure and convert between units
Stars: ✭ 31 (-77.37%)
Mutual labels:  units
Types First Ui
An opinionated framework for building long-lived, maintainable UI codebases
Stars: ✭ 114 (-16.79%)
Mutual labels:  typesafe
Unit
Conversion of unit library for golang
Stars: ✭ 80 (-41.61%)
Mutual labels:  units-of-measure
Barril
Python package to manage units for physical quantities
Stars: ✭ 25 (-81.75%)
Mutual labels:  units-of-measure
Hoodie
Hoodie is a type safe wrapper around jersey http client
Stars: ✭ 22 (-83.94%)
Mutual labels:  typesafe
Undux
⚡️ Dead simple state for React. Now with Hooks support.
Stars: ✭ 1,488 (+986.13%)
Mutual labels:  typesafe

Safe Units

Build Status NPM Version MIT License

Safe Units is a type safe library for using units of measurement in TypeScript. Safe Units provides an implementation of an SI based unit system but is flexible enough to allow users to create their own unit systems which can be independent or can interoperate with the built-in units. Users can also make unit systems for any numeric type they'd like not just the JavaScript number type. This library requires TypeScript 3.2 or higher.

import { Length, Measure, meters, seconds, Time, Velocity } from "safe-units";

const length: Length = Measure.of(30, meters);
const time: Time = Measure.of(15, seconds);
const velocity: Velocity = length.over(time);

console.log(length.toString());   // 30 m
console.log(time.toString());     // 15 s
console.log(velocity.toString()); // 2 m * s^-1

const error: Velocity = length.times(time);
// ERROR: A measure of m*s isn't assignable to a measure of m/s.

Features

⭐  Compile-time unit arithmetic for typesafe dimensional analysis (with exponents between -5 and +5)!

⭐  Large library of predefined units including metric (with prefixes), Imperial, and US customary units!

⭐  Ability to add your own unit system that can work with built-in units!

⭐  Long build times & cryptic error messages!

Prerequisites

Safe units is written in TypeScript and should be consumed by TypeScript users to take full advantage of what it provides. In addition you will need the following:

Installation

npm install safe-units

or

yarn add safe-units

Examples

Unit arithmetic

import { bars, kilograms, Measure, meters, milli, seconds } from "safe-units";

const width = Measure.of(3, meters);
const height = Measure.of(4, meters);
const area = width.times(height).scale(0.5);
const hypot = Measure.sqrt(width.squared().plus(height.squared())); // 5 m

const mass = Measure.of(30, kilograms);
const mps2 = meters.per(seconds.squared());
const acceleration = Measure.of(9.8, mps2);

const force = mass.times(acceleration); // 294 N
const pressure = force.over(area); // 49 Pa
const maxPressure = Measure.of(0.5, milli(bars)); // 0.5 mbar
pressure.lt(maxPressure) // true

Type errors

import { Force, Length, Measure, meters, seconds, Time } from "safe-units";

const length: Length = Measure.of(10, meters);
const time: Time = Measure.of(10, seconds);

length.plus(time);
// ERROR: Measures of different units cannot be added

length.minus(time);
// ERROR: Measures of different units cannot be subtracted

const force: Force = length.over(time);
// ERROR: Measure of m/s is not assignable to measure of kg*m/s^2

const root = Measure.sqrt(length);
// ERROR: Can't take sqrt of measure of m since it's not a perfect square

Naming units

import { days, Measure, miles, speedOfLight, yards } from "safe-units";

const furlongs = Measure.of(220, yards, "fur");

console.log(Measure.of(8, furlongs).in(miles)); // 1 mi
console.log(Measure.of(1, miles).in(furlongs)); // 8 fur

const fortnights = Measure.of(14, days, "ftn");
const megaFurlongsPerMicroFortnight = mega(furlongs)
    .per(micro(fortnights))
    .withSymbol("Mfur/µftn");

console.log(speedOfLight.in(megaFurlongsPerMicroFortnight)); // 1.8026174997852542 Mfur/µftn

Deriving quantities

import { Acceleration, Measure, meters, seconds, Time } from "safe-units";

const Jerk = Acceleration.over(Time);
type Jerk = typeof Jerk;

const mps2 = meters.per(seconds.squared());
const acceleration = Measure.of(9.8, mps2);
const jerk: Jerk = acceleration.over(Measure.of(2, seconds));

console.log(jerk.toString()); // 4.9 m * s^-3

Defining dimensions

import { Area, Measure, minutes, seconds, Time } from "safe-units";

const frames = Measure.dimension("frames");

const Frames = frames;
type Frames = typeof frames;

const FrameRate = Frames.over(Time);
type FrameRate = typeof FrameRate;

const fps: FrameRate = frames.per(seconds).withSymbol("fps");

const minFrameRate = Measure.of(60, fps);

const measuredFrames = Measure.of(8000, frames);
const elapsedTime = Measure.of(2, minutes);
const measuredFps: FrameRate = measuredFrames.over(elapsedTime);

if (measuredFps.lt(minFrameRate)) {
    // Optimize
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].