Inflection

main

SwiftDocOrg/Inflection

Inflection

CI Documentation

A Swift port of the string inflection functionality from Rails ActiveSupport, with localizations for the following locales adapted from Inflections by @davidcelis:

  • Azerbaijani (az)
  • English (en)
  • Spanish (es)
  • French (fr)
  • Italian (it)
  • Kazakh (kk)
  • Latin (la)
  • Norwegian Bokmål (nb)
  • Brazilian Portuguese (pt-BR)
  • Swedish (se)
  • Turkish (tr)

Requirements

  • Swift 5.3+

Usage

import Inflection

// Rails-style inflectors

"employee_salary".camelized() // "employeeSalary"
"employee_salary".camelized(uppercasingFirstLetter: true) // "EmployeeSalary"

"employee_salary".dasherized() // "employee-salary"

"customer_support".humanized() // "Customer Support"
"parent_id".humanized() // "Parent"

"Donald E. Knuth".parameterized() // "donald-e-knuth"
"^très|Jolie-- ".parameterized() // "tres-jolie"

"TheIncredibleHulk".titleized() // "The Incredible Hulk"
"spider-man: far from home".titleized() // "Spider Man: Far From Home"
"guardians_of_the_galaxy".titleized() // "Guardians Of The Galaxy"

"HTTPRequest".underscored() // "http_request"

// Pluralization and singularization

for singular in ["person", "tomato", "matrix", "octopus", "fish"] {
    let plural = singular.pluralized()
    print("\(singular)\(plural)\(plural.singularized())")
}
/*
Prints:
person → people → person
tomato → tomatoes → tomato
matrix → matrices → matrix
octopus → octopi → octopus
fish → fish → fish
*/

// Pass a `locale` argument to pluralize in other languages:

let 🇮🇹 = Locale(identifier: "it-IT")
let singular = "ragno" // 🕷
let plural = singular.pluralized(with: 🇮🇹) // 🕷🕷🕷
print("\(singular)\(plural)\(plural.singularized())")
/*
Prints:
ragno → ragni → ragno
*/

// You can also add pluralization rules,
// including irregular and uncountable words:

let inflector = Inflector.default
inflector.addPluralRule(#"^i(Pod|Pad)( Mini)?$"#, replacement: #"i$1s$2"#)
inflector.addIrregular(singular: "lol", plural: "lolz")
inflector.addUncountable("Herokai")

for singular in ["iPad Mini", "lol", "Herokai"] {
    print("\(singular)\(singular.pluralized)")
}
/*
Prints:
iPad Mini → iPads Mini
lol → lolz
Herokai → Herokai
*/

Installation

Swift Package Manager

Add the Inflection package to your target dependencies in Package.swift:

// swift-tools-version:5.3
import PackageDescription

let package = Package(
  name: "YourProject",
  dependencies: [
    .package(
        url: "https://github.com/SwiftDoc/Inflection",
        from: "0.0.1"
    ),
  ]
)

Then run the swift build command to build your project.

License

MIT

Contact

Mattt (@mattt)

Description

  • Swift Tools 5.3.0
View More Packages from this Author

Dependencies

  • None
Last updated: Mon Oct 21 2024 14:50:18 GMT-0900 (Hawaii-Aleutian Daylight Time)