|
| 1 | +// |
| 2 | +// Inventory.swift |
| 3 | +// Units |
| 4 | +// |
| 5 | +// Created by Jason Jobe on 1/28/26. |
| 6 | +// |
| 7 | +import Foundation |
| 8 | + |
| 9 | +// MARK: SKU for Inventory |
| 10 | +public typealias SKU = URL |
| 11 | +public typealias Stock = Quantum |
| 12 | + |
| 13 | +public extension QuantumType { |
| 14 | + var sku: SKU { URL(sku: "\(self)") } |
| 15 | +} |
| 16 | + |
| 17 | +// MARK: Inventory - a collection of Quamtum |
| 18 | +public struct Inventory { |
| 19 | + public private(set) var items: [Stock] |
| 20 | + public var count: Int { items.count } |
| 21 | +} |
| 22 | + |
| 23 | +public extension Inventory { |
| 24 | + |
| 25 | + func adding(_ q: Quantum) -> Self { |
| 26 | + var newSelf = self |
| 27 | + newSelf.add(q) |
| 28 | + return newSelf |
| 29 | + } |
| 30 | + |
| 31 | + func subtracting(_ q: Quantum) -> Self { |
| 32 | + var newSelf = self |
| 33 | + newSelf.subtract(q) |
| 34 | + return newSelf |
| 35 | + } |
| 36 | + |
| 37 | + mutating func subtract(_ q: Quantum) { |
| 38 | + if let ndx = items.firstIndex(where: { $0.qtype == q.qtype }) { |
| 39 | + items[ndx].magnitude -= q.magnitude |
| 40 | + if items[ndx].magnitude == 0 { |
| 41 | + items.remove(at: ndx) |
| 42 | + } |
| 43 | + } else { |
| 44 | + items.append(q) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + mutating func add(_ q: Quantum) { |
| 49 | + if let ndx = items.firstIndex(where: { $0.qtype == q.qtype }) { |
| 50 | + items[ndx].magnitude += q.magnitude |
| 51 | + if items[ndx].magnitude == 0 { |
| 52 | + items.remove(at: ndx) |
| 53 | + } |
| 54 | + } else { |
| 55 | + items.append(q) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + func amount(of qt: QuantumType) -> Double { |
| 60 | + items.reduce(0) { partial, elem in partial + (elem.qtype == qt ? elem.magnitude : 0) } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +public extension Inventory { |
| 65 | + static func += (lhs: inout Self, rhs: Quantum) { |
| 66 | + lhs.add(rhs) |
| 67 | + } |
| 68 | + static func -= (lhs: inout Self, rhs: Quantum) { |
| 69 | + lhs.subtract(rhs) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +public extension Inventory { |
| 74 | + static func += (lhs: inout Self, rhs: Inventory) { |
| 75 | + rhs.items.forEach { lhs.add($0) } |
| 76 | + } |
| 77 | + |
| 78 | + static func -= (lhs: inout Self, rhs: Inventory) { |
| 79 | + rhs.items.forEach { lhs.subtract($0) } |
| 80 | + } |
| 81 | + |
| 82 | + static func *= (lhs: inout Self, rhs: Double) { |
| 83 | + for i in lhs.items.indices { |
| 84 | + lhs.items[i].magnitude *= rhs |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + static func /= (lhs: inout Self, rhs: Double) { |
| 89 | + for i in lhs.items.indices { |
| 90 | + lhs.items[i].magnitude /= rhs |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + static func + (lhs: Self, rhs: Inventory) -> Inventory { |
| 95 | + var result = lhs |
| 96 | + for element in rhs.items { |
| 97 | + result.add(element) |
| 98 | + } |
| 99 | + return result |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// MARK: SKU Extenstion to URL |
| 104 | +extension URL { |
| 105 | + /** |
| 106 | + A SKU URL has the scheme 'sku' and has the general format of |
| 107 | + sku:<host>/<id>:=material[25units] |
| 108 | + Guarenteed to create something |
| 109 | + */ |
| 110 | + public init(sku: String) { |
| 111 | + self = Self.parse(sku: sku) |
| 112 | + } |
| 113 | + |
| 114 | + public static func parse(sku: String) -> URL { |
| 115 | + if sku.lowercased().hasPrefix("sku:") { |
| 116 | + if let url = URL(string: sku) { |
| 117 | + return url |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Ensure we always create a valid URL with the `sku` scheme. |
| 122 | + // We build components and percent-encode the path so arbitrary SKU tokens are safe. |
| 123 | + var comps = URLComponents() |
| 124 | + comps.scheme = "sku" |
| 125 | + |
| 126 | + // Treat the whole string as the URL path |
| 127 | + // Valid URL MUST have single leading slash |
| 128 | + let rawPath = sku.hasPrefix("/") ? sku : "/" + sku |
| 129 | + // URLComponents expects path to be unescaped |
| 130 | + // it will handle encoding when producing .url |
| 131 | + comps.path = rawPath |
| 132 | + |
| 133 | + if let url = comps.url { |
| 134 | + return url |
| 135 | + } |
| 136 | + |
| 137 | + // Absolute fallback: construct from a minimally encoded string. |
| 138 | + // Replace spaces with %20 and remove illegal characters conservatively. |
| 139 | + let allowed = CharacterSet.urlPathAllowed.union(CharacterSet(charactersIn: "/")) |
| 140 | + let encodedPath = rawPath.unicodeScalars.map { allowed.contains($0) ? String($0) : String(format: "%%%02X", $0.value) }.joined() |
| 141 | + return URL(string: "sku:\\" + encodedPath) |
| 142 | + ?? URL(string: "sku:/UNKOWN")! |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +#if PLAY_TIME |
| 147 | +import Playgrounds |
| 148 | +#Playground { |
| 149 | + if let sku = URL(string: "sku:joe@wildthink.com/role[3]:=12345") |
| 150 | + { |
| 151 | + print("SKU:", sku) |
| 152 | + print("SKU.path:", sku.path) |
| 153 | + } else { |
| 154 | + print("BAD") |
| 155 | + } |
| 156 | +} |
| 157 | +#endif |
0 commit comments