Package for tokenizing and case swapping for free strings
There are a bunch of predefined case modifiers that you can use out of the box
| Modifiers | Examples |
|---|---|
upper |
string -> STRING |
lower |
STRING → string |
upperFirst |
strinG → StrinG |
lowerFirst |
STRINg → sTRINg |
capital |
some string → Some String |
swap |
Some String → sOME sTRING |
Any case modifiers can be combined with other ones by calling combined(with:), keep in mind that the order matters and transformations are applied sequentially
"myString".case(.lower.combined(with: .upperFirst)) // MystringThose modifiers are a bit more compex and do support configuration
String.Casification.PrefixPredicateallows to configure allowed prefixes,.swiftDeclarationsis used by default allowing$and_symbols for prefixes- You can also pass list of acronyms, default list can be found here
String.Casification.standardAcronyms, there is no way to modify this list at least yet, but you can explicitly specify your own, we'll add modification mechanism in future versions of the library
| Modifiers | Examples |
|---|---|
camel(.camel) |
some string → someString |
camel(.pascal) |
some string → SomeString |
camel |
some string → someString |
pascal |
some string → SomeString |
camel(.automatic) |
some string → someString |
camel(.automatic) |
Some string → SomeString |
snake() |
some string → some_string |
kebab() |
some string → some-string |
dot() |
some string → some.string |
Note
Check out Tests for more examples
For simple modifiers conforming your types to String.Casification.Modifier should be enough
extension String.Casification.Modifiers {
public struct Delete: String.Casification.Modifier {
public init() {}
@inlinable
public func transform(_ input: Substring) -> Substring {
""
}
}
}
extension String.Casification.Modifier
where Self == String.Casification.Modifiers.Delete {
@inlinable
public var delete: Self { .init() }
}
func test() {
"myString".case(.delete) // ""
}For complex processing you can process tokens instead of raw strings by creating a type conforming to String.Casification.TokensProcessor
extension String.Casification.TokensProcessors {
public struct RemoveSeparators: String.Casification.TokensProcessor {
public init() {}
@inlinable
public func processTokens(
_ tokens: ArraySlice<String.Casification.Token>
) -> ArraySlice<String.Casification.Token> {
return filter { $0.kind != .separator }[...]
}
}
}
// This declaration looks heavy, but allows to
// create a modifier from tokens processor without
// creating a separate modifier type
extension String.Casification.Modifier
where Self == String.Casification.Modifier.ProcessingTokens<
String.Casification.TokensProcessors.RemoveSeparators
>{
@inlinable
public var noSeparators: Self {
.init(using: .init())
}
}
func test() {
"my test-string".case(.noSeparators.combined(with: .upper)) // "MYTESTSTRING"
}You can add Casification to an Xcode project by adding it as a package dependency.
- From the File menu, select Swift Packages › Add Package Dependency…
- Enter
"https://github.com/capturecontext/swift-casification.git"into the package repository URL text field - Choose products you need to link them to your project.
If you use SwiftPM for your project, you can add StandardExtensions to your package file.
.package(
url: "https://github.com/capturecontext/swift-casification.git",
.upToNextMinor(from: "0.0.1")
)Do not forget about target dependencies:
.product(
name: "Casification",
package: "swift-casification"
)This library is released under the MIT license. See LICENSE for details.