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

ts-unit-converter

v1.1.7

Published

Automatic unit of measurement conversion for class attributes

Downloads

6

Readme

ts-unit-converter

This is a simple library intended to deal with metric/imperial units conversion with property decorators.

Install

yarn add ts-unit-converter

npm install ts-unit-converter

Usage

To perform the conversion of a class property, use @Measurement . You can indicate the type of unit (see table below) for common pairs of units, or specify the imperial and metric units individually.

Also, this library needs to know the sourceUnit of each property. Typically this is the unit coming from an API or similar. This is the unit used in the setters and constructors. The sourceUnit can be omitted if you specify a default source unit for the unit type. To do so, use TSUnitConverter.setDefaultSourceUnit(type, unit) the earliest you can. Usually in main.ts or index.ts.

If you want to set a property in the displayed units (for example, when taking user input), you have to wrap the set operations inside setInDisplayUnits().

To change the unit system, use TSUnitConverter.setUnitSystem. It can be 'metric' or 'imperial'. By default it is 'metric' To get the current unit system use TSUnitConverter.getUnitSystem().

About JSON.stringify

When parsing an object with decorated properties, all the properties will be converted back to the source unit, to keep consistency when sending data back to the API.

Unit names and abbreviations

You can get the unit name or abbreviation using getUnit<Class>(object, property) or getUnitAbbreviation<Class>(object, property). You have to specify the class as a type, and pass the instance and the property name as parameters.

Manual conversion

You can convert a property manually using convertToUnit<Class>(object, property, unit) (see example).

Example:

import {Measurement, TSUnitConverter, setInDisplayUnits, getUnitAbbreviation, getUnit, convertToUnit} from 'ts-unit-converter';

class MyClass {

    @Measurement({type: "long-distance", sourceUnit: 'meters'})
    distance: number;

    @Measurement({metric: "meters", imperial: "feet", sourceUnit: "inches"})
    radius: string;

    @Measurement({type: "volume"})
    volume: number;

    @Measurement({metric: "grams", imperial: "ounces", sourceUnit: "pounds"})
    weight: number;

    constructor(o: Partial<MyClass>) {
        Object.assign(this, o)
    }
}

//lets say all the volumes without sourceUnit specified will be 'liters'
TSUnitConverter.setDefaultSourceUnit('volume', "liters");

const obj = new MyClass({
    distance: 5000, //sourceUnit: meters
    radius: "60", //sourceUnit: inches
    volume: 180, //sourceUnit: liters
});

//by default the unit system is metric
console.log('metric distance: ', obj.distance + " " + getUnit<MyClass>(obj, "distance")); // 5 kilometers
console.log('metric radius: ', obj.radius + " " + getUnit<MyClass>(obj, "radius")); // 1.524 meters
console.log('metric volume: ', obj.volume + " " + getUnit<MyClass>(obj, "volume")); // 180 liters

//set weight to 12 grams, using the unit displayed
setInDisplayUnits(() => obj.weight = 12);
console.log('metric weight: ', obj.weight + " " + getUnitAbbreviation<MyClass>(obj, "weight")); // 12 g

TSUnitConverter.setUnitSystem("imperial");
console.log('imperial distance: ', obj.distance + " " + getUnit<MyClass>(obj, "distance")); // 3.1 miles
console.log('imperial radius: ', obj.radius + " " + getUnit<MyClass>(obj, "radius")); // 5 feet
console.log('imperial weight: ', obj.weight + " " + getUnit<MyClass>(obj, "weight")); // 0.42 ounces

//set volume to 10 gallons
setInDisplayUnits(() => obj.volume = 10);
console.log('imperial volume: ', obj.volume + " " + getUnit<MyClass>(obj, "volume")); // 10 gallons

//The source units are preserved when converting to JSON
console.log("JSON:", JSON.stringify(obj)); 
//{"distance":5000,"radius":60,"volume":37.85,"weight":0.0264}

//convert unit manually
console.log("distance in yards: " + convertToUnit<MyClass>(obj, "distance", "yards"));

//Get the unit name and abbreviation for that property, in the displayed unit system.
console.log("imperial radius unit abbreviation: ", getUnitAbbreviation<MyClass>(obj, "radius")); //ft
console.log("imperial radius unit name: ", getUnit<MyClass>(obj, "radius")); //feet

Unit types

| Type | Metric | Imperial | | :------------ | :------------ | :------------ | | distance | meters | feet | | long-distance | kilometers | miles | | short-distance | centimeters | inches | | volume | liters | gallons | | mass | kilograms | pounds | | little-mass | grams | ounces | | temperature | celsius | fahrenheit | | speed | kmh | mph |

Supported units

Metric: "meters" | "kilometers" | "centimeters" | "kilograms" | "grams" | "liters" | "celsius" | "kmh"

Imperial: "feet" | "yards" |"miles" | "inches" | "pounds" | "ounces" | "gallons" | "fahrenheit" | "mph"