Skip to content

Commit

Permalink
feat: add PascalCase
Browse files Browse the repository at this point in the history
  • Loading branch information
ccoVeille committed Jul 28, 2024
1 parent 918d7fb commit 7e5c9ba
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ echo "Hello World" | sttr base64-encode | sttr md5
- [x] **camel** - Transform your text to camelCase
- [x] **kebab** - Transform your text to kebab-case
- [x] **lower** - Transform your text to lower case
- [x] **pascal** - Transform your text to PascalCase
- [x] **reverse** - Reverse Text ( txeT esreveR )
- [x] **slug** - Transform your text to slug-case
- [x] **snake** - Transform your text to snake_case
Expand Down
53 changes: 53 additions & 0 deletions cmd/processor_pascal.go

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

1 change: 1 addition & 0 deletions hugo/content/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ echo "Hello World" | sttr base64-encode | sttr md5
* [sttr lower]({{< relref "sttr_lower.md" >}}) - Transform your text to lower case
* [sttr markdown-html]({{< relref "sttr_markdown-html.md" >}}) - Convert Markdown to HTML
* [sttr md5]({{< relref "sttr_md5.md" >}}) - Get the MD5 checksum of your text
* [sttr pascal]({{< relref "sttr_pascal.md" >}}) - Transform your text to PascalCase
* [sttr reverse]({{< relref "sttr_reverse.md" >}}) - Reverse Text ( txeT esreveR )
* [sttr rot13-encode]({{< relref "sttr_rot13-encode.md" >}}) - Encode your text to ROT13
* [sttr sha1]({{< relref "sttr_sha1.md" >}}) - Get the SHA-1 checksum of your text
Expand Down
1 change: 1 addition & 0 deletions hugo/content/cli/sttr.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ sttr [flags]
* [sttr lower]({{< relref "sttr_lower.md" >}}) - Transform your text to lower case
* [sttr markdown-html]({{< relref "sttr_markdown-html.md" >}}) - Convert Markdown to HTML
* [sttr md5]({{< relref "sttr_md5.md" >}}) - Get the MD5 checksum of your text
* [sttr pascal]({{< relref "sttr_pascal.md" >}}) - Transform your text to PascalCase
* [sttr reverse]({{< relref "sttr_reverse.md" >}}) - Reverse Text ( txeT esreveR )
* [sttr rot13-encode]({{< relref "sttr_rot13-encode.md" >}}) - Encode your text to ROT13
* [sttr sha1]({{< relref "sttr_sha1.md" >}}) - Get the SHA-1 checksum of your text
Expand Down
1 change: 1 addition & 0 deletions processors/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var List = []list.Item{
MorseCodeDecode{},
MD5{},
MSGPACKToJSON{},
Pascal{},
RemoveNewLines{},
RemoveSpaces{},
Reverse{},
Expand Down
34 changes: 34 additions & 0 deletions processors/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,40 @@ func (p Camel) FilterValue() string {
return p.Title()
}

// Pascal convert string to CamelCase.
// Example: "this is string" to "ThisIsString".
type Pascal struct{}

func (p Pascal) Name() string {
return "pascal"
}

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

func (p Pascal) Transform(data []byte, _ ...Flag) (string, error) {
str := regexp.MustCompile(`\s+`).ReplaceAllString(string(data), " ")
return strcase.ToCamel(str), nil
}

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

func (p Pascal) Title() string {
title := "To Pascal case"
return fmt.Sprintf("%s (%s)", title, p.Name())
}

func (p Pascal) Description() string {
return "Transform your text to PascalCase"
}

func (p Pascal) FilterValue() string {
return p.Title()
}

// Slug convert string to StringToSlug. It's similar to Kebab case but URL Friendly.
// Example: "this is string" to "this-is-string".
type Slug struct{}
Expand Down
94 changes: 94 additions & 0 deletions processors/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,100 @@ func TestStringToCamel(t *testing.T) {
}
}

func TestPascal_Command(t *testing.T) {
test := struct {
alias []string
description string
filterValue string
flags []Flag
name string
title string
}{
alias: nil,
description: "Transform your text to PascalCase",
filterValue: "To Pascal case (pascal)",
flags: nil,
name: "pascal",
title: "To Pascal case (pascal)",
}
p := Pascal{}
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("FilterValue() = %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 TestStringToPascal(t *testing.T) {
type args struct {
data []byte
in1 []Flag
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "Normal String",
args: args{data: []byte("the quick brown fox jumps over a lazy dog")},
want: "TheQuickBrownFoxJumpsOverALazyDog",
},
{
name: "String Uppercase",
args: args{data: []byte("THE QUICK BROWN FOX JUMPS OVER A LAZY DOG")},
want: "TheQuickBrownFoxJumpsOverALazyDog",
},
{
name: "Camel Case Text",
args: args{data: []byte("camelCaseText")},
want: "CamelCaseText",
},
{
name: "Pacal Case Text",
args: args{data: []byte("PacalCaseText")},
want: "PacalCaseText", // stable
},
{
name: "Underscore text lowercase",
args: args{data: []byte("underscore_text")},
want: "UnderscoreText",
},
{
name: "Underscore text uppercase",
args: args{data: []byte("UNDERSCORE_TEXT_UPPER_CASE")},
want: "UnderscoreTextUpperCase",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := Pascal{}
got, err := p.Transform(tt.args.data, tt.args.in1...)
if (err != nil) != tt.wantErr {
t.Errorf("Transform() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Transform() got = %v, want %v", got, tt.want)
}
})
}
}

func TestEscapeQuotes_Command(t *testing.T) {
test := struct {
alias []string
Expand Down

0 comments on commit 7e5c9ba

Please sign in to comment.