From 944b32d78261b23bfe2fff0b803a04d6e0af64a2 Mon Sep 17 00:00:00 2001 From: renatho Date: Mon, 31 Aug 2020 21:16:46 -0300 Subject: [PATCH] flect#47 - Add new rules at the beginning of rules array, so the new rules will be applied first reverse init for to standard rules --- plural_rules.go | 14 +++++++------- singular_rules.go | 8 ++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/plural_rules.go b/plural_rules.go index d14c4f5..61d34d3 100644 --- a/plural_rules.go +++ b/plural_rules.go @@ -6,18 +6,18 @@ var pluralRules = []rule{} func AddPlural(suffix string, repl string) { pluralMoot.Lock() defer pluralMoot.Unlock() - pluralRules = append(pluralRules, rule{ + pluralRules = append([]rule{{ suffix: suffix, fn: func(s string) string { s = s[:len(s)-len(suffix)] return s + repl }, - }) + }}, pluralRules...) - pluralRules = append(pluralRules, rule{ + pluralRules = append([]rule{{ suffix: repl, fn: noop, - }) + }}, pluralRules...) } var singleToPlural = map[string]string{ @@ -280,8 +280,8 @@ var singularToPluralSuffixList = []singularToPluralSuffix{ } func init() { - for _, suffix := range singularToPluralSuffixList { - AddPlural(suffix.singular, suffix.plural) - AddSingular(suffix.plural, suffix.singular) + for i := len(singularToPluralSuffixList) - 1; i >= 0; i-- { + AddPlural(singularToPluralSuffixList[i].singular, singularToPluralSuffixList[i].plural) + AddSingular(singularToPluralSuffixList[i].plural, singularToPluralSuffixList[i].singular) } } diff --git a/singular_rules.go b/singular_rules.go index b20371b..9b85a87 100644 --- a/singular_rules.go +++ b/singular_rules.go @@ -6,18 +6,18 @@ var singularRules = []rule{} func AddSingular(ext string, repl string) { singularMoot.Lock() defer singularMoot.Unlock() - singularRules = append(singularRules, rule{ + singularRules = append([]rule{{ suffix: ext, fn: func(s string) string { s = s[:len(s)-len(ext)] return s + repl }, - }) + }}, singularRules...) - singularRules = append(singularRules, rule{ + singularRules = append([]rule{{ suffix: repl, fn: func(s string) string { return s }, - }) + }}, singularRules...) }