Added servings
This commit is contained in:
@@ -67,6 +67,7 @@ public enum UnitCategory: String, CaseIterable, CustomStringConvertible,
|
||||
case temperature
|
||||
case speed
|
||||
case energy
|
||||
case quantity
|
||||
// Add more categories as needed
|
||||
|
||||
public var description: String {
|
||||
@@ -141,6 +142,9 @@ public enum Unit: String, CaseIterable, CustomStringConvertible, Codable,
|
||||
case kilocalorie = "kcal"
|
||||
case kilojoule = "kJ"
|
||||
|
||||
// MARK: Quantity units
|
||||
case servings = "servings"
|
||||
|
||||
public init?(rawValue: String) {
|
||||
if let unit = Unit.allCases.first(where: {
|
||||
$0.rawValue.lowercased() == rawValue.lowercased()
|
||||
@@ -170,6 +174,8 @@ public enum Unit: String, CaseIterable, CustomStringConvertible, Codable,
|
||||
return .speed
|
||||
case .kilocalorie, .kilojoule, .joule:
|
||||
return .energy
|
||||
case .servings:
|
||||
return .quantity
|
||||
}
|
||||
}
|
||||
|
||||
@@ -230,6 +236,9 @@ public enum Unit: String, CaseIterable, CustomStringConvertible, Codable,
|
||||
case .kilojoule: 1000.0
|
||||
case .kilocalorie: 4184.0
|
||||
|
||||
// Quantity (Base: Servings)
|
||||
case .servings: 1.0
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -522,6 +531,7 @@ extension UnitCategory {
|
||||
case .temperature: return .celsius
|
||||
case .speed: return .metersPerSecond
|
||||
case .energy: return .joule
|
||||
case .quantity: return .servings
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -591,6 +601,9 @@ extension ConvertibleToDouble {
|
||||
public var kJ: UnitValue<Self> { .init(value: self, unit: .kilojoule) }
|
||||
public var kcal: UnitValue<Self> { .init(value: self, unit: .kilocalorie) }
|
||||
|
||||
// MARK: Quantity Initializers
|
||||
public var servings: UnitValue<Self> { .init(value: self, unit: .servings) }
|
||||
|
||||
// MARK: - Subscripts for Numeric
|
||||
/// Allows creating a UnitValue directly from a numeric literal using a Unit enum case.
|
||||
/// Example: `10[.kg]`
|
||||
@@ -670,6 +683,9 @@ extension UnitValue where ValueType: ConvertibleToDouble { // Apply constraints
|
||||
public var kJ: UnitValue<Double>? { converted(to: .kilojoule) }
|
||||
public var kcal: UnitValue<Double>? { converted(to: .kilocalorie) }
|
||||
|
||||
// MARK: Quantity Conversions
|
||||
public var servings: UnitValue<Double>? { converted(to: .servings) }
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Height Conversion Extensions
|
||||
|
||||
Reference in New Issue
Block a user