Skip to content

Commit

Permalink
Adding new processor revers lines
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimanyu003 committed Dec 23, 2021
1 parent 7bfd297 commit c87ac19
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 3 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,6 @@ echo "Hello World" | sttr base64-encode | sttr md5
- [x] **reverse** - Reverse Text ( txeT esreveR )
- [x] **slug** - Transform your text to slug-case
- [x] **snake** - Transform your text to snake_case
- [x] **sort-lines** - Sort lines alphabetically
- [x] **shuffle-lines** - Shuffle lines randomly
- [x] **unique-lines** - Get unique lines from list
- [x] **title** - Transform your text to Title Case
- [x] **upper** - Transform your text to UPPER CASE

Expand All @@ -171,6 +168,14 @@ echo "Hello World" | sttr base64-encode | sttr md5
- [x] **count-lines** - Count the number of lines in your text
- [x] **count-words** - Count the number of words in your text

### Lines

- [x] **count-lines** - Count the number of lines in your text
- [x] **reverse-lines** - Reverse lines
- [x] **shuffle-lines** - Shuffle lines randomly
- [x] **sort-lines** - Sort lines alphabetically
- [x] **unique-lines** - Get unique lines from list

#### RGB/Hex

- [x] **hex-rgb** - Convert a #hex-color code to RGB
Expand Down
56 changes: 56 additions & 0 deletions cmd/processor_reverse-lines.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions processors/lines.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,42 @@ func (p UniqueLines) Description() string {
func (p UniqueLines) FilterValue() string {
return p.Title()
}

// ReverseLines sort given lines, in random order.
type ReverseLines struct{}

func (p ReverseLines) Name() string {
return "reverse-lines"
}

func (p ReverseLines) Alias() []string {
return nil
}

func (p ReverseLines) Transform(data []byte, _ ...Flag) (string, error) {
var output []string
split := strings.Split(string(data), "\n")
length := len(split) - 1

for i := length; i >= 0; i-- {
output = append(output, split[i])
}

return strings.Join(output, "\n"), nil
}

func (p ReverseLines) Flags() []Flag {
return nil
}

func (p ReverseLines) Title() string {
return "Reverse Lines"
}

func (p ReverseLines) Description() string {
return "Reverse Lines"
}

func (p ReverseLines) FilterValue() string {
return p.Title()
}
87 changes: 87 additions & 0 deletions processors/lines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,90 @@ func TestUniqueLines_Transform(t *testing.T) {
})
}
}

func TestReverseLines_Command(t *testing.T) {
test := struct {
alias []string
description string
filterValue string
flags []Flag
name string
title string
}{
alias: nil,
description: "Reverse Lines",
filterValue: "Reverse Lines",
flags: nil,
name: "reverse-lines",
title: "Reverse Lines",
}
p := ReverseLines{}
if got := p.Alias(); !reflect.DeepEqual(got, test.alias) {
t.Errorf("Alias() = %v, want %v", got, test.alias)
}
if got := p.Description(); got != test.description {
t.Errorf("Description() = %v, want %v", got, test.description)
}
if got := p.FilterValue(); got != test.filterValue {
t.Errorf("Flags() = %v, want %v", got, test.filterValue)
}
if got := p.Flags(); !reflect.DeepEqual(got, test.flags) {
t.Errorf("Flags() = %v, want %v", got, test.flags)
}
if got := p.Name(); got != test.name {
t.Errorf("Name() = %v, want %v", got, test.name)
}
if got := p.Title(); got != test.title {
t.Errorf("Title() = %v, want %v", got, test.title)
}
}

func TestReverseLines_Transform(t *testing.T) {
type args struct {
data []byte
opts []Flag
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "Reverse lines with numbers",
args: args{data: []byte("1\n2\n3\n4")},
want: "4\n3\n2\n1",
},
{
name: "Reverse lines with numbers and alpha",
args: args{data: []byte("1\n2\ntest")},
want: "test\n2\n1",
},
{
name: "Empty input",
args: args{data: []byte("")},
want: "",
},
{
name: "Single input",
args: args{data: []byte("1")},
want: "1",
},
{
name: "Single input with new line",
args: args{data: []byte("1\n")},
want: "\n1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// We are going run test 100 to make sure that list order is preserved.
for i := 0; i < 100; i++ {
p := ReverseLines{}
if got, _ := p.Transform(tt.args.data, tt.args.opts...); got != tt.want {
t.Errorf("ReverseLines() = %v, want %v", got, tt.want)
}
}
})
}
}
1 change: 1 addition & 0 deletions processors/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var List = []list.Item{
Markdown{},
ROT13Encode{},
Reverse{},
ReverseLines{},
SHA1{},
SHA256{},
SHA512{},
Expand Down

0 comments on commit c87ac19

Please sign in to comment.