Skip to content

Commit

Permalink
allowing to set acronyms on the inflections.json
Browse files Browse the repository at this point in the history
  • Loading branch information
paganotoni committed Nov 1, 2018
1 parent 8f6be1a commit 8d6c662
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
18 changes: 15 additions & 3 deletions flect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

Expand Down
18 changes: 14 additions & 4 deletions flect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
9 changes: 9 additions & 0 deletions ident.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -77,6 +85,7 @@ func toParts(s string) []string {
x += cs
continue
}

parts = xappend(parts, x)
x = ""
prev = c
Expand Down
3 changes: 3 additions & 0 deletions underscore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import (
)

func Test_Underscore(t *testing.T) {
baseAcronyms["TLC"] = true

table := []tt{
{"", ""},
{"bob dylan", "bob_dylan"},
{"Nice to see you!", "nice_to_see_you"},
{"*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 {
Expand Down

0 comments on commit 8d6c662

Please sign in to comment.