Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.Support for a new flag --strategy to remove vowels from input if no… #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Flags:
-m, --max int Maximum length of string, keep on abbreviating while the string is longer than this limit
-n, --newline Add newline to the end of the string (default true)
-s, --set string Abbreviation set (default "common")
-r, --strategy Set a strategy to use if no match is found for input string(allowed value "removeVowel")

Use "abbreviate [command] --help" for more information about a command.
```
Expand All @@ -88,6 +89,10 @@ stg+ltd

$ abbreviate separated StrategyLimited
stgltd

$ abbreviate original --strategy removeVowel WhatToDo
WhtTD

```

## Code of Conduct
Expand Down
2 changes: 1 addition & 1 deletion cmd/camel.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var camelCmd = &cobra.Command{
Long: `Abbreviate a string and convert it to camel case.`,
Args: validateArgPresent,
Run: func(cmd *cobra.Command, args []string) {
abbr := domain.AsPascal(matcher, args[0], optMax, optFrmFront)
abbr := domain.AsPascal(matcher, args[0], optMax, optFrmFront, optStrategy)

ch := string(abbr[0])

Expand Down
2 changes: 1 addition & 1 deletion cmd/kebab.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Where a string is not shortened, it will be converted to kebab case anyway, even
if this means that the string will end up longer.`,
Args: validateArgPresent,
Run: func(cmd *cobra.Command, args []string) {
abbr := domain.AsSeparated(matcher, args[0], optKebabSeperator, optMax, optFrmFront)
abbr := domain.AsSeparated(matcher, args[0], optKebabSeperator, optMax, optFrmFront,optStrategy)

fmt.Printf("%s", abbr)
if optNewline {
Expand Down
2 changes: 1 addition & 1 deletion cmd/original.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var originalCmd = &cobra.Command{
Short: "Abbreviate the string using the original word boundary separators",
Args: validateArgPresent,
Run: func(cmd *cobra.Command, args []string) {
abbr := domain.AsOriginal(matcher, args[0], optMax, optFrmFront)
abbr := domain.AsOriginal(matcher, args[0], optMax, optFrmFront,optStrategy)

fmt.Printf("%s", abbr)
if optNewline {
Expand Down
2 changes: 1 addition & 1 deletion cmd/pascal.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var pascalCmd = &cobra.Command{
Long: `Abbreviate a string and convert it to pascal case.`,
Args: validateArgPresent,
Run: func(cmd *cobra.Command, args []string) {
abbr := domain.AsPascal(matcher, args[0], optMax, optFrmFront)
abbr := domain.AsPascal(matcher, args[0], optMax, optFrmFront,optStrategy)

fmt.Printf("%s", abbr)
if optNewline {
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
optCustom = ""
optMax = 0
optFrmFront = false
optStrategy = ""

matcher *domain.Matcher
)
Expand Down Expand Up @@ -120,6 +121,7 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&optCustom, "custom", "c", optCustom, "Custom abbreviation set")
rootCmd.PersistentFlags().IntVarP(&optMax, "max", "m", optMax, "Maximum length of string, keep on abbreviating while the string is longer than this limit")
rootCmd.PersistentFlags().BoolVarP(&optFrmFront, "from-front", "", optFrmFront, "Shorten from the front")
rootCmd.PersistentFlags().StringVarP(&optStrategy, "strategy", "r", optStrategy, "Use remove vowel strategy")
}

func validateArgPresent(cmd *cobra.Command, args []string) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/separated.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Where a string is not shortened, it will be converted to your case anyway, even
if this means that the string will end up longer.`,
Args: validateArgPresent,
Run: func(cmd *cobra.Command, args []string) {
abbr := domain.AsSeparated(matcher, args[0], optSeperator, optMax, optFrmFront)
abbr := domain.AsSeparated(matcher, args[0], optSeperator, optMax, optFrmFront,optStrategy)

fmt.Printf("%s", abbr)
if optNewline {
Expand Down
2 changes: 1 addition & 1 deletion cmd/snake.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Where a string is not shortened, it will be converted to snake case anyway, even
if this means that the string will end up longer.`,
Args: validateArgPresent,
Run: func(cmd *cobra.Command, args []string) {
abbr := domain.AsSeparated(matcher, args[0], optSnakeSeperator, optMax, optFrmFront)
abbr := domain.AsSeparated(matcher, args[0], optSnakeSeperator, optMax, optFrmFront, optStrategy)

fmt.Printf("%s", abbr)
if optNewline {
Expand Down
34 changes: 33 additions & 1 deletion domain/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package domain

import (
"fmt"
"os"
"sort"
"strings"
)
Expand All @@ -18,13 +19,44 @@ func NewMatcher(items map[string]string) *Matcher {
}
}

func isVowel(r rune) bool {
switch r {
case 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U':
return true
}
return false
}

func guessMatchByVowel(word string) string {

result := ""
for _, character := range word {
if !isVowel(character) {
result += string(character)
}
}
return result
}

// Match against a list of mappings
func (matcher Matcher) Match(word string) string {
func (matcher Matcher) Match(word string, strategy string) string {
if abbr, found := matcher.items[word]; found {
return abbr
}

switch strategy {
case "":
return word
case "removeVowel":
guessWord := guessMatchByVowel(word)
return guessWord
default:
fmt.Println("Invalid strategy provided")
os.Exit(0)
}

return word

}

// All of the abbreviations in this set in order of the linked word
Expand Down
24 changes: 16 additions & 8 deletions domain/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ import (
func TestMatcher_Match(t *testing.T) {
m := Matcher{items: map[string]string{"abbreviation": "abbr"}}

assert.Equal(t, "abbr", m.Match("abbreviation"))
assert.Equal(t, "something", m.Match("something"))
assert.Equal(t, "abbr", m.Match("abbreviation", ""))
assert.Equal(t, "something", m.Match("something", ""))
}

func TestMatcher_Strategy(t *testing.T) {
m := Matcher{items: map[string]string{"limited": "ltd"}}

assert.Equal(t, "smthng", m.Match("something", "removeVowel"))
assert.Equal(t, "ltd", m.Match("limited", "removeVowel"))
assert.Equal(t, "ltd", m.Match("limited", ""))
}

func TestMatcher_NewMatcherFromString(t *testing.T) {
Expand All @@ -24,12 +32,12 @@ func TestMatcher_NewMatcherFromString(t *testing.T) {

`)

assert.Equal(t, "abbr", m.Match("abbreviation"))
assert.Equal(t, "2", m.Match("b"))
assert.Equal(t, "3", m.Match("3"))
assert.Equal(t, "c", m.Match("c"))
assert.Equal(t, "d", m.Match("d"))
assert.Equal(t, "dd", m.Match("dd"))
assert.Equal(t, "abbr", m.Match("abbreviation", ""))
assert.Equal(t, "2", m.Match("b", ""))
assert.Equal(t, "3", m.Match("3", ""))
assert.Equal(t, "c", m.Match("c", ""))
assert.Equal(t, "d", m.Match("d", ""))
assert.Equal(t, "dd", m.Match("dd", ""))
}

func TestMatcher_All(t *testing.T) {
Expand Down
12 changes: 6 additions & 6 deletions domain/shorteners.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ type Shortener func(matcher Matcher, original string, max int) string
// AsOriginal discovers words using camel case and non letter characters,
// starting from the back or the front until the string has less than 'max' characters
// or it can't shorten any more.
func AsOriginal(matcher *Matcher, original string, max int, frmFront bool) string {
func AsOriginal(matcher *Matcher, original string, max int, frmFront bool, strategy string) string {
if len(original) < max {
return original
}

shortened := NewSequences(original)
shorten(shortened, max, frmFront, func(pos int) {
str := shortened[pos]
abbr := matcher.Match(strings.ToLower(str))
abbr := matcher.Match(strings.ToLower(str), strategy)
if isTitleCase(str) {
abbr = makeTitle(abbr)
}
Expand All @@ -120,7 +120,7 @@ func AsOriginal(matcher *Matcher, original string, max int, frmFront bool) strin
// starting from the back or the front until the string has less than 'max' characters
// or it can't shorten any more. This inserts the specified separator
// where a sequence is not alpha-numeric
func AsSeparated(matcher *Matcher, original, separator string, max int, frmFront bool) string {
func AsSeparated(matcher *Matcher, original, separator string, max int, frmFront bool, strategy string) string {
if original == "" {
return ""
}
Expand All @@ -144,7 +144,7 @@ func AsSeparated(matcher *Matcher, original, separator string, max int, frmFront

shorten(shortened, max, frmFront, func(pos int) {
str := shortened[pos]
abbr := matcher.Match(str)
abbr := matcher.Match(str, strategy)
shortened[pos] = abbr
})

Expand All @@ -155,7 +155,7 @@ func AsSeparated(matcher *Matcher, original, separator string, max int, frmFront
// starting from the back or the front until the string has less than 'max' characters
// or it can't shorten any more. Word boundaries are a capital letter at
// the start of each word
func AsPascal(matcher *Matcher, original string, max int, frmFront bool) string {
func AsPascal(matcher *Matcher, original string, max int, frmFront bool, strategy string) string {
if original == "" {
return ""
}
Expand All @@ -179,7 +179,7 @@ func AsPascal(matcher *Matcher, original string, max int, frmFront bool) string

shorten(shortened, max, frmFront, func(pos int) {
str := strings.ToLower(shortened[pos])
abbr := matcher.Match(str)
abbr := matcher.Match(str, strategy)
abbr = makeTitle(abbr)
shortened[pos] = abbr
})
Expand Down
9 changes: 6 additions & 3 deletions domain/shorteners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ltd=limited`)
max int
frmFront bool
want string
strategy string
}{
{name: "Length longer than origin with '-'", original: "aaa-bbb-ccc", max: 99, want: "aaa-bbb-ccc"},
{name: "Length is 0 with '-'", original: "aaa-bbb-ccc", max: 0, want: "a-b-c"},
Expand Down Expand Up @@ -49,7 +50,7 @@ ltd=limited`)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := AsOriginal(matcher, tt.original, tt.max, tt.frmFront); got != tt.want {
if got := AsOriginal(matcher, tt.original, tt.max, tt.frmFront, tt.strategy); got != tt.want {
t.Errorf("AsOriginal('%s', %d) = '%v', want '%v'", tt.original, tt.max, got, tt.want)
}
})
Expand Down Expand Up @@ -171,6 +172,7 @@ ltd=limited`)
max int
frmFront bool
want string
strategy string
}{
{name: "Length is 0 with '-'", original: "aaa-bbb-ccc", separator: "_", max: 0, want: "a_b_c"},
{name: "Partial abbreviation with '_'", original: "aaa-bbb-ccc", separator: "_", max: 10, want: "aaa_bbb_c"},
Expand Down Expand Up @@ -203,7 +205,7 @@ ltd=limited`)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := AsSeparated(matcher, tt.original, tt.separator, tt.max, tt.frmFront); got != tt.want {
if got := AsSeparated(matcher, tt.original, tt.separator, tt.max, tt.frmFront, tt.strategy); got != tt.want {
t.Errorf("AsSeparated() = %v, want %v", got, tt.want)
}
})
Expand All @@ -223,6 +225,7 @@ ltd=limited`)
max int
frmFront bool
want string
strategy string
}{
{name: "Length longer than origin with '-'", original: "aaa-bbb-ccc", max: 99, want: "AaaBbbCcc"},
{name: "Length is 0 with '-'", original: "aaa-bbb-ccc", max: 0, want: "ABC"},
Expand Down Expand Up @@ -251,7 +254,7 @@ ltd=limited`)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := AsPascal(matcher, tt.original, tt.max, tt.frmFront); got != tt.want {
if got := AsPascal(matcher, tt.original, tt.max, tt.frmFront, tt.strategy); got != tt.want {
t.Errorf("AsPascal('%s', %d) = '%v', want '%v'", tt.original, tt.max, got, tt.want)
}
})
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=