diff --git a/flect.go b/flect.go index 7a4cc4c..4dbfc96 100644 --- a/flect.go +++ b/flect.go @@ -35,7 +35,8 @@ func init() { //LoadReader loads rules from io.Reader param func LoadReader(r io.Reader) error { - m := map[string]string{} + m := map[string]interface{}{} + err := json.NewDecoder(r).Decode(&m) if err != nil { return fmt.Errorf("could not decode inflection JSON from reader: %s", err) @@ -46,9 +47,20 @@ func LoadReader(r io.Reader) error { defer singularMoot.Unlock() for s, p := range m { - singleToPlural[s] = p - pluralToSingle[p] = s + if ps, ok := p.(string); ok { + singleToPlural[s] = ps + pluralToSingle[ps] = s + } + + if pa, ok := p.([]interface{}); ok && s == "_acronyms" { + for _, acronym := range pa { + key := (acronym).(string) + baseAcronyms[key] = true + } + } + } + return nil } diff --git a/flect_test.go b/flect_test.go index 998a639..c2d5105 100644 --- a/flect_test.go +++ b/flect_test.go @@ -16,21 +16,31 @@ type tt struct { func Test_LoadReader(t *testing.T) { r := require.New(t) // in := `{"beatle": "the beatles", "xyz": "zyx"}` - m := map[string]string{ - "beatle": "the beatles", - "xyz": "zyx", + m := map[string]interface{}{ + "beatle": "the beatles", + "xyz": "zyx", + "_acronyms": []string{"TLC", "LSA"}, } b, err := json.Marshal(m) r.NoError(err) r.NoError(LoadReader(bytes.NewReader(b))) - for k, v := range m { + + tcases := map[string]string{ + "beatle": "the beatles", + "xyz": "zyx", + } + + for k, v := range tcases { r.Equal(v, Pluralize(k)) r.Equal(v, Pluralize(v)) r.Equal(k, Singularize(k)) r.Equal(k, Singularize(v)) } + + r.True(baseAcronyms["TLC"]) + r.True(baseAcronyms["LSA"]) } var singlePluralAssertions = []tt{ diff --git a/ident.go b/ident.go index f70f175..1987819 100644 --- a/ident.go +++ b/ident.go @@ -66,6 +66,14 @@ func toParts(s string) []string { prev = c continue } + + if baseAcronyms[x] { + parts = xappend(parts, x) + x = cs + prev = c + continue + } + if unicode.IsUpper(c) && !unicode.IsUpper(prev) { parts = xappend(parts, x) x = cs @@ -77,6 +85,7 @@ func toParts(s string) []string { x += cs continue } + parts = xappend(parts, x) x = "" prev = c diff --git a/underscore_test.go b/underscore_test.go index 3a85380..2425738 100644 --- a/underscore_test.go +++ b/underscore_test.go @@ -7,6 +7,8 @@ import ( ) func Test_Underscore(t *testing.T) { + baseAcronyms["TLC"] = true + table := []tt{ {"", ""}, {"bob dylan", "bob_dylan"}, @@ -14,6 +16,7 @@ func Test_Underscore(t *testing.T) { {"*hello*", "hello"}, {"i've read a book! have you?", "ive_read_a_book_have_you"}, {"This is `code` ok", "this_is_code_ok"}, + {"TLCForm", "tlc_form"}, } for _, tt := range table {