From 061a558760496394958da368764c01fcd5b7089f Mon Sep 17 00:00:00 2001 From: James Reategui Date: Tue, 16 Apr 2024 11:42:11 -0400 Subject: [PATCH] slug string (#62) * slug string * update linter --- .golangci.yml | 41 +++++++++++++++++++++++++++++++++++--- slug_string.go | 24 +++++++++++++++++++++++ slug_string_test.go | 48 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 slug_string.go create mode 100644 slug_string_test.go diff --git a/.golangci.yml b/.golangci.yml index 7af3572..3d2f3eb 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -5,13 +5,48 @@ linters: - prealloc - revive - goimports - - deadcode - errcheck - gosimple - govet - ineffassign - staticcheck - - structcheck - typecheck - unused - - varcheck + - zerologlint + +issues: + fix: true + +linters-settings: + gci: + # DEPRECATED: use `sections` and `prefix(github.com/org/project)` instead. + local-prefixes: github.com/org/project + # Section configuration to compare against. + # Section names are case-insensitive and may contain parameters in (). + # The default order of sections is `standard > default > custom > blank > dot`, + # If `custom-order` is `true`, it follows the order of `sections` option. + # Default: ["standard", "default"] + sections: + - standard # Standard section: captures all standard packages. + - default # Default section: contains all imports that could not be matched to another section type. + - prefix(github.com/DIMO-Network/shared) # Custom section: groups all imports with the specified Prefix. + - blank # Blank section: contains all blank imports. This section is not present unless explicitly enabled. + - dot # Dot section: contains all dot imports. This section is not present unless explicitly enabled. + # Skip generated files. + # Default: true + skip-generated: true + # Enable custom order of sections. + # If `true`, make the section order the same as the order of `sections`. + # Default: false + custom-order: false + tagliatelle: + # Check the struck tag name case. + case: + # Use the struct field name to check the name of the struct tag. + # Default: false + use-field-name: true + rules: + # Any struct tag type can be used. + # Support string case: `camel`, `pascal`, `kebab`, `snake`, `upperSnake`, `goCamel`, `goPascal`, `goKebab`, `goSnake`, `upper`, `lower`, `header`. + json: camel + yaml: upperSnake \ No newline at end of file diff --git a/slug_string.go b/slug_string.go new file mode 100644 index 0000000..b431a80 --- /dev/null +++ b/slug_string.go @@ -0,0 +1,24 @@ +package shared + +import ( + "strings" + "unicode" + + "golang.org/x/text/cases" + "golang.org/x/text/language" + "golang.org/x/text/runes" + "golang.org/x/text/transform" + "golang.org/x/text/unicode/norm" +) + +func SlugString(term string) string { + lowerCase := cases.Lower(language.English, cases.NoLower) + lowerTerm := lowerCase.String(term) + + t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC) + cleaned, _, _ := transform.String(t, lowerTerm) + cleaned = strings.ReplaceAll(cleaned, " ", "-") + cleaned = strings.ReplaceAll(cleaned, "_", "-") + + return cleaned +} diff --git a/slug_string_test.go b/slug_string_test.go new file mode 100644 index 0000000..694cfb5 --- /dev/null +++ b/slug_string_test.go @@ -0,0 +1,48 @@ +package shared + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSlugString(t *testing.T) { + tests := []struct { + term string + want string + }{ + { + term: "Mercedes-Benz", + want: "mercedes-benz", + }, + { + term: "AC", + want: "ac", + }, + { + term: "AM General", + want: "am-general", + }, + { + term: "Alfa Romeo", + want: "alfa-romeo", + }, + { + term: "Citroën", + want: "citroen", + }, + { + term: "SsangYong", + want: "ssangyong", + }, + { + term: "Land Rover", + want: "land-rover", + }, + } + for _, tt := range tests { + t.Run(tt.term, func(t *testing.T) { + assert.Equalf(t, tt.want, SlugString(tt.term), "SlugString(%v)", tt.term) + }) + } +}