Skip to content

Instantly share code, notes, and snippets.

@dinneo
Forked from reitzig/Camelizer.swift
Created July 30, 2023 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dinneo/420252cc34ef0a59fa055b9b585dbffc to your computer and use it in GitHub Desktop.
Save dinneo/420252cc34ef0a59fa055b9b585dbffc to your computer and use it in GitHub Desktop.
Convert Swift strings to camel case
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