Energy units

This commit is contained in:
cdricms
2025-09-02 21:09:42 +02:00
parent 3d19cd6252
commit 0962ae2900

View File

@@ -33,6 +33,7 @@ public enum UnitCategory: String, CaseIterable, CustomStringConvertible,
case time case time
case temperature case temperature
case speed case speed
case energy
// Add more categories as needed // Add more categories as needed
public var description: String { public var description: String {
@@ -102,6 +103,11 @@ public enum Unit: String, CaseIterable, CustomStringConvertible, Codable,
case milesPerHour = "mph" case milesPerHour = "mph"
case knots = "kn" case knots = "kn"
// MARK: Energy units
case joule = "J"
case kilocalorie = "kcal"
case kilojoule = "kJ"
public var category: UnitCategory { public var category: UnitCategory {
switch self { switch self {
case .kilogram, .gram, .milligram, .microgram, .pound, .ounce, case .kilogram, .gram, .milligram, .microgram, .pound, .ounce,
@@ -119,6 +125,8 @@ public enum Unit: String, CaseIterable, CustomStringConvertible, Codable,
case .celsius, .fahrenheit, .kelvin: return .temperature case .celsius, .fahrenheit, .kelvin: return .temperature
case .metersPerSecond, .kilometersPerHour, .milesPerHour, .knots: case .metersPerSecond, .kilometersPerHour, .milesPerHour, .knots:
return .speed return .speed
case .kilocalorie, .kilojoule, .joule:
return .energy
} }
} }
@@ -173,6 +181,12 @@ public enum Unit: String, CaseIterable, CustomStringConvertible, Codable,
case .kilometersPerHour: 1000.0 / 3600.0 case .kilometersPerHour: 1000.0 / 3600.0
case .milesPerHour: 1609.344 / 3600.0 case .milesPerHour: 1609.344 / 3600.0
case .knots: 0.514444 case .knots: 0.514444
// Energy (Base: Joule)
case .joule: 1.0
case .kilojoule: 1000.0
case .kilocalorie: 4184.0
} }
} }
@@ -420,6 +434,7 @@ extension UnitCategory {
case .time: return .second case .time: return .second
case .temperature: return .celsius case .temperature: return .celsius
case .speed: return .metersPerSecond case .speed: return .metersPerSecond
case .energy: return .joule
} }
} }
} }
@@ -484,6 +499,11 @@ extension ConvertibleToDouble {
public var mph: UnitValue<Self> { .init(value: self, unit: .milesPerHour) } public var mph: UnitValue<Self> { .init(value: self, unit: .milesPerHour) }
public var kn: UnitValue<Self> { .init(value: self, unit: .knots) } public var kn: UnitValue<Self> { .init(value: self, unit: .knots) }
// MARK: Energy Initializers
public var J: UnitValue<Self> { .init(value: self, unit: .joule) }
public var kJ: UnitValue<Self> { .init(value: self, unit: .kilojoule) }
public var kcal: UnitValue<Self> { .init(value: self, unit: .kilocalorie) }
// MARK: - Subscripts for Numeric // MARK: - Subscripts for Numeric
/// Allows creating a UnitValue directly from a numeric literal using a Unit enum case. /// Allows creating a UnitValue directly from a numeric literal using a Unit enum case.
/// Example: `10[.kg]` /// Example: `10[.kg]`
@@ -557,6 +577,12 @@ extension UnitValue where ValueType: ConvertibleToDouble { // Apply constraints
public var kmh: UnitValue<Double>? { converted(to: .kilometersPerHour) } public var kmh: UnitValue<Double>? { converted(to: .kilometersPerHour) }
public var mph: UnitValue<Double>? { converted(to: .milesPerHour) } public var mph: UnitValue<Double>? { converted(to: .milesPerHour) }
public var kn: UnitValue<Double>? { converted(to: .knots) } public var kn: UnitValue<Double>? { converted(to: .knots) }
// MARK: Energy Conversions
public var J: UnitValue<Double>? { converted(to: .joule) }
public var kJ: UnitValue<Double>? { converted(to: .kilojoule) }
public var kcal: UnitValue<Double>? { converted(to: .kilocalorie) }
} }
// MARK: - Height Conversion Extensions // MARK: - Height Conversion Extensions