Skip to content

Commit 26205bd

Browse files
authored
Merge pull request #8 from shahabfar/speed-conversions
Add Speed Conversions
2 parents 85c04f6 + 71bfbdd commit 26205bd

7 files changed

Lines changed: 227 additions & 18 deletions

File tree

readme.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# **UniVertAll**
1+
# **UniVertAll**
22

33
[![Publish to NuGet](https://github.com/shahabfar/Univertall/actions/workflows/release.yml/badge.svg)](https://github.com/shahabfar/Univertall/actions/workflows/release.yml)
44

@@ -35,19 +35,20 @@ Search for `UniVertAll` in the NuGet Package Manager within Visual Studio and in
3535

3636
Here’s a list of all the units supported by UniVertAll:
3737

38-
| **Category** | **Units Supported** |
39-
|--------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
40-
| **Length** | Meter, Kilometer, Centimeter, Millimeter, Micrometer, Nanometer, Inch, Foot, Yard, Mile, Nautical Mile |
41-
| **Mass** | Kilogram, Gram, Milligram, Microgram, Ton, Metric Ton, Long Ton, Short Ton, Pound, Ounce, Stone, Carat |
38+
| **Category** | **Units Supported** |
39+
|--------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
40+
| **Length** | Meter, Kilometer, Centimeter, Millimeter, Micrometer, Nanometer, Inch, Foot, Yard, Mile, Nautical Mile |
41+
| **Mass** | Kilogram, Gram, Milligram, Microgram, Ton, Metric Ton, Long Ton, Short Ton, Pound, Ounce, Stone, Carat |
4242
| **Volume** | Cubic Meter, Liter, Milliliter, Gallon (US), Gallon (UK), Quart, Pint, Cup, Fluid Ounce, Tablespoon, Teaspoon |
43-
| **Power** | Watt, Kilowatt, Megawatt, Gigawatt, Horsepower (Metric), Horsepower (Imperial) |
43+
| **Power** | Watt, Kilowatt, Megawatt, Gigawatt, Horsepower (Metric), Horsepower (Imperial) |
4444
| **Temperature** | Celsius, Fahrenheit, Kelvin |
4545
| **Pressure** | Pascal, Kilopascal, Bar, Atmosphere, Torr, PSI (Pounds per Square Inch) |
46-
| **Volume Flow Rate** | Cubic Meter per Second, Liter per Second, Gallon per Minute (US), Gallon per Hour (US), Cubic Feet per Second, Cubic Feet per Minute, Cubic Inch per Second, Cubic Inch per Minute |
46+
| **Volume Flow Rate** | Cubic Meter / Second, Liter/Second, Gallon/Minute (US), Gallon/Hour (US), Cubic Feet/Second, Cubic Feet/Minute, Cubic Inch/Second, Cubic Inch/Minute |
4747
| **Angle** | Degree, Radian, Gradian |
4848
| **Digital** | Bit, Byte, Kilobyte, Megabyte, Gigabyte, Terabyte, Petabyte, Exabyte |
49-
| **Fuel Economy** | Miles per Gallon (US), Miles per Gallon (Imperial), Liters per 100 Kilometers, Kilometers per Liter |
49+
| **Fuel Economy** | Miles/Gallon (US), Miles/Gallon (Imperial), Liters/100 Kilometers, Kilometers/Liter |
5050
| **Force** | Newton, Kilonewton, Pound-Force, Ounce-Force |
51+
| **Speed** | Kilometers/Hour, Kilonewton, Pound-Force, Ounce-Force |
5152
| **Custom** | CustomUnit (User-defined, such as UnitA, UnitB, etc.) |
5253

5354
##

src/Univertall/Extensions/ConversionExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Univertall.Units.Mass;
77
using Univertall.Units.Power;
88
using Univertall.Units.Pressure;
9+
using Univertall.Units.Speed;
910
using Univertall.Units.Temperature;
1011
using Univertall.Units.Volume;
1112
using Univertall.Units.VolumeFlowRate;
@@ -126,4 +127,10 @@ public static MassConversionBuilder ConvertMass(this double value)
126127
{
127128
return new MassConversionBuilder(value);
128129
}
130+
131+
public static SpeedConversionBuilder ConvertSpeed(this double value)
132+
{
133+
return new SpeedConversionBuilder(value);
134+
}
135+
129136
}

src/Univertall/NugetReadme.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# UniVertAll
2+
3+
UniVertAll is a versatile unit conversion library for .NET developers. It supports a wide range of units and provides a fluent API for easy and intuitive conversions. The library is compatible with multiple .NET versions, including .NET 6, .NET 7, .NET 8, and .NET 9.
4+
5+
## Features
6+
7+
- Comprehensive support for various units (length, mass, power, volume, etc.)
8+
- Fluent API for easy and intuitive conversions
9+
- Extensible with custom units and conversions
10+
- Robust error handling
11+
- Thread-safe
12+
- Compatible with multiple .NET versions
13+
- Packaged as a NuGet package for easy distribution
14+
15+
## Usage
16+
17+
### Sample Conversion
18+
19+
Here is an example of how to use the library for Power unit conversions:
20+
21+
```csharp
22+
using UnitConverter.Units.Power;
23+
24+
double watts = 746;
25+
double horsepower = watts.ConvertPower()
26+
.From(PowerUnit.Watt)
27+
.To(PowerUnit.MetricHorsepower);
28+
Console.WriteLine($"{watts} Watts is equal to {horsepower} Horsepower.");
29+
```
30+
31+
Thank you for using UniVertAll! We hope it makes your unit conversion tasks easier and more efficient.
32+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace Univertall.Units.Speed;
2+
3+
public class SpeedConversionBuilder
4+
{
5+
private readonly double _value;
6+
private SpeedUnit _fromUnit;
7+
8+
public SpeedConversionBuilder(double value)
9+
{
10+
_value = value;
11+
}
12+
13+
/// <summary>
14+
/// Sets the unit to convert from.
15+
/// </summary>
16+
/// <param name="fromUnit">The unit to convert from.</param>
17+
/// <returns>The current instance of <see cref="SpeedConversionBuilder"/>.</returns>
18+
public SpeedConversionBuilder From(SpeedUnit fromUnit)
19+
{
20+
_fromUnit = fromUnit;
21+
return this;
22+
}
23+
24+
/// <summary>
25+
/// Converts the value to the specified unit.
26+
/// </summary>
27+
/// <param name="toUnit">The unit to convert to.</param>
28+
/// <returns>The converted value.</returns>
29+
public double To(SpeedUnit toUnit)
30+
{
31+
var converter = new SpeedConverter();
32+
return converter.Convert(_value, _fromUnit, toUnit);
33+
}
34+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
namespace Univertall.Units.Speed;
2+
3+
/// <summary>
4+
/// Enum representing various speed units.
5+
/// </summary>
6+
public enum SpeedUnit
7+
{
8+
MetersPerSecond,
9+
KilometersPerHour,
10+
MilesPerHour,
11+
Knots,
12+
Knots_UK,
13+
FeetPerSecond,
14+
YardPerHour,
15+
Mach,
16+
Mach_SI
17+
}
18+
19+
/// <summary>
20+
/// Class for converting between different speed units.
21+
/// </summary>
22+
public class SpeedConverter : BaseUnivertall<SpeedUnit>
23+
{
24+
/// <summary>
25+
/// Converts the given value from the specified unit to the base unit (MetersPerSecond).
26+
/// </summary>
27+
/// <param name="value">The value to convert.</param>
28+
/// <param name="fromUnit">The unit to convert from.</param>
29+
/// <returns>The value in the base unit (MetersPerSecond).</returns>
30+
protected override double ToBaseUnit(double value, SpeedUnit fromUnit)
31+
{
32+
return fromUnit switch
33+
{
34+
SpeedUnit.MetersPerSecond => value,
35+
SpeedUnit.KilometersPerHour => value / 3.6,
36+
SpeedUnit.MilesPerHour => value * 0.44704,
37+
SpeedUnit.Knots => value * 0.5144444444,
38+
SpeedUnit.Knots_UK => value * 0.5147733333,
39+
SpeedUnit.FeetPerSecond => value * 0.3048,
40+
SpeedUnit.YardPerHour => value * 0.000254,
41+
SpeedUnit.Mach => value * 343.6,
42+
SpeedUnit.Mach_SI => value * 295.0464000003,
43+
_ => throw new ArgumentOutOfRangeException(nameof(fromUnit), fromUnit, null)
44+
};
45+
}
46+
47+
/// <summary>
48+
/// Converts the given value from the base unit (MetersPerSecond) to the specified unit.
49+
/// </summary>
50+
/// <param name="value">The value to convert.</param>
51+
/// <param name="toUnit">The unit to convert to.</param>
52+
/// <returns>The value in the specified unit.</returns>
53+
protected override double FromBaseUnit(double value, SpeedUnit toUnit)
54+
{
55+
return toUnit switch
56+
{
57+
SpeedUnit.MetersPerSecond => value,
58+
SpeedUnit.KilometersPerHour => value * 3.6,
59+
SpeedUnit.MilesPerHour => value / 0.44704,
60+
SpeedUnit.Knots => value / 0.5144444444,
61+
SpeedUnit.Knots_UK => value / 0.5147733333,
62+
SpeedUnit.FeetPerSecond => value / 0.3048,
63+
SpeedUnit.YardPerHour => value / 0.000254,
64+
SpeedUnit.Mach => value / 343.6,
65+
SpeedUnit.Mach_SI => value / 295.0464000003,
66+
_ => throw new ArgumentOutOfRangeException(nameof(toUnit), toUnit, null)
67+
};
68+
}
69+
}
70+

src/Univertall/Univertall.csproj

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFrameworks>net9.0;net8.0;net7.0;net6.0</TargetFrameworks>
@@ -7,7 +7,7 @@
77
<Nullable>enable</Nullable>
88

99
<PackageId>Univertall</PackageId>
10-
<PackageVersion>1.0.0</PackageVersion>
10+
<PackageVersion>1.1.0</PackageVersion>
1111
<Authors>Majid Shahabfar</Authors>
1212
<Description>Univertall is a versatile and comprehensive unit conversion library for .NET Core. It provides an easy-to-use API for converting between various units of measurement, including volume, pressure, length, mass, etc. Ideal for developers needing reliable unit conversion functionality in their applications.</Description>
1313
<PackageTags>unit conversion; .NET; library</PackageTags>
@@ -20,14 +20,10 @@
2020
<AssemblyCompany>Shahabfar</AssemblyCompany>
2121
<AssemblyProduct>Univertall</AssemblyProduct>
2222
<AssemblyCopyright>Majid Shahabfar 2024</AssemblyCopyright>
23-
<AssemblyVersion>1.0.0</AssemblyVersion>
24-
<FileVersion>1.0.0</FileVersion>
25-
<ProductVersion>1.0.0</ProductVersion>
26-
<PackageReadmeFile>readme.md</PackageReadmeFile>
23+
<AssemblyVersion>1.1.0</AssemblyVersion>
24+
<FileVersion>1.1.0</FileVersion>
25+
<ProductVersion>1.1.0</ProductVersion>
26+
<PackageReadmeFile>NugetReadme.md</PackageReadmeFile>
2727
</PropertyGroup>
2828

29-
<ItemGroup>
30-
<None Include="univertall.png" Pack="true" PackagePath="" />
31-
<None Include="..\..\readme.md" Pack="true" PackagePath="" />
32-
</ItemGroup>
3329
</Project>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using Univertall.Units.Speed;
2+
3+
namespace Univertall.Tests.UnitsTests;
4+
5+
public class SpeedConversionsTests
6+
{
7+
[Theory]
8+
[InlineData(1.0, SpeedUnit.MetersPerSecond, 1.0)]
9+
[InlineData(3.6, SpeedUnit.KilometersPerHour, 1.0)]
10+
[InlineData(2.23694, SpeedUnit.MilesPerHour, 1.0)]
11+
[InlineData(1.94384, SpeedUnit.Knots, 1.0)]
12+
[InlineData(3.28084, SpeedUnit.FeetPerSecond, 1.0)]
13+
[InlineData(3937.01, SpeedUnit.YardPerHour, 1.0)]
14+
[InlineData(0.00291036, SpeedUnit.Mach, 1.0)]
15+
[InlineData(0.0033893, SpeedUnit.Mach_SI, 1.0)]
16+
public void ToBaseUnit_ShouldConvertCorrectly(double value, SpeedUnit fromUnit, double expected)
17+
{
18+
// Arrange
19+
var converter = new SpeedConverter();
20+
21+
// Act
22+
var result = converter.Convert(value, fromUnit, SpeedUnit.MetersPerSecond);
23+
24+
// Assert
25+
Assert.Equal(expected, result, 5);
26+
}
27+
28+
[Theory]
29+
[InlineData(1.0, SpeedUnit.MetersPerSecond, 1.0)]
30+
[InlineData(1.0, SpeedUnit.KilometersPerHour, 3.6)]
31+
[InlineData(1.0, SpeedUnit.MilesPerHour, 2.23694)]
32+
[InlineData(1.0, SpeedUnit.Knots, 1.94384)]
33+
[InlineData(1.0, SpeedUnit.FeetPerSecond, 3.28084)]
34+
[InlineData(1.0, SpeedUnit.YardPerHour, 3937.007874)]
35+
[InlineData(1.0, SpeedUnit.Mach, 0.0029103609)]
36+
[InlineData(1.0, SpeedUnit.Mach_SI, 0.0033892974)]
37+
public void FromBaseUnit_ShouldConvertCorrectly(double value, SpeedUnit toUnit, double expected)
38+
{
39+
// Arrange
40+
var converter = new SpeedConverter();
41+
42+
// Act
43+
var result = converter.Convert(value, SpeedUnit.MetersPerSecond, toUnit);
44+
45+
// Assert
46+
Assert.Equal(expected, result, 5);
47+
}
48+
49+
[Theory]
50+
[InlineData(1.0, SpeedUnit.MetersPerSecond, SpeedUnit.KilometersPerHour, 3.6)]
51+
[InlineData(1.0, SpeedUnit.KilometersPerHour, SpeedUnit.MetersPerSecond, 0.27778)]
52+
[InlineData(1.0, SpeedUnit.MilesPerHour, SpeedUnit.KilometersPerHour, 1.60934)]
53+
[InlineData(1.0, SpeedUnit.Knots, SpeedUnit.MilesPerHour, 1.15078)]
54+
[InlineData(1.0, SpeedUnit.FeetPerSecond, SpeedUnit.MetersPerSecond, 0.3048)]
55+
[InlineData(1.0, SpeedUnit.YardPerHour, SpeedUnit.MetersPerSecond, 0.000254)]
56+
[InlineData(1.0, SpeedUnit.Mach, SpeedUnit.MetersPerSecond, 343.6)]
57+
[InlineData(1.0, SpeedUnit.Mach_SI, SpeedUnit.MetersPerSecond, 295.0464)]
58+
public void Convert_ShouldConvertCorrectly(double value, SpeedUnit fromUnit, SpeedUnit toUnit, double expected)
59+
{
60+
// Arrange
61+
var converter = new SpeedConverter();
62+
63+
// Act
64+
var result = converter.Convert(value, fromUnit, toUnit);
65+
66+
// Assert
67+
Assert.Equal(expected, result, 5);
68+
}
69+
}

0 commit comments

Comments
 (0)