From 41579ea99abae0a8d84286a94587a5a29796fc52 Mon Sep 17 00:00:00 2001 From: cdricms <36056008+cdricms@users.noreply.github.com> Date: Sat, 3 Jan 2026 17:44:39 +0100 Subject: [PATCH] Added servings --- Sources/Units/Units.swift | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Sources/Units/Units.swift b/Sources/Units/Units.swift index 2f287f6..758f1bd 100644 --- a/Sources/Units/Units.swift +++ b/Sources/Units/Units.swift @@ -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 { .init(value: self, unit: .kilojoule) } public var kcal: UnitValue { .init(value: self, unit: .kilocalorie) } + // MARK: Quantity Initializers + public var servings: UnitValue { .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? { converted(to: .kilojoule) } public var kcal: UnitValue? { converted(to: .kilocalorie) } + // MARK: Quantity Conversions + public var servings: UnitValue? { converted(to: .servings) } + } // MARK: - Height Conversion Extensions