A lightweight JavaScript library for unit conversion.
- Converts units for length, weight, temperature, and more.
- Supports both metric and imperial systems.
Example:
unitEase.convert(10, "meters", "feet");
- Convert between various units for length, weight, temperature, and time.
- Check if a conversion is supported between two units.
UnitEase can be installed via npm:
npm install unitease
const UnitEase = require("unitease");
// Convert 10 kilograms to feet
const result = UnitEase.convert(10, "kilograms", "pounds");
console.log(result); // Output: 22.0462
// Add custom units and conversion factor.
// Add the factor for only one way calculation!
// Reverse factor will be automatically calculated.
UnitEase.addConversion("lightyears", "parsecs", 0.306601);
console.log(UnitEase.convert(2, "lightyears", "parsecs")); // Output: 0.613202
console.log(UnitEase.convert(2, "parsecs", "lightyears")); // Output: 6.5137
// Add custom aliases for any unit
UnitEase.addAlias("kilograms", "kg");
console.log(UnitEase.convert(5, "kg", "pounds")); // Output: 11.0231
// Get possible conversions for a temperature unit
console.log(UnitEase.getPossibleConversions("celsius"));
// Output: ['fahrenheit', 'kelvin']
-
Find all the valid conversions for a specific unit:
const possibilities = UnitEase.getPossibleConversions("meters"); console.log(`You can convert meters to: ${possibilities.join(", ")}`); // Output: You can convert meters to: feet, inches
-
Handle unsupported units gracefully:
const possibilities = UnitEase.getPossibleConversions("lightyears"); if (possibilities.length === 0) { console.log("No conversions available for this unit."); } // Output: No conversions available for this unit.
// Check if a conversion from meters to feet is supported
const isSupported = UnitEase.supports("meters", "feet");
console.log(isSupported); // Output: true
// Length
console.log(UnitEase.convert(100, "meters", "feet")); // 328.084
// Weight
console.log(UnitEase.convert(5, "kilograms", "pounds")); // 11.0231
// Temperature
console.log(UnitEase.convert(100, "celsius", "fahrenheit")); // 212
// Time
console.log(UnitEase.convert(2, "hours", "minutes")); // 120
// Unsupported conversion
console.log(UnitEase.supports("kilograms", "miles")); // false
feet
inches
yards
miles
meters
kilometers
centimeters
millimeters
kilograms
milligrams
grams
pounds
ounces
celsius
fahrenheit
kelvin
seconds
minutes
hours
days
weeks
years
-
The
convert
function checks the relevant unit type (length
,weight
,temperature
, ortime
) and performs the conversion. -
The
supports
function verifies if a conversion between the given units is possible. -
The
getPossibleConversions
function lists all the units that a given unit can be converted to.
Contributions are welcome! If you want to fix a bug, add more units for conversion or just improve UnitEase, feel free to create a pull request!
MIT License
Made with ❤️ by Irtaza