Refactoring

This commit is contained in:
Sven Weidauer 2020-12-30 12:50:26 +01:00
parent cf00c1cd30
commit 7d277a0e43
10 changed files with 384 additions and 312 deletions

View file

@ -0,0 +1,7 @@
import Foundation
extension FileWrapper {
convenience init(_ string: String) {
self.init(regularFileWithContents: Data(string.utf8))
}
}

View file

@ -0,0 +1,40 @@
extension StringProtocol {
var capitalizeFirst: String {
guard !isEmpty else {
return String(self)
}
return prefix(1).uppercased() + dropFirst()
}
var lowercasedFirst: String {
guard !isEmpty else {
return String(self)
}
return prefix(1).lowercased() + dropFirst()
}
func droppingSuffix(_ suffix: String) -> SubSequence {
guard hasSuffix(suffix) else {
return self[...]
}
return dropLast(suffix.count)
}
func insertCamelCaseSeparators(separator: String = " ") -> String {
replacingOccurrences(
of: "(?<=[a-z0-9])([A-Z])",
with: "\(separator)$1",
options: .regularExpression,
range: nil
)
}
func camelCasePathToSnakeCase() -> String {
insertCamelCaseSeparators(separator: "_")
.replacingOccurrences(of: "/", with: "_")
.lowercased()
}
}