Skip to content

rwbutler/LetterCase

Repository files navigation

LetterCase

CI Status Version Carthage compatible License Twitter

Lightweight library written in Swift for converting the letter case of a String. For more information take a look at the blog post or the table of contents below:

Features

  • Converts Strings to a variety of supported cases including: capitalized, kebab case, lower case, lower camel case, macro case, snake case, upper case and upper camel case.
  • Provides conversion from any letter case to another e.g. "the-quick-brown-fox-jumped-over-the-lazy-dog".convert(from: .kebab, to: .macro) prints THE_QUICK_BROWN_FOX_JUMPED_OVER_THE_LAZY_DOG
  • Implementations of JSONDecoder.KeyDecodingStrategy and JSONEncoder.KeyEncodingStrategy for decoding / encoding of JSON using Codable keys in just about any letter case.
  • Provides convenience methods on String for each of the supported cases e.g. "The Quick Brown Fox".kebabCased() emits "the-quick-brown-fox".

Installation

Cocoapods

CocoaPods is a dependency manager which integrates dependencies into your Xcode workspace. To install it using Ruby gems run:

gem install cocoapods

To install LetterCase using Cocoapods, simply add the following line to your Podfile:

pod "LetterCase"

Then run the command:

pod install

For more information see here.

Carthage

Carthage is a dependency manager which produces a binary for manual integration into your project. It can be installed via Homebrew using the commands:

brew update
brew install carthage

In order to integrate LetterCase into your project via Carthage, add the following line to your project's Cartfile:

github "rwbutler/LetterCase"

From the macOS Terminal run carthage update --platform iOS to build the framework then drag LetterCase.framework into your Xcode project.

For more information see here.

Swift Package Manager

Xcode 11 includes support for Swift Package Manager. In order to add LetterCase to your project in Xcode 11, from the File menu select Swift Packages and then select Add Package Dependency.

A dialogue will request the package repository URL which is:

https://github.com/rwbutler/LetterCase

After verifying the URL, Xcode will prompt you to select whether to pull a specific branch, commit or versioned release into your project.

Xcode 11 Package Options

Proceed to the next step by where you will be asked to select the package product to integrate into a target. There will be a single package product named LetterCase which should be pre-selected. Ensure that your main app target is selected from the rightmost column of the dialog then click Finish to complete the integration.

Xcode 11 Add Package

Letter Cases

case regular                     // No transformation applied.
case capitalized                 // e.g. Capitalized Case
case kebab                       // e.g. kebab-case
case lower                       // e.g. lower case
case lowerCamel                  // e.g. lowerCamelCase
case macro                       // e.g. MACRO_CASE
case snake                       // e.g. snakecase
case upper                       // e.g. UPPER CASE
case upperCamel                  // e.g. UpperCamelCase

Usage

In order to use LetterCase first import it using: import LetterCase. Then invoke one the convenience methods on String as follows:

let exampleString = "The quick brown fox jumped over the lazy dog."
let result = exampleString.letterCase(.kebab)
print(result)

Results in the following being printed:

the-quick-brown-fox-jumped-over-the-lazy-dog

Alternatively:

let exampleString = "The quick brown fox jumped over the lazy dog."
let result = exampleString.kebabCased()
print(result)

LetterCase Conversion

Use the convert function to convert from one letter case to another as follows:

let input = "the-quick-brown-fox-jumped-over-the-lazy-dog"
let result = input.convert(from: .kebab, to: .capitalized)
print(result) // Prints "The Quick Brown Fox Jumped Over The Lazy Dog"

JSON Decoding

To decode JSON with keys in kebab case e.g.

{
    "vehicles": [{
        "name": "car",
        "travels-on": "road",
        "number-of-wheels": 4
    }, {
        "name": "boat",
        "travels-on": "water",
        "number-of-wheels": 0
    }, {
        "name": "train",
        "travels-on": "rail",
        "number-of-wheels": 80
    }, {
        "name": "plane",
        "travels-on": "air",
        "number-of-wheels": 18
    }]
}

Specify the keyDecodingStrategy as follows:

let jsonData = try Data(contentsOf: jsonResourceURL)
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromKebabCase
let vehicles try decoder.decode(Vehicles.self, from: jsonData)

Available strategies include:

  • convertFromCapitalized
  • convertFromDashCase
  • convertFromKebabCase
  • convertFromLispCase
  • convertFromLowerCase
  • convertFromLowerCamelCase
  • convertFromMacroCase
  • convertFromScreamingSnakeCase
  • convertFromTrainCase
  • convertFromUpperCase
  • convertFromUpperCamelCase

JSON Encoding

To encode a Swift structure to JSON with keys in kebab case specify the keyEncodingStrategy as follows:

let encoder = JSONEncoder()
encoder.keyEncodingStrategy = .convertToKebabCase
let jsonData = try encoder.encode(vehicles)

Available strategies include:

  • convertToCapitalized
  • convertToDashCase
  • convertToKebabCase
  • convertToLispCase
  • convertToLowerCase
  • convertToLowerCamelCase
  • convertToMacroCase
  • convertToScreamingSnakeCase
  • convertToTrainCase
  • convertToUpperCase
  • convertToUpperCamelCase

Author

Ross Butler

License

LetterCase is available under the MIT license. See the LICENSE file for more info.

Additional Software

Controls

AnimatedGradientView
AnimatedGradientView

Frameworks

  • Cheats - Retro cheat codes for modern iOS apps.
  • Connectivity - Improves on Reachability for determining Internet connectivity in your iOS application.
  • FeatureFlags - Allows developers to configure feature flags, run multiple A/B or MVT tests using a bundled / remotely-hosted JSON configuration file.
  • FlexibleRowHeightGridLayout - A UICollectionView grid layout designed to support Dynamic Type by allowing the height of each row to size to fit content.
  • LetterCase - Lightweight library written in Swift for converting the letter case of a String.
  • Skylark - Fully Swift BDD testing framework for writing Cucumber scenarios using Gherkin syntax.
  • TailorSwift - A collection of useful Swift Core Library / Foundation framework extensions.
  • TypographyKit - Consistent & accessible visual styling on iOS with Dynamic Type support.
  • Updates - Automatically detects app updates and gently prompts users to update.
Cheats Connectivity FeatureFlags Skylark TypographyKit Updates
Cheats Connectivity FeatureFlags Skylark TypographyKit Updates

Tools

  • Clear DerivedData - Utility to quickly clear your DerivedData directory simply by typing cdd from the Terminal.
  • Config Validator - Config Validator validates & uploads your configuration files and cache clears your CDN as part of your CI process.
  • IPA Uploader - Uploads your apps to TestFlight & App Store.
  • Palette - Makes your TypographyKit color palette available in Xcode Interface Builder.
Config Validator IPA Uploader Palette
Config Validator IPA Uploader Palette