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

slug string #62

Merged
merged 2 commits into from
Apr 16, 2024
Merged
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
41 changes: 38 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions slug_string.go
Original file line number Diff line number Diff line change
@@ -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
}
48 changes: 48 additions & 0 deletions slug_string_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
Loading