Skip to content
This repository was archived by the owner on Oct 17, 2021. It is now read-only.
mattt edited this page Jan 5, 2021 · 2 revisions

Inflector

A string inflector.

public final class Inflector

Nested Type Aliases

Rule

An inflection rule.

public typealias Rule = (String) -> String?

Initializers

init(locale:caseSensitive:normalizationSensitive:)

Creates a new inflector.

public init(locale: Locale = .current, caseSensitive: Bool = false, normalizationSensitive: Bool = false)

Parameters

  • locale: The locale used to determine character case mapping.
  • caseSensitive: Whether inflection rules based on regular expressions are case-sensitive. false by default.
  • normalizationSensitive: Whether inflection rules based on regular expressions are normalization-sensitive. false by default.b

Properties

locale

The locale used to determine character case mapping.

let locale: Locale

caseSensitive

Whether inflection rules based on regular expressions are case-sensitive.

let caseSensitive: Bool

normalizationSensitive

Whether inflection rules based on regular expressions are normalization-sensitive.

let normalizationSensitive: Bool

`default`

The default string inflector.

let `default`: Inflector = en

Methods

`default`(for:)

The default string inflector for the specified locale, if any.

public class func `default`(for locale: Locale) -> Inflector?

addSingularizationRule(matching:replacement:options:)

Adds a singularization rule using a regular expression with the provided pattern, replacement template, and options.

public func addSingularizationRule(matching pattern: String, replacement template: String, options: NSRegularExpression.Options? = nil) throws

Parameters

  • pattern: The regular expression pattern.
  • template: The replacement template for matches.
  • options: Regular expression options.

Throws

An error if a regular expression couldn't be constructed.

addPluralizationRule(_:)

Adds a pluralization rule.

public func addPluralizationRule(_ rule: @escaping Rule)

Parameters

  • rule: The pluralization rule.

addPluralizationRule(matching:replacement:options:)

Adds a pluralization rule using a regular expression with the provided pattern, replacement template, and options.

public func addPluralizationRule(matching pattern: String, replacement template: String, options: NSRegularExpression.Options? = nil) throws

Parameters

  • pattern: The regular expression pattern.
  • template: The replacement template for matches.
  • options: Regular expression options.

Throws

An error if a regular expression couldn't be constructed.

addHumanizationRule(_:)

Adds a humanization rule.

public func addHumanizationRule(_ rule: @escaping Rule)

Parameters

  • rule: The humanization rule.

addHumanizationRule(replacing:with:)

Adds a humanization rule with a given string and replacement.

public func addHumanizationRule(replacing original: String, with replacement: String)

Parameters

  • original: The string to be replaced.
  • replacement: The replacement string.

addIrregular(singular:plural:)

Adds an irregular pluralization / singularization rule.

@discardableResult public func addIrregular(singular: String, plural: String) -> (inserted: Bool, memberAfterInsert: (singular: String, plural: String))

Parameters

  • singular: The singular form.
  • plural: The plural form.

Returns

A tuple with two members. The first is a Bool indicating whether the rule was inserted. The second is a tuple containing the inserted singular and plural forms.

addUncountable(_:)

Adds an irregular pluralization / singularization rule for words that don't have a distinct plural or singular form.

@discardableResult public func addUncountable(_ word: String) -> (inserted: Bool, memberAfterInsert: (String))

Parameters

  • word: The uncountable word.

Returns

A tuple with two members. The first is a Bool indicating whether the rule was inserted. The second is a tuple containing the inserted word.

addAcronym(_:)

Adds an acronym.

@discardableResult public func addAcronym(_ word: String) -> (inserted: Bool, memberAfterInsert: (String))

Parameters

  • word: The acronym.

Returns

A tuple with two members. The first is a Bool indicating whether the rule was inserted. The second is a tuple containing the inserted word.

camelize(_:uppercasingFirstLetter:)

Returns the string removing underscores and indicating word boundaries with a single capitalized letter.

public func camelize(_ term: String, uppercasingFirstLetter: Bool = true) -> String
Inflector.default.camlize("employee_salary") // => "employeeSalary"
Inflector.default.camlize("employee_salary, uppercasingFirstLetter: true) // => "EmployeeSalary"

Parameters

  • term: The string to be camel-cased.
  • uppercasingFirstLetter: Whether to uppercase the first letter of the resulting string. false by default.

dasherize(_:)

Returns the string replacing each underscore (_) with a hypen / minus sign (-).

public func dasherize(_ term: String) -> String
Inflector.dasherize("puni_puni") // => "puni-puni"

Parameters

  • term: The string to be dasherized.

humanize(_:capitalizing:preservingIDSuffix:)

Returns a form of the string suitable for display to users.

public func humanize(_ term: String, capitalizing: Bool = true, preservingIDSuffix: Bool = false) -> String

This function performs the following transformations:

  • Applies human inflection rules to the argument.

  • Deletes leading underscores, if any.

  • Removes an "_id" suffix, if present.

  • Replaces underscores with spaces, if any.

  • Downcases all words except acronyms.

  • Capitalizes the first word.

Parameters

  • term: The string to be humanized.
  • capitalizing: Whether to capitalize the resulting string. true by default.
  • preservingIDSuffix: Whether to preserve an "_id" suffix, if present. false by default.

parameterize(_:separator:preservingCase:)

Returns a form of the string suitable to be used in a URL path component.

public func parameterize(_ string: String, separator: Character = "-", preservingCase: Bool = false) -> String
Inflector.default.parameterize("Donald E. Knuth") // => "donald-e-knuth"

Parameters

  • string: The string to be parameterized.
  • separator: The character used to replace whitespace. A hyphen-minus sign ("-") by default.
  • preservingCase: Whether to preserve the case instead of lowercasing. false by default.

pluralize(_:)

Returns the pluralized form of the string, or the original string if no pluralization rules match.

public func pluralize(_ word: String) -> String
Inflector.default.pluralize("cow") // => "cows"
Inflector.default.pluralize("octopus") // => "octopi"
Inflector.default.pluralize("sheep") // => "sheep"
Inflector.default.pluralize("dogs") // => "dogs"

Parameters

  • word: The word to pluralize.

singularize(_:)

Returns the pluralized form of the string, or the original string if no pluralization rules match.

public func singularize(_ word: String) -> String
Inflector.default.singularize("cows") // => "cow"
Inflector.default.singularize("octopi") // => "octopus"
Inflector.default.singularize("sheep") // => "sheep"
Inflector.default.singularize("dog") // => "dog"

Parameters

  • word: The word to singularize.

addSingularizationRule(_:)

Adds a singularization rule.

public func addSingularizationRule(_ rule: @escaping Rule)

Parameters

  • rule: The singularization rule.

titleize(_:preservingIDSuffix:)

Returns a titlecase form of the string by capitalizing and replacing certain characters.

public func titleize(_ term: String, preservingIDSuffix: Bool = false) -> String
Inflector.default.titleize("raiders_of_the_lost_ark") // => "Raiders Of The Lost Ark"
Inflector.default.titleize("x-men: the last stand") // => "X-Men: The Last Stand"
Inflector.default.titleize("TheManWithoutAPast") // => "The Man Without A Past"

Parameters

  • term: The string to be titleized.
  • preservingIDSuffix: Whether to preserve an "_id" suffix, if present. false by default.

underscore(_:)

public func underscore(_ term: String) -> String