diff --git a/README.md b/README.md index e6d8e37..5c8dd8a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cmd/processor_pascal.go b/cmd/processor_pascal.go new file mode 100644 index 0000000..c540ae1 --- /dev/null +++ b/cmd/processor_pascal.go @@ -0,0 +1,53 @@ +// Code generated by github.com/abhimanyu003/sttr/cmd/generate.go. DO NOT EDIT + +package cmd + +import ( + "fmt" + "os" + + "github.com/abhimanyu003/sttr/processors" + "github.com/abhimanyu003/sttr/utils" + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(pascalCmd) +} + +var pascalCmd = &cobra.Command{ + Use: "pascal [string]", + Short: "Transform your text to PascalCase", + Aliases: []string{}, + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + var err error + var in []byte + var out string + + if len(args) == 0 { + in = []byte(utils.ReadMultilineInput()) + } else { + if fi, err := os.Stat(args[0]); err == nil && !fi.IsDir() { + d, err := os.ReadFile(args[0]) + if err != nil { + return err + } + in = d + } else { + in = []byte(args[0]) + } + } + + flags := make([]processors.Flag, 0) + p := processors.Pascal{} + + out, err = p.Transform(in, flags...) + if err != nil { + return err + } + + _, err = fmt.Fprint(os.Stdout, out) + return err + }, +} diff --git a/hugo/content/_index.md b/hugo/content/_index.md index 09a183b..f47e970 100644 --- a/hugo/content/_index.md +++ b/hugo/content/_index.md @@ -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 diff --git a/hugo/content/cli/sttr.md b/hugo/content/cli/sttr.md index 0113a68..14ad690 100644 --- a/hugo/content/cli/sttr.md +++ b/hugo/content/cli/sttr.md @@ -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 diff --git a/processors/processor.go b/processors/processor.go index 7cb4fe7..9a6cf61 100644 --- a/processors/processor.go +++ b/processors/processor.go @@ -43,6 +43,7 @@ var List = []list.Item{ MorseCodeDecode{}, MD5{}, MSGPACKToJSON{}, + Pascal{}, RemoveNewLines{}, RemoveSpaces{}, Reverse{}, diff --git a/processors/strings.go b/processors/strings.go index d1ff63b..83eaeed 100644 --- a/processors/strings.go +++ b/processors/strings.go @@ -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{} diff --git a/processors/strings_test.go b/processors/strings_test.go index c9b31f3..8ad9109 100644 --- a/processors/strings_test.go +++ b/processors/strings_test.go @@ -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