-
-
Save ptrkstr/5787a235037b9d2a38191326cbe60b8b to your computer and use it in GitHub Desktop.
Convert Swift strings to camel case
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fileprivate let badChars = CharacterSet.alphanumerics.inverted | |
extension String { | |
var uppercasingFirst: String { | |
return prefix(1).uppercased() + dropFirst() | |
} | |
var lowercasingFirst: String { | |
return prefix(1).lowercased() + dropFirst() | |
} | |
var camelized: String { | |
guard !isEmpty else { | |
return "" | |
} | |
let parts = self.components(separatedBy: badChars) | |
let first = String(describing: parts.first!).lowercasingFirst | |
let rest = parts.dropFirst().map({String($0).uppercasingFirst}) | |
return ([first] + rest).joined(separator: "") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment