Singularize converts the specified word into its singular version. For example: inflector.Singularize("people") // "person"
(word string)
| 62 | // |
| 63 | // inflector.Singularize("people") // "person" |
| 64 | func Singularize(word string) string { |
| 65 | if word == "" { |
| 66 | return "" |
| 67 | } |
| 68 | |
| 69 | for _, rule := range singularRules { |
| 70 | re := compiledPatterns.GetOrSet(rule.pattern, func() *regexp.Regexp { |
| 71 | re, err := regexp.Compile(rule.pattern) |
| 72 | if err != nil { |
| 73 | return nil |
| 74 | } |
| 75 | return re |
| 76 | }) |
| 77 | if re == nil { |
| 78 | // log only for debug purposes |
| 79 | log.Println("[Singularize] failed to retrieve/compile rule pattern " + rule.pattern) |
| 80 | continue |
| 81 | } |
| 82 | |
| 83 | if re.MatchString(word) { |
| 84 | return re.ReplaceAllString(word, rule.replacement) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return word |
| 89 | } |
searching dependent graphs…