Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
extension String {
static func indent(level: Int) -> String {
guard level > 0 else {
return ""
}
let prefix = String(repeating: " ", count: level)
return prefix
}
var isNumber: Bool {
return !isEmpty && rangeOfCharacter(from: CharacterSet.decimalDigits.inverted) == nil
}
var removedWhitespaces: String {
let comps = components(separatedBy: CharacterSet.whitespacesAndNewlines)
return comps.joined(separator: "")
}
var lowerFirstLetter: String {
let first = String(prefix(1)).lowercased()
let other = String(dropFirst(1))
return first + other
}
var swiftProperty: String {
var first = String(prefix(1)).replacingOccurrences(of: "-", with: "negative")
if first.isNumber {
first = "_\(first)"
}
let other = String(dropFirst(1))
let property = (first + other)
.removedWhitespaces
.lowerFirstLetter
.replacingOccurrences(of: "-", with: "")
.replacingOccurrences(of: "/", with: "")
.replacingOccurrences(of: ")", with: "")
switch property {
case "class",
"extension",
"default",
"return",
"private",
"operator":
return "_\(property)"
default: return property
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment