|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// A type performing transformation on provided `CodingKey`. |
| 4 | +/// |
| 5 | +/// Performs transformation on provided `CodingKey` |
| 6 | +/// based on the strategy passed during initialization. |
| 7 | +/// The separation letter and separated words capitalization |
| 8 | +/// style is adjusted according to the provided case style. |
| 9 | +struct CodingKeyTransformer { |
| 10 | + /// The key transformation strategy provided. |
| 11 | + let strategy: CodingKeys.Strategy |
| 12 | + |
| 13 | + /// Transform provided `CodingKey` string according |
| 14 | + /// to current strategy. |
| 15 | + /// |
| 16 | + /// Adjusts elements in provided `CodingKey` to match |
| 17 | + /// the current casing strategy. |
| 18 | + /// |
| 19 | + /// - Parameter key: The `CodingKey` to transform. |
| 20 | + /// - Returns: The transformed `CodingKey`. |
| 21 | + func transform(key: String) -> String { |
| 22 | + guard !key.isEmpty else { return key } |
| 23 | + |
| 24 | + let interimKey: String |
| 25 | + if #available( |
| 26 | + macOS 13, iOS 16, macCatalyst 16, |
| 27 | + tvOS 16, watchOS 9, * |
| 28 | + ) { |
| 29 | + let regex = #/([a-z0-9])([A-Z])/# |
| 30 | + interimKey = key.replacing(regex) { match in |
| 31 | + let (_, first, second) = match.output |
| 32 | + return "\(first)@\(second)" |
| 33 | + }.lowercased() |
| 34 | + } else { |
| 35 | + let regex = try! NSRegularExpression(pattern: "([a-z0-9])([A-Z])") |
| 36 | + let range = NSRange(location: 0, length: key.count) |
| 37 | + interimKey = regex.stringByReplacingMatches( |
| 38 | + in: key, |
| 39 | + range: range, |
| 40 | + withTemplate: "$1@$2" |
| 41 | + ).lowercased() |
| 42 | + } |
| 43 | + |
| 44 | + let parts = interimKey.components(separatedBy: .alphanumerics.inverted) |
| 45 | + return strategy.capitalization |
| 46 | + .transform(parts: parts) |
| 47 | + .joined(separator: strategy.separator) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +fileprivate extension CodingKeys.Strategy { |
| 52 | + /// The separator being used by current case style. |
| 53 | + /// |
| 54 | + /// There might not be any separator for current case style, |
| 55 | + /// in such case empty string is returned. Otherwise the separator |
| 56 | + /// character corresponding to current case is returned. |
| 57 | + var separator: String { |
| 58 | + switch self { |
| 59 | + case .camelCase, .PascalCase: |
| 60 | + return "" |
| 61 | + case .snake_case, .camel_Snake_Case, .SCREAMING_SNAKE_CASE: |
| 62 | + return "_" |
| 63 | + case .kebab-case, .Train-Case, .SCREAMING-KEBAB-CASE: |
| 64 | + return "-" |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +fileprivate extension CodingKeys.Strategy { |
| 70 | + /// Represents capitalization style |
| 71 | + /// of each token in a casing style. |
| 72 | + /// |
| 73 | + /// Indicates capitalization style preferred |
| 74 | + /// by each separated word in a casing style, |
| 75 | + /// i.e. upper, lower, only first letter is capitalized etc. |
| 76 | + enum Capitalization { |
| 77 | + /// Represents all the separated |
| 78 | + /// words are in upper case. |
| 79 | + /// |
| 80 | + /// Typically used for screaming |
| 81 | + /// style cases with separators. |
| 82 | + case upper |
| 83 | + /// Represents all the separated words |
| 84 | + /// have only first letter capitalized. |
| 85 | + /// |
| 86 | + /// Typically used for default |
| 87 | + /// style cases with separators. |
| 88 | + case lower |
| 89 | + /// Represents all the separated |
| 90 | + /// words are in lower case. |
| 91 | + /// |
| 92 | + /// Typically used for default |
| 93 | + /// style cases with separators. |
| 94 | + case all |
| 95 | + /// Represents first word is in lower case |
| 96 | + /// and subsequent words have only |
| 97 | + /// first letter capitalized. |
| 98 | + /// |
| 99 | + /// Typically used for styles that are variation |
| 100 | + /// on top of default styles. |
| 101 | + case exceptFirst |
| 102 | + |
| 103 | + /// Converts provided string tokens according |
| 104 | + /// to current casing style. |
| 105 | + /// |
| 106 | + /// Adjusts capitalization style of provided string tokens |
| 107 | + /// according to current casing style. |
| 108 | + /// |
| 109 | + /// - Parameter parts: The string tokens to transform. |
| 110 | + /// - Returns: The transformed string tokens. |
| 111 | + func transform(parts: [String]) -> [String] { |
| 112 | + guard !parts.isEmpty else { return parts } |
| 113 | + switch self { |
| 114 | + case .upper: |
| 115 | + return parts.map { $0.uppercased() } |
| 116 | + case .lower: |
| 117 | + return parts.map { $0.lowercased() } |
| 118 | + case .all: |
| 119 | + return parts.map { $0.uppercasingFirst } |
| 120 | + case .exceptFirst: |
| 121 | + let first = parts.first!.lowercasingFirst |
| 122 | + let rest = parts.dropFirst().map { $0.uppercasingFirst } |
| 123 | + return [first] + rest |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /// The capitalization casing style of each pattern |
| 129 | + /// corresponding to current strategy. |
| 130 | + /// |
| 131 | + /// Depending on the current style it might be upper, |
| 132 | + /// lower or capitalizing first word etc. |
| 133 | + var capitalization: Capitalization { |
| 134 | + switch self { |
| 135 | + case .camelCase, .camel_Snake_Case: |
| 136 | + return .exceptFirst |
| 137 | + case .snake_case, .kebab-case: |
| 138 | + return .lower |
| 139 | + case .SCREAMING_SNAKE_CASE, .SCREAMING-KEBAB-CASE: |
| 140 | + return .upper |
| 141 | + case .PascalCase, .Train-Case: |
| 142 | + return .all |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +/// Helps converting any string to camel case |
| 148 | +/// |
| 149 | +/// Picked up from: |
| 150 | +/// https://gist.github.com/reitzig/67b41e75176ddfd432cb09392a270218 |
| 151 | +extension String { |
| 152 | + /// Makes the first letter lowercase. |
| 153 | + var lowercasingFirst: String { prefix(1).lowercased() + dropFirst() } |
| 154 | + /// Makes the first letter uppercase. |
| 155 | + var uppercasingFirst: String { prefix(1).uppercased() + dropFirst() } |
| 156 | + |
| 157 | + /// Convert any string to camel case |
| 158 | + /// |
| 159 | + /// Removes non-alphanumeric characters |
| 160 | + /// and makes the letters just after these |
| 161 | + /// characters uppercase. |
| 162 | + /// |
| 163 | + /// First letter is made lowercase. |
| 164 | + var camelCased: String { |
| 165 | + return CodingKeyTransformer(strategy: .camelCase).transform(key: self) |
| 166 | + } |
| 167 | +} |
0 commit comments