orm/inflection
2016-11-08 15:44:29 +09:00
..
inflections_test.go gorm -> orm 2016-11-08 15:44:29 +09:00
inflections.go gorm -> orm 2016-11-08 15:44:29 +09:00
LICENSE gorm -> orm 2016-11-08 15:44:29 +09:00
README.md gorm -> orm 2016-11-08 15:44:29 +09:00

Inflection

Inflection pluralizes and singularizes English nouns

Basic Usage

inflection.Plural("person") => "people"
inflection.Plural("Person") => "People"
inflection.Plural("PERSON") => "PEOPLE"
inflection.Plural("bus")    => "buses"
inflection.Plural("BUS")    => "BUSES"
inflection.Plural("Bus")    => "Buses"

inflection.Singularize("people") => "person"
inflection.Singularize("People") => "Person"
inflection.Singularize("PEOPLE") => "PERSON"
inflection.Singularize("buses")  => "bus"
inflection.Singularize("BUSES")  => "BUS"
inflection.Singularize("Buses")  => "Bus"

inflection.Plural("FancyPerson") => "FancyPeople"
inflection.Singularize("FancyPeople") => "FancyPerson"

Register Rules

Standard rules are from Rails's ActiveSupport (https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflections.rb)

If you want to register more rules, follow:

inflection.AddUncountable("fish")
inflection.AddIrregular("person", "people")
inflection.AddPlural("(bu)s$", "${1}ses") # "bus" => "buses" / "BUS" => "BUSES" / "Bus" => "Buses"
inflection.AddSingular("(bus)(es)?$", "${1}") # "buses" => "bus" / "Buses" => "Bus" / "BUSES" => "BUS"