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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@caue_neves/unit-convertor-js

v1.0.0

Published

A versatile and lightweight JavaScript library for unit conversions, supporting length, area, volume, and more.

Downloads

9

Readme

Unit Converter Library

Welcome to the Unit Converter Library! 🎉
This library helps developers perform unit conversions for a wide range of categories such as distance, weight, temperature, area, volume, pressure, data, power, fuel economy, and more. It even includes advanced scientific conversions for specialized applications.


🚀 Installation

Install the library via npm:

npm install unit-convertor-js

🛠️ Usage

First, import the library into your project:

const { convert } = require('unit-convertor-js');

Then, start converting! 🚀


📚 Supported Categories

Distance

  • Units: meters, kilometers, miles, yards, feet, inches

Weight

  • Units: grams, kilograms, pounds, ounces, tons

Temperature

  • Units: celsius, fahrenheit, kelvin

Area

  • Units: squareMeters, squareKilometers, hectares, acres

Volume

  • Units: liters, milliliters, cubicMeters, gallons

Pressure

  • Units: pascal, bar, psi

Data

  • Units: bytes, kilobytes, megabytes, gigabytes, terabytes

Power

  • Units: watts, kilowatts, horsepower

Fuel Economy

  • Units: kilometersPerLiter, milesPerGallon

Advanced

  • Examples: liters ↔ square meters, kilograms ↔ liters, and more

🧮 Examples

Basic Conversion

Perform a simple conversion between two units of the same category:

// Convert 100 meters to kilometers
console.log(convert(100, "meters", "kilometers")); 
// Output: 0.1

// Convert 50 grams to pounds
console.log(convert(50, "grams", "pounds")); 
// Output: 0.110231

Temperature Conversion

Convert between Celsius, Fahrenheit, and Kelvin:

// Convert 25 Celsius to Fahrenheit
console.log(convert(25, "celsius", "fahrenheit")); 
// Output: 77

// Convert 300 Kelvin to Celsius
console.log(convert(300, "kelvin", "celsius")); 
// Output: 26.85

Advanced Conversion

Use additional parameters for specialized calculations:

Liters to Square Meters

Specify the thickness in meters for accurate area calculation.

console.log(convert(20, "liters", "squareMeters", { thickness: 0.05 })); 
// Output: 400

Weight to Volume

Specify the density (kg/L):

console.log(convert(10, "kilograms", "liters", { density: 0.8 })); 
// Output: 12.5

Fuel Economy

Convert between kilometers per liter and miles per gallon:

console.log(convert(5, "kilometersPerLiter", "milesPerGallon")); 
// Output: 11.76

🛡️ Error Handling

The library provides clear error messages for invalid conversions:

try {
  console.log(convert(100, "grams", "meters")); // Invalid conversion
} catch (error) {
  console.error(error.message); 
  // Output: Conversion from "grams" to "meters" is not supported.
}

🧪 Testing the Library

1. Install Jest for Testing

Use Jest for easy testing:

npm install jest --save-dev

2. Create a Test File

Create a file called unitConverter.test.js:

const { convert } = require('./unitConverter');

// Test basic conversions
test("Convert meters to kilometers", () => {
  expect(convert(1000, "meters", "kilometers")).toBe(1);
});

test("Convert grams to pounds", () => {
  expect(convert(1000, "grams", "pounds")).toBeCloseTo(2.20462);
});

// Test temperature conversions
test("Convert Celsius to Fahrenheit", () => {
  expect(convert(0, "celsius", "fahrenheit")).toBe(32);
});

test("Convert Kelvin to Celsius", () => {
  expect(convert(300, "kelvin", "celsius")).toBeCloseTo(26.85);
});

// Test advanced conversions
test("Convert liters to square meters (thickness)", () => {
  expect(convert(20, "liters", "squareMeters", { thickness: 0.05 })).toBe(400);
});

test("Convert kilograms to liters (density)", () => {
  expect(convert(10, "kilograms", "liters", { density: 0.8 })).toBe(12.5);
});

// Test invalid conversions
test("Invalid conversion", () => {
  expect(() => convert(100, "grams", "meters")).toThrow();
});

3. Run the Tests

Run the tests using:

npx jest unitConverter.test.js

🔮 Future Features (Coming Soon)

  • 🌍 Currency Converter: Convert between currencies using real-time exchange rates.
  • 📡 Astronomical Units: Light years, parsecs, and more.
  • 🚀 Speed: Kilometers per hour ↔️ Miles per hour ↔️ Meters per second.

❤️ Feedback and Contributions

We love contributions! Feel free to fork the repository, suggest features, or submit issues. 🛠️