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

unit-transformer

v1.0.0

Published

unit converter for si to metric and metric to si

Downloads

60

Readme

Unit Converter

Features

  • Convert between metric and US customary units for length, weight, and volume.
  • Simple API for quick integration.
  • TypeScript for strong type safety.
  • Easily extensible for additional unit categories or custom conversions.

Installation

Install via npm:

npm install unit-transformer

Usage

Import converter functions and convert values between units.

Mass conversions

convert mass/weight convert

function exampleUsage() {
	// Convert 5 pounds to kilograms
	const kgValue = convertMass(5, 'lb', 'kg');
	console.log('5 pounds is', kgValue, 'kilograms');

	// Using convenience methods
	const ozToGrams = MassConverter.ouncesToGrams(10);
	console.log('10 ounces is', ozToGrams, 'grams');
}

Temperature conversions

Length Conversion

Convert between units of length such as meters, miles, feet, and more:

import { convertLength } from 'unit-converter';
const miles = convertLength(1000, 'meter', 'mile'); // 0.621371 miles
const feet = convertLength(1, 'kilometer', 'foot'); // 3280.84 feet

Weight Conversion

Convert between kilograms, pounds, ounces, etc.:

import { convertWeight } from 'unit-converter';

const pounds = convertWeight(10, 'kilogram', 'pound'); // 22.0462 pounds
const ounces = convertWeight(1, 'pound', 'ounce'); // 16 ounces

Volume Conversion

Convert between various volume units such as UK Gallons, US Gallons, Liters (Litres), and more. The converter accepts both American and British English spellings.

import { convertVolume } from 'unit-converter';

const usGallons = convertVolume(10, 'liter', 'us_gallon'); // Output: 2.64172 US gallons
const ukGallons = convertVolume(10, 'litre', 'uk_gallon'); // Output: 2.19969 UK gallons
const cubicFeet = convertVolume(1000, 'liter', 'cubic_foot'); // Output: 35.3147 cubic feet
const cubicInches = convertVolume(1, 'cubic_foot', 'cubic_inch'); // Output: 1728 cubic inches
const milliliters = convertVolume(1, 'liter', 'millilitre'); // Output: 1000 milliliters
const gallons = convertVolume(10, 'liter', 'gallon'); // Output: 2.64172 (interpreted as US gallons)

Area Conversion

Convert between various area units such as acres, hectares, square meters, and more:

import { convertArea } from 'unit-converter';

const acres = convertArea(1000, 'square_meter', 'acre'); // Output: 0.247105 acres
const squareFeet = convertArea(1, 'acre', 'square_foot'); // Output: 43560 square feet

Supported Units

Length Units

  • Metric: meter, kilometer
  • US Customary: mile, foot, yard

Weight Units

  • Metric: kilogram
  • US Customary: pound, ounce

Volume Units

  • Metric:

    • liter
    • milliliter / millilitre
    • cubic_centimeter
    • cubic_meter / cubic_metre
  • Imperial/US Customary:

    • gallon (interpreted as US gallon by default), us_gallon
    • uk_gallon
    • cubic_inch
    • cubic_foot,
    • quart

Area Units

  • Metric: square_meter, square_centimeter, are, hectare
  • US Customary: acre, square_foot, square_inch

API

convertLength(value: number, from: string, to: string): number

Converts a length value between supported units.

  • value: The numerical value to convert.
  • from: The source unit.
  • to: The target unit.

convertWeight(value: number, from: string, to: string): number

Converts a weight value between supported units.

  • value: The numerical value to convert.
  • from: The source unit.
  • to: The target unit.

convertVolume(value: number, from: string, to: string): number

Convert between various volume units such as UK Gallons, US Gallons, Liters (Litres), and more. The converter accepts both American and British English spellings.

  • value: The numerical value to convert.
  • from: The source unit.
  • to: The target unit.

convertArea(value: number, from: string, to: string): number

Converts an area value between the supported units.

  • value: The numerical value to convert.
  • from: The source unit.
  • to: The target unit.

Extending the Module

To add a new category or conversions:

  1. Create a new file in src/converters/, e.g., temperature-converter.ts.
  2. Define conversion rates or formulas in a similar structure.
  3. Export the function and include it in src/index.ts.

Development

Run Tests

The library uses Jest for unit testing. Run tests with:

npm test

Build the Library

Compile the TypeScript code into JavaScript for distribution:

npm run build

Clean the Project

Remove all build artifacts:

npm run clean

Contributing

Contributions are welcome! Feel free to submit issues or feature requests. Fork the repository and create a pull request for any improvements.

License

This project is licensed under the MIT License.