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

unitopia

v1.0.0

Published

Types and utilities for dealing with measurement units (time, length, mass, money, etc.).

Downloads

6

Readme

Unitopia

Types and utilities for associating units (dimensions) to quantities. The primary use case for Unitopia is applications that deal with like measurements in possibly non-homogenous units. For example, an application that allows the user to switch between pounds and kilograms, or tasks that can be measured in days or weeks.

What it provides:

  • Type safety for working with dimensional values, making it more difficult to accidentally compare or perform arithmetic on dissimilar units.
  • Ergonomic unit conversion & normalization.
  • Serialization and deserialization to JSON.
  • Zero dependencies.

Currently available dimensions:

  • Length
  • Mass (weight)
  • Time

Coming soon:

  • Money (currency)
  • Power (Watt)
  • Current (Ampere)
  • Voltage (Volt)

Coming not-so-soon:

  • Support for derived units & dimensional analysis

Usage

Creating Quantities

To create a quantity object, simply import the appropriate class and use the appropriate static method:

import { Mass } from 'unitopia'

const m1 = Mass.Pounds(100)
const m2 = Mass.Kilograms(22)

The type of m1 will be Mass<'Pound'>, tye type of m2 will be Mass<'Kilogram'>. You can also use the Mass constructor but (currently) it won't be typed to the specific unit:

const m3 = new Mass(2, 'Short Ton')

The type of m3 will be Mass<'Kilogram' | 'Milligram' | 'Gram' | 'Short Ton' |...>. For this reason, it's preferred to use the convenience constructors illustrated above.

Serializing Quantities

Quantities can be serialized directly with JSON.stringify:

JSON.stringify(m1) // {"dimension":"Mass","unit":"Pound","value":100}

Deserializing Quantities

Deserialize with the parse or tryParse methods, which can accept either an object or a string (which will be parsed from JSON):

try {
  const m = Mass.parse({ dimension: 'Mass', unit: 'Gram', value: 100 })
  console.log(m)
} catch (err) {
  console.error('parsing error:', err.message)
}
const res = Mass.tryParse({ dimension: 'Mass', unit: 'Gram', value: 100 })
if ('error' in res) console.error('parsing error:', err.message)
else console.log(res.mass)

Unit Conversion

Simply call the static convert method and specify the desired unit:

import { Length } from 'unitopia'
const h1 = Length.Inches(71)
const h2 = Length.convert(h1, 'Centimeter') // h2.value is 180.34000000072135

Unit Array Normalization

To normalize an array of possibly nonhomogeneous values, call the static normalize method:

const lengths = [Length.Inches(71), Length.Meters(0.5), Length.Feet(2.5)]
const lengths_in_cm = Length.normalize(length, 'Centimeter')

Contributing

TODO