Added servings

This commit is contained in:
cdricms
2026-01-03 17:44:39 +01:00
parent 2d6d59cae7
commit 41579ea99a

View File

@@ -67,6 +67,7 @@ public enum UnitCategory: String, CaseIterable, CustomStringConvertible,
case temperature case temperature
case speed case speed
case energy case energy
case quantity
// Add more categories as needed // Add more categories as needed
public var description: String { public var description: String {
@@ -141,6 +142,9 @@ public enum Unit: String, CaseIterable, CustomStringConvertible, Codable,
case kilocalorie = "kcal" case kilocalorie = "kcal"
case kilojoule = "kJ" case kilojoule = "kJ"
// MARK: Quantity units
case servings = "servings"
public init?(rawValue: String) { public init?(rawValue: String) {
if let unit = Unit.allCases.first(where: { if let unit = Unit.allCases.first(where: {
$0.rawValue.lowercased() == rawValue.lowercased() $0.rawValue.lowercased() == rawValue.lowercased()
@@ -170,6 +174,8 @@ public enum Unit: String, CaseIterable, CustomStringConvertible, Codable,
return .speed return .speed
case .kilocalorie, .kilojoule, .joule: case .kilocalorie, .kilojoule, .joule:
return .energy return .energy
case .servings:
return .quantity
} }
} }
@@ -230,6 +236,9 @@ public enum Unit: String, CaseIterable, CustomStringConvertible, Codable,
case .kilojoule: 1000.0 case .kilojoule: 1000.0
case .kilocalorie: 4184.0 case .kilocalorie: 4184.0
// Quantity (Base: Servings)
case .servings: 1.0
} }
} }
@@ -522,6 +531,7 @@ extension UnitCategory {
case .temperature: return .celsius case .temperature: return .celsius
case .speed: return .metersPerSecond case .speed: return .metersPerSecond
case .energy: return .joule 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 kJ: UnitValue<Self> { .init(value: self, unit: .kilojoule) }
public var kcal: UnitValue<Self> { .init(value: self, unit: .kilocalorie) } 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 // 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]`
@@ -670,6 +683,9 @@ extension UnitValue where ValueType: ConvertibleToDouble { // Apply constraints
public var kJ: UnitValue<Double>? { converted(to: .kilojoule) } public var kJ: UnitValue<Double>? { converted(to: .kilojoule) }
public var kcal: UnitValue<Double>? { converted(to: .kilocalorie) } public var kcal: UnitValue<Double>? { converted(to: .kilocalorie) }
// MARK: Quantity Conversions
public var servings: UnitValue<Double>? { converted(to: .servings) }
} }
// MARK: - Height Conversion Extensions // MARK: - Height Conversion Extensions