unitease
v1.0.0
Published
A lightweight JavaScript library for unit conversions, supporting length, weight, temperature, and time.
Maintainers
Readme
UnitEase
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");Features
- Convert between various units for length, weight, temperature, and time.
- Check if a conversion is supported between two units.
Installation
UnitEase can be installed via npm:
npm install uniteaseUsage
Import UnitEase
const UnitEase = require("unitease");Convert Units
// Convert 10 kilograms to feet
const result = UnitEase.convert(10, "kilograms", "pounds");
console.log(result); // Output: 22.0462Add Custom Conversion Definitions
// 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.5137Add Custom Aliases
// Add custom aliases for any unit
UnitEase.addAlias("kilograms", "kg");
console.log(UnitEase.convert(5, "kg", "pounds")); // Output: 11.0231Check Possible Conversions
// Get possible conversions for a temperature unit
console.log(UnitEase.getPossibleConversions("celsius"));
// Output: ['fahrenheit', 'kelvin']Example Scenerios
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, inchesHandle 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 Conversion Support
// Check if a conversion from meters to feet is supported
const isSupported = UnitEase.supports("meters", "feet");
console.log(isSupported); // Output: trueExamples
// 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")); // falseSupported Units
Length
feetinchesyardsmilesmeterskilometerscentimetersmillimeters
Weight
kilogramsmilligramsgramspoundsounces
Temperature
celsiusfahrenheitkelvin
Time
secondsminuteshoursdaysweeksyears
How It Works
The
convertfunction checks the relevant unit type (length,weight,temperature, ortime) and performs the conversion.The
supportsfunction verifies if a conversion between the given units is possible.The
getPossibleConversionsfunction lists all the units that a given unit can be converted to.
Contributing
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!
License
MIT License
Made with ❤️ by Irtaza
