-
Notifications
You must be signed in to change notification settings - Fork 2
Home
A string inflector.
public final class InflectorAn inflection rule.
public typealias Rule = (String) -> String?Creates a new inflector.
public init(locale: Locale = .current, caseSensitive: Bool = false, normalizationSensitive: Bool = false)- locale: The locale used to determine character case mapping.
- caseSensitive: Whether inflection rules based on regular expressions are case-sensitive.
falseby default. - normalizationSensitive: Whether inflection rules based on regular expressions are normalization-sensitive.
falseby default.b
The locale used to determine character case mapping.
let locale: LocaleWhether inflection rules based on regular expressions are case-sensitive.
let caseSensitive: BoolWhether inflection rules based on regular expressions are normalization-sensitive.
let normalizationSensitive: BoolThe default string inflector.
let `default`: Inflector = enThe default string inflector for the specified locale, if any.
public class func `default`(for locale: Locale) -> Inflector?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- pattern: The regular expression pattern.
- template: The replacement template for matches.
- options: Regular expression options.
An error if a regular expression couldn't be constructed.
Adds a pluralization rule.
public func addPluralizationRule(_ rule: @escaping Rule)- rule: The pluralization rule.
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- pattern: The regular expression pattern.
- template: The replacement template for matches.
- options: Regular expression options.
An error if a regular expression couldn't be constructed.
Adds a humanization rule.
public func addHumanizationRule(_ rule: @escaping Rule)- rule: The humanization rule.
Adds a humanization rule with a given string and replacement.
public func addHumanizationRule(replacing original: String, with replacement: String)- original: The string to be replaced.
- replacement: The replacement string.
Adds an irregular pluralization / singularization rule.
@discardableResult public func addIrregular(singular: String, plural: String) -> (inserted: Bool, memberAfterInsert: (singular: String, plural: String))- singular: The singular form.
- plural: The plural form.
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.
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))- word: The uncountable word.
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.
Adds an acronym.
@discardableResult public func addAcronym(_ word: String) -> (inserted: Bool, memberAfterInsert: (String))- word: The acronym.
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.
Returns the string removing underscores and indicating word boundaries with a single capitalized letter.
public func camelize(_ term: String, uppercasingFirstLetter: Bool = true) -> StringInflector.default.camlize("employee_salary") // => "employeeSalary"
Inflector.default.camlize("employee_salary, uppercasingFirstLetter: true) // => "EmployeeSalary"- term: The string to be camel-cased.
- uppercasingFirstLetter: Whether to uppercase the first letter of the resulting string.
falseby default.
Returns the string replacing each underscore (_) with a hypen / minus sign (-).
public func dasherize(_ term: String) -> StringInflector.dasherize("puni_puni") // => "puni-puni"- term: The string to be dasherized.
Returns a form of the string suitable for display to users.
public func humanize(_ term: String, capitalizing: Bool = true, preservingIDSuffix: Bool = false) -> StringThis 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.
- term: The string to be humanized.
- capitalizing: Whether to capitalize the resulting string.
trueby default. - preservingIDSuffix: Whether to preserve an "_id" suffix, if present.
falseby default.
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) -> StringInflector.default.parameterize("Donald E. Knuth") // => "donald-e-knuth"- 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.
falseby default.
Returns the pluralized form of the string, or the original string if no pluralization rules match.
public func pluralize(_ word: String) -> StringInflector.default.pluralize("cow") // => "cows"
Inflector.default.pluralize("octopus") // => "octopi"
Inflector.default.pluralize("sheep") // => "sheep"
Inflector.default.pluralize("dogs") // => "dogs"- word: The word to pluralize.
Returns the pluralized form of the string, or the original string if no pluralization rules match.
public func singularize(_ word: String) -> StringInflector.default.singularize("cows") // => "cow"
Inflector.default.singularize("octopi") // => "octopus"
Inflector.default.singularize("sheep") // => "sheep"
Inflector.default.singularize("dog") // => "dog"- word: The word to singularize.
Adds a singularization rule.
public func addSingularizationRule(_ rule: @escaping Rule)- rule: The singularization rule.
Returns a titlecase form of the string by capitalizing and replacing certain characters.
public func titleize(_ term: String, preservingIDSuffix: Bool = false) -> StringInflector.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"- term: The string to be titleized.
- preservingIDSuffix: Whether to preserve an "_id" suffix, if present.
falseby default.
public func underscore(_ term: String) -> String