/vendor/github.com/jinzhu/inflection/inflections.go

https://gitlab.com/prima101112/test-quiz · Go · 195 lines · 153 code · 19 blank · 23 comment · 9 complexity · 2d5feac2355863a78ba75d242a625818 MD5 · raw file

  1. /*
  2. Package inflection pluralizes and singularizes English nouns.
  3. inflection.Plural("person") => "people"
  4. inflection.Plural("Person") => "People"
  5. inflection.Plural("PERSON") => "PEOPLE"
  6. inflection.Singularize("people") => "person"
  7. inflection.Singularize("People") => "Person"
  8. inflection.Singularize("PEOPLE") => "PERSON"
  9. inflection.Plural("FancyPerson") => "FancydPeople"
  10. inflection.Singularize("FancyPeople") => "FancydPerson"
  11. Standard rules are from Rails's ActiveSupport (https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflections.rb)
  12. If you want to register more rules, follow:
  13. inflection.AddUncountable("fish")
  14. inflection.AddIrregular("person", "people")
  15. inflection.AddPlural("(bu)s$", "${1}ses") # "bus" => "buses" / "BUS" => "BUSES" / "Bus" => "Buses"
  16. inflection.AddSingular("(bus)(es)?$", "${1}") # "buses" => "bus" / "Buses" => "Bus" / "BUSES" => "BUS"
  17. */
  18. package inflection
  19. import (
  20. "regexp"
  21. "strings"
  22. )
  23. var pluralInflections = [][]string{
  24. []string{"([a-z])$", "${1}s"},
  25. []string{"s$", "s"},
  26. []string{"^(ax|test)is$", "${1}es"},
  27. []string{"(octop|vir)us$", "${1}i"},
  28. []string{"(octop|vir)i$", "${1}i"},
  29. []string{"(alias|status)$", "${1}es"},
  30. []string{"(bu)s$", "${1}ses"},
  31. []string{"(buffal|tomat)o$", "${1}oes"},
  32. []string{"([ti])um$", "${1}a"},
  33. []string{"([ti])a$", "${1}a"},
  34. []string{"sis$", "ses"},
  35. []string{"(?:([^f])fe|([lr])f)$", "${1}${2}ves"},
  36. []string{"(hive)$", "${1}s"},
  37. []string{"([^aeiouy]|qu)y$", "${1}ies"},
  38. []string{"(x|ch|ss|sh)$", "${1}es"},
  39. []string{"(matr|vert|ind)(?:ix|ex)$", "${1}ices"},
  40. []string{"^(m|l)ouse$", "${1}ice"},
  41. []string{"^(m|l)ice$", "${1}ice"},
  42. []string{"^(ox)$", "${1}en"},
  43. []string{"^(oxen)$", "${1}"},
  44. []string{"(quiz)$", "${1}zes"},
  45. }
  46. var singularInflections = [][]string{
  47. []string{"s$", ""},
  48. []string{"(ss)$", "${1}"},
  49. []string{"(n)ews$", "${1}ews"},
  50. []string{"([ti])a$", "${1}um"},
  51. []string{"((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$", "${1}sis"},
  52. []string{"(^analy)(sis|ses)$", "${1}sis"},
  53. []string{"([^f])ves$", "${1}fe"},
  54. []string{"(hive)s$", "${1}"},
  55. []string{"(tive)s$", "${1}"},
  56. []string{"([lr])ves$", "${1}f"},
  57. []string{"([^aeiouy]|qu)ies$", "${1}y"},
  58. []string{"(s)eries$", "${1}eries"},
  59. []string{"(m)ovies$", "${1}ovie"},
  60. []string{"(x|ch|ss|sh)es$", "${1}"},
  61. []string{"^(m|l)ice$", "${1}ouse"},
  62. []string{"(bus)(es)?$", "${1}"},
  63. []string{"(o)es$", "${1}"},
  64. []string{"(shoe)s$", "${1}"},
  65. []string{"(cris|test)(is|es)$", "${1}is"},
  66. []string{"^(a)x[ie]s$", "${1}xis"},
  67. []string{"(octop|vir)(us|i)$", "${1}us"},
  68. []string{"(alias|status)(es)?$", "${1}"},
  69. []string{"^(ox)en", "${1}"},
  70. []string{"(vert|ind)ices$", "${1}ex"},
  71. []string{"(matr)ices$", "${1}ix"},
  72. []string{"(quiz)zes$", "${1}"},
  73. []string{"(database)s$", "${1}"},
  74. }
  75. var irregularInflections = [][]string{
  76. []string{"person", "people"},
  77. []string{"man", "men"},
  78. []string{"child", "children"},
  79. []string{"sex", "sexes"},
  80. []string{"move", "moves"},
  81. []string{"mombie", "mombies"},
  82. }
  83. var uncountableInflections = []string{"equipment", "information", "rice", "money", "species", "series", "fish", "sheep", "jeans", "police"}
  84. type inflection struct {
  85. regexp *regexp.Regexp
  86. replace string
  87. }
  88. var compiledPluralMaps []inflection
  89. var compiledSingularMaps []inflection
  90. func compile() {
  91. compiledPluralMaps = []inflection{}
  92. compiledSingularMaps = []inflection{}
  93. for _, uncountable := range uncountableInflections {
  94. inf := inflection{
  95. regexp: regexp.MustCompile("^(?i)(" + uncountable + ")$"),
  96. replace: "${1}",
  97. }
  98. compiledPluralMaps = append(compiledPluralMaps, inf)
  99. compiledSingularMaps = append(compiledSingularMaps, inf)
  100. }
  101. for _, value := range irregularInflections {
  102. infs := []inflection{
  103. inflection{regexp: regexp.MustCompile(strings.ToUpper(value[0]) + "$"), replace: strings.ToUpper(value[1])},
  104. inflection{regexp: regexp.MustCompile(strings.Title(value[0]) + "$"), replace: strings.Title(value[1])},
  105. inflection{regexp: regexp.MustCompile(value[0] + "$"), replace: value[1]},
  106. }
  107. compiledPluralMaps = append(compiledPluralMaps, infs...)
  108. }
  109. for _, value := range irregularInflections {
  110. infs := []inflection{
  111. inflection{regexp: regexp.MustCompile(strings.ToUpper(value[1]) + "$"), replace: strings.ToUpper(value[0])},
  112. inflection{regexp: regexp.MustCompile(strings.Title(value[1]) + "$"), replace: strings.Title(value[0])},
  113. inflection{regexp: regexp.MustCompile(value[1] + "$"), replace: value[0]},
  114. }
  115. compiledSingularMaps = append(compiledSingularMaps, infs...)
  116. }
  117. for i := len(pluralInflections) - 1; i >= 0; i-- {
  118. value := pluralInflections[i]
  119. infs := []inflection{
  120. inflection{regexp: regexp.MustCompile(strings.ToUpper(value[0])), replace: strings.ToUpper(value[1])},
  121. inflection{regexp: regexp.MustCompile(value[0]), replace: value[1]},
  122. inflection{regexp: regexp.MustCompile("(?i)" + value[0]), replace: value[1]},
  123. }
  124. compiledPluralMaps = append(compiledPluralMaps, infs...)
  125. }
  126. for i := len(singularInflections) - 1; i >= 0; i-- {
  127. value := singularInflections[i]
  128. infs := []inflection{
  129. inflection{regexp: regexp.MustCompile(strings.ToUpper(value[0])), replace: strings.ToUpper(value[1])},
  130. inflection{regexp: regexp.MustCompile(value[0]), replace: value[1]},
  131. inflection{regexp: regexp.MustCompile("(?i)" + value[0]), replace: value[1]},
  132. }
  133. compiledSingularMaps = append(compiledSingularMaps, infs...)
  134. }
  135. }
  136. func init() {
  137. compile()
  138. }
  139. func AddPlural(key, value string) {
  140. pluralInflections = append(pluralInflections, []string{key, value})
  141. compile()
  142. }
  143. func AddSingular(key, value string) {
  144. singularInflections = append(singularInflections, []string{key, value})
  145. compile()
  146. }
  147. func AddIrregular(key, value string) {
  148. irregularInflections = append(irregularInflections, []string{key, value})
  149. compile()
  150. }
  151. func AddUncountable(value string) {
  152. uncountableInflections = append(uncountableInflections, value)
  153. compile()
  154. }
  155. func Plural(str string) string {
  156. for _, inflection := range compiledPluralMaps {
  157. if inflection.regexp.MatchString(str) {
  158. return inflection.regexp.ReplaceAllString(str, inflection.replace)
  159. }
  160. }
  161. return str
  162. }
  163. func Singular(str string) string {
  164. for _, inflection := range compiledSingularMaps {
  165. if inflection.regexp.MatchString(str) {
  166. return inflection.regexp.ReplaceAllString(str, inflection.replace)
  167. }
  168. }
  169. return str
  170. }